Spring commonly used annotation (@ Entity, @ Table, @ Column, @ Repository, @ Service)

When the project becomes relatively large, but also how to use hbm.xml file to configure Hibernate entity becomes more complicated. Here Hibernate provides Annotation annotation way that Hibernate mapping files become very easy to manage.

        Here's a brief Hibernate Annotations Annotation

        A statement entity

       @Entity

            Entity comments. Any Hibernate mapping objects should have this comment

      @Table

           Disclaimer This object is mapped to a data table in the database, which allows you to specify the entity table (talbe), catalog (Catalog) and schema name. This comment is not necessary, if the system does not use the default value (short class name of an entity).

     @Version

             The annotation can be used to add optimistic locking support in the Entity Bean.

 Second, the primary key statement

    @Id

     This property is declared as the primary key. The attribute value can be created itself, but recommended by Hibernate Hibernate generated

    @GeneratedValue

    Specify the primary key generation strategy. We have the following four values

               TABLE: Table id value preservation

               IDENTITY:identitycolumn

               SEQUENCR :sequence

               AUTO: The three different databases used above

Third, the declaration of a general property

@Column

 Statement mapping of the property and database fields.

1@Column(nam=”category_name” length=20)2Publicvoid getCategoryName(){3Returnthis.categoryName;4}

 note:

          1, when the property does not need to have POJO mapping must use @Transitent modification, the comment indicates that this property is no mapping table is only a temporary property.

          2, @Lob annotation indicates that the property persisted as Clob or Blob type, depending on the type of attribute.

Fourth, the declaration relationship

        To-many relationship

  @OneToMany(mappedBy=” person”,cascade=CascadeType.ALL,fetch=FetchType.LAZY)

Many declarations

        @ManyToOne(cascade=CascadeType.REFRESH,)

        @JoinColumn

         Many-statement, the statement is bidirectional associations

        One to one relationship

        @OneToOne(optional= true,cascade =CascadeType.ALL, mappedBy = “person”)

        One association statement

        @OneToOne(optional = false, cascade = CascadeType.REFRESH)

        @JoinColumn(name = “Person_ID”, referencedColumnName = “personid”,unique = true)

        Declared as bidirectional associations

        To-many relationship

        @ManyToMany(mappedBy= “students”)

        Many to many association statement.

        @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)

        @JoinTable(name = “Teacher_Student”,

        joinColumns = {@JoinColumn(name = “Teacher_ID”, referencedColumnName =“teacherid”)},

        inverseJoinColumns = {@JoinColumn(name = “Student_ID”, referencedColumnName =“studentid”)})

@Repository

spring since version 2.0, after another introduces some annotations to simplify the development of Spring. @Repository notes will belong to a group of first introduced it for the class data access layer (DAO layer) is identified as a Spring Bean. The only specific annotations can be marked on the DAO classes. Meanwhile, in order to make Spring can scan classpath class and identify @Repository comment, you need to enable automatic scanning function Bean in the XML configuration file, which can be achieved.

So, we no longer need to explicitly used in XML

Bean conduct configuration. Spring when the container initialization all packets in the class file automatically scan base-package and its sub-packets specified, all marked

@Repository classes will be registered as a Spring Bean.

Why @Repository only marked DAO

Class on it? This is because not only the effect of the annotation as a class identification Bean, it can simultaneously marked class data access exceptions thrown exception type of encapsulation Spring access data.

Spring itself provides a rich and is independent of the access technology specific data structure data access exceptions, for different packages persistence framework thrown exception, such that the frame independent of the underlying abnormality.

@ Service, @ Controller and @Component will identify this as Bean

Spring 2.5 @Repository on the basis of an increase of three additional functionality similar comment: @ Component, @ Service, @ Constroller, which are used for different levels of software systems:

@Component is a generalization of the concept, represent only one component (Bean), may act at any level.

@Service usually applied in the business layer, but this feature is currently the @Component same.

@Constroller usually applied in the control layer, but present the same function @Component.

By the use of the class

@ Repository, @ Component, @ Service and @Constroller annotations, Spring will automatically create the appropriate

BeanDefinition object and registered to ApplicationContext. These classes became

Spring managed assembly. The three notes in addition to acting on the different software-level classes, using exactly the same way as @Repository.

Also, in addition to the above four notes, users can create custom annotations and notes on the label @Component, then the custom annotation will have @Component with the same functionality. But this feature is not commonly used.

When a Bean is automatically detected, based on the scanner

BeanNameGenerator strategy generating its bean name. By default, for the name attribute of

@ Component, @ Repository, @Service and @Controller, will name value as Bean

name. If the comment does not contain a name or other value components are found in custom filters, Bean default name will be non-qualified class name in lowercase beginning. If you do not want to use the default

bean naming strategy, naming strategy can provide a custom. First, realize

BeanNameGenerator interfaces, be sure to include a default constructor without parameters. Then providing a fully qualified class name when configuring the scanner, as shown below:

 

And configured through XML Spring Bean

Like, by Bean The Notes are identified, the default scope is a "singleton", in order to meet these four notes, while Bean label can specify Bean

Scope, Spring2.5 introduced @Scope comment. Just provide the name of the scope of the use of the comment on the line, as follows:

@Scope("prototype") @Repository publicclassDemo { … }

If you want to provide a custom scope resolution strategies without using annotation-based method, just realize ScopeMetadataResolver interface to make a default constructor contains no parameters. Fully qualified class name is then provided when configuring the scanner:

Guess you like

Origin www.cnblogs.com/qingmuchuanqi48/p/11619602.html