Development of annotations in hibernate

Source: Internet
Author: User

Using annotations to develop a database persistence layer once, you need to learn a specification JPA (Java persistence API), which is the persistence specification of the database proposed by Sun Company.

is similar to JDBC. Servlet,jsp and other specifications. The Hibernate framework implements this specification, and we can implement this specification through annotations and configuration files. The previous study is based on the configuration file, and annotation development is now in a trend. After all, it greatly improves the speed of development.

Take a look at the development knowledge of annotations in the Hibernate framework.

One. First, you need to import a jar package that supports annotation development:

Hibernate-annotations.jar

Hibernate-commons-annotations.jar (jar package supported for annotation development in hibernate)

Ejb3-persistence.jar (API Spec JAR package)

Slf4j/slf4j-api-1.5.0.jar

Slf4j-log4j12-1.5.0.jar

Log4j-1.2.15.jar (jar package that supports log output.) Note the match of the version number)

Two. During the development of annotations in the Hibernate framework, the main substitution is for the mapping file of the entity and the data table, and the core configuration file. database connection, etc., or the need for an XML configuration file to configure. Let's take a look at the frequently used annotations in hibernate:

1. @Entity: Indicates that the current class is an entity class, declared before the class.

The ability to associate mappings between the current class and the tables and fields of the database.

2. @Table: Represents a mapped table, declared in front of the class

3, @Id: Represents a primary key mapping, declared before the Get method of the primary key, by default the primary key takes advantage of assigned (user supplied) to generate the policy. Suppose you want to set up an API specification that provides four primary key generation strategies:

@GeneratedValue (Strategy=generationtype.auto) equivalent to the native primary key generation strategy of the Hibernate framework

The @GeneratedValue (strategy=generationtype.identity) corresponds to the IDENTITY primary key generation strategy of the Hibernate framework. Applies to Mysql,sql Server

The @GeneratedValue (strategy=generationtype.sequence) corresponds to the SEQUENCE primary key generation strategy of the Hibernate framework for ORACLE,DB2

@GeneratedValue (strategy=generationtype.table) uses a single table to manage primary key values.

By default, the API specification is a primary key generation strategy that does not support UUID in the Hibernate framework, if we want to use it. We need to add a primary key generator, such as the following:

@GenericGenerator (name= "Hibernate-uuid", strategy= "UUID")

@GeneratedValue (generator= "Hibernate-uuid")

4, @Column: Represents the Mapping property. Associating with a table's fields

5. @ManyToOne: Represents many-to-one in a mapping relationship

6. @JoinColumn: Represents a foreign key mapping property, associated with a table's foreign key field

7, @OneToMany: The mapping relationship in the one-to-many, it is important to note that a couple of long, foreign key maintenance needs to be flipped over to the side of the maintenance, you need to configure the reverse attribute Mappedby, the property value is more than the name of a party mapping attribute. Equivalent to inverse= "true" in the XML configuration file.

8, @OrderBy: Indicates a sort field

9, @Transient: Represents an instantaneous attribute that is not associated with a database field, that is, there is no mapping of columns in the table.

Wait, I actually feel like these properties and XML configuration file properties are all one by one appropriate. Not the same in time. There will also be similar properties to implement its function, so we can use it accordingly.

Of course, it's just something that's often used here. We also need to review the documentation to write when we need it.

Three. Let's look at a simple entity class that is developed with a primary key, and there's no need for us to write the configuration file again:

 

@Entity @Table (name= "t_org") public class Org {private String ID;      private String name;      Private String Code;      Private String Manager;      Private String remark;      Private String Createtime;            Private String UpdateTime;            Private Orgtype Orgtype;            Private String pid;      @Transient public String Getpid () {return PID;      } public void Setpid (String pid) {this.pid = pid;            } private Org parentorg;        Private set<org> Childorgset; @Id @GenericGenerator (name= "Hibernate-uuid", strategy= "UUID") @GeneratedValue (generator= "Hibernate-uuid") p      Ublic String getId () {return id;      } public void SetId (String id) {this.id = ID;      } public String GetName () {return name;      } public void SetName (String name) {this.name = name;      } public String GetCode () {return code;        }public void Setcode (String code) {this.code = code;      } public String GetManager () {return manager;      } public void Setmanager (String manager) {This.manager = manager;      } public String Getremark () {return remark;      } public void Setremark (String remark) {This.remark = remark;      } @Column (Updatable=false) public String getcreatetime () {return createtime;      } public void Setcreatetime (String createtime) {this.createtime = Createtime;      } @Column (Insertable=false) public String getupdatetime () {return updatetime;      } public void Setupdatetime (String updatetime) {this.updatetime = UpdateTime;      } @ManyToOne @JoinColumn (name= "Orgtypeid") public Orgtype Getorgtype () {return orgtype;      } public void Setorgtype (Orgtype orgtype) {this.orgtype = Orgtype;        }@ManyToOne @JoinColumn (name= "pid", Updatable=false) public Org getparentorg () {return parentorg;      } public void setparentorg (Org parentorg) {this.parentorg = parentorg; } @OneToMany (mappedby= "parentorg") @JoinColumn (name= "pid") @OrderBy ("code") public set<org> ge      Tchildorgset () {return childorgset;      } public void Setchildorgset (set<org> childorgset) {this.childorgset = Childorgset;   }  }


Four. Of course, with the development of annotations, we need to change the core configuration file, this will require Hibernate Annotationsessionfactorybean factory class,

<!--Hibernate Core Object--      <bean id= "sessionfactory" class= " Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean ">            <!--omit ......-->           < !--Specifies a package that has been developed for hibernate annotations. Easy frame Scan Settings-           <property name= "Packagestoscan" >              <list>                  <value>com/ljh/ppp/ domain</value>              </list>          </property>      </bean>  

In this way, the annotation development of the Hibernate framework is basically completed, and the overall feeling is more efficient. We didn't write a class one method, and by the way we wrote the annotations, so we didn't have to write the mapping file specifically. Mastering the development of annotations can greatly improve our efficiency in many times. Of course there are other frameworks for annotations to use, which are summarized behind.

Development of annotations in hibernate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.