Learning hibernate JPA

Source: Internet
Author: User
In recent years, ORM (object-relational mapping; object relationship ing, that is, ing between object and database tables) technology has become a buzz in the market, sun has developed a new JPA (Java persistence API) specification based on the existing good ORM framework design ideas. So what is JPA? JPA describes the ing between an object and a relational table through the jdk5.0 annotation or XML, and persists the object to the database at runtime.

In recent years, ORM (object-relational mapping; object relationship ing, that is, ing between object and database tables) technology has become a buzz in the market, sun has developed a new JPA (Java persistence API) specification based on the existing good ORM framework design ideas. So what is JPA? JPA describes the ing between an object and a relational table through the jdk5.0 annotation or XML, and persists the object to the database at runtime. Here we will first talk about what is entity. According to the JPA specification, domain objects with ORM metadata are called entities. It should meet the following conditions: 1. javax. persistence. there are corresponding <entity> elements in the Entity annotation or XML ing file; 2. must have a constructor without parameters. The class cannot be declared as final, and the methods and attributes that require persistence cannot be declared as final; 3. if the object in the Free State needs to be passed by value (for example, through the remote service interface of Session Bean), the serializable interface must be implemented; 4. A persistent attribute. The access modifier cannot be public. It must be accessed through the object class method. Next we will try to annotate a domain object with JPA to make it an object class:
@ Entity (name = "t_test ")
Public class test implements serializable {
@ ID
@ Generatedvalue (Strategy = generationtype. Table)
@ Column (name = "Id ")
Private int testid;

@ Column (name = "uname", length = 100)
Private string uname;

@ Column (name = "Password ")
Private string password;

@ Column (name = "time ")
@ Temporal (temporaltype. Date)
Private date logintime;

Omitting the get/setter Method
}
Next, Let's explain the JPA annotations involved in the above Code @ entity: Mark the domain object as an entity class, indicating that the class needs to be persisted to the database, by default, the class name is the table name. The name attribute is used to explicitly specify the table name. For example, name = "t_test" indicates that test is saved to the t_test table. @ ID: The corresponding attribute is the table's primary key @ generatedvalue: The primary key generation policy, which is specified through the Strategy attribute. By default, JPA automatically selects a primary key generation policy most suitable for the underlying database, such as the identity: MySQL auto increment corresponding to sqlserver, in Java. persistence. generationtype defines several options: 1.: The table auto-increment field. Oracle does not support this method. identity2.: JPA automatically selects an appropriate policy, which is the default option. auto3.: generate a primary key through sequence, and specify the sequence name through @ sequencegenerator annotation, mySQL does not support this method. Sequence4. Table @ colunm (name = "uname"): The table field corresponding to the attribute. We do not need to specify the table field type, because JPA obtains the type from the object Attribute Based on reflection. If it is a string type, we can specify the field length to automatically generate DDL statements. @ Temporal (temporaltype. date): If the attribute is of the time type, because the data table has a more strict division of the time type, you must specify the specific time type in Java. persistence. the temporaltype enumeration defines three time types: Date: equal to Java. SQL. date; Time: equal to Java. SQL. time; timestamp: equal to Java. SQL. timestamp. For classes with parent-child relationships, JPA must declare the ing policies of inherited entities for the parent class. For inherited entities, Java. persistence. inheritancetype defines three ing policies: single_table: Parent and Child classes are stored in the same table and distinguished by field values. Joined: the same parts of the Parent and Child classes are stored in the same table. Different departments are stored separately and complete data is obtained by connecting different tables. Table_per_class: each class corresponds to its own table. This method is generally not recommended. Next, let's take a look at how the actual column sub is used. Parent class test @ entity (name = "test") @ inheritance (Strategy = inheritancetype. single_table) // specify the inheritance policy @ discriminatorcolumn (name = "types", discriminatortype = discriminatortype. integer, length = 1) // specifies that the distinguished field is types and the type is integer. The length is 1 @ discriminatorvalue (value = "1 ") // The value of the corresponding object public class test implements serializable {.....} Subclass child @ entity @ discriminatorvalue (value = "2") public class Child extends test {// if we do not want JPA to persist this attribute to the database, the annotation @ transientprivate string tempstr; @ lob // lob type field @ basic (fetch = fetchtype. lazy) // uses delayed loading and fetchtype. the eager does not use @ column (name = "postattach", columndefinition = "longtext not null") corresponding to the field type private string postattach;} You can see that the field types is used to distinguish Parent and Child data, it is also quite convenient. As for the association relationship provided by JPA, for example, one-to-many, many-to-many, there are also corresponding annotations for association. If you are interested, you can refer to the relevant help documentation. The above is about JPA in the form of annotation for persistence, Next let's look at the form of XML Metadata, XML Metadata Information named by Orm. XML, placed in the class path of the META-INF directory.
<?xml version=”1.0” encoding=”UTF-8”?>
<entity-mappings
xmlns=”http://java.sun.com/xml/ns/persistence/orm” xmlns=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd”
version=”1.0”>
<package>com.test</package>
<entity class=”Test”>
<table name=”test”/>
<attributes>
<id name=”id”>
<column name=”id”/>
<generated-value strategy=”TABLE”/>
</id>
<basic name=”uname”>
<column name=”uname” length=”30”/>
</basic>
<basic name=”logintime”>
<column name=” logintime”/>
<temporal>DATE</temporal>
</basic>
</attributes>
</entity>
<entity-mappings>
It can be seen that the JPA metadata is in XML format and is quite easy to understand.

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.