Annotation configuration of javax. Persistence in JPA User Guide
JPA Configuration Management beanhibernateorm
JPA annotation persistence class is very convenient, need jar package: ejb3-persistence.jar. I use the following three classes to describe the usage.
Sh original reprinted Please note: http://67566894.iteye.com/blog/659829javacode
- @ Suppresswarnings ("serial ")
- @ Entity
- @ Table (name = "t_x ")
- Public Class X implements serializable
- {
- @ ID
- @ Generatedvalue (Strategy = generationtype. Auto)
- Private int ID;
- @ Column (length = 32)
- Private string name;
- @ Transient // indicates that this data is not created in the database table.
- Private string temp;
- @ Temporal (temporaltype. timestamp) // This Is A type with time, minute, and second.
- Private date;
- @ Onetoone (cascade = cascadetype. All, mappedby = "X ")
- Private;
- }
- @ Suppresswarnings ("serial ")
- @ Entity
- @ Table (name = "T_A ")
- Public Class A implements serializable
- {
- @ ID
- @ Generatedvalue (Strategy = generationtype. Auto)
- Private int ID;
- @ Onetoager (cascade = cascadetype. All, mappedby = "A", fetch = fetchtype. Eager)
- Private list <B> B = new arraylist <B> ();
- @ Onetoone ()
- @ Joincolumn (name = "x_id") // after adding this sentence, both parties will jointly maintain the relationship
- Private X;
- }
- @ Suppresswarnings ("serial ")
- @ Entity
- Public Class B implements serializable {
- @ ID
- @ Generatedvalue (Strategy = generationtype. Auto)
- Protected int ID;
- @ Manytoone ()
- @ Joincolumn (name = "a_id ")
- Protected;
- }
@ Suppresswarnings ("serial") @ entity @ table (name = "t_x") Public Class X implements serializable {@ ID @ generatedvalue (Strategy = generationtype. auto) Private int ID; @ column (length = 32) Private string name; @ transient // indicates that this data is not created in the database table. Private string temp; @ temporal (temporaltype. timestamp) // This is the private date with the time, minute, and second type; @ onetoone (cascade = cascadetype. all, mappedby = "X") Private A;} @ suppresswarnings ("serial") @ entity @ table (name = "T_A ") public Class A implements serializable {@ ID @ generatedvalue (Strategy = generationtype. auto) Private int ID; @ onetomany (cascade = cascadetype. all, mappedby = "A", fetch = fetchtype. eager) Private list <B> B = new arraylist <B> (); @ onetoone () @ joincolumn (name = "x_id ") // after adding this sentence, both parties will jointly maintain the private X;} @ suppresswarnings ("serial") @ entitypublic Class B implements serializable {@ ID @ generatedvalue (Strategy = generationtype. auto) protected int ID; @ manytoone () @ joincolumn (name = "a_id") Protected ;}
Note: Fetch = fetchtype. the eager statement can only appear once in a class. If it appears twice, the error "cannotsimultaneously fetch multiple bags" will be reported. Change the other statements to fetch = fetchtype. lazy delays loading. I heard that changing the list set to set can also solve this error.
Other highlights:
1. The @ table (name = "t_x") statement can be left empty. If it is left empty, the class name is used as the table name.
2. If you want to generate a data table for the attributes of two classes, add another class in one class: @ embedded
Private C;
3. If you want a class to inherit all the attributes of another class, write in the parent class as follows:
@ Suppresswarnings ("serial ")
@ Entity
@ Mappedsuperclass // Add this line
Change the private of all attributes of the parent class to protected.
4. In one-to-multiple associations, we recommend that you use the "one" side to load with latency "multiple" side explicitly "Urgent left Outer Join" left join fetch "in hql so that hibernate can access the database less., you can also use "@ batchsize (size = 5)" to reduce the number of database accesses.
1. @ ID declares that the attribute is the primary key.
2. @ generatedvalue indicates that the primary key is automatically generated. Generally, this comment is used with @ ID.
3. @ entity any hibernte ing object must be annotated once
4. @ table (name = "tablename") Class declares the table to which the object is mapped
5. @ column (name = "name", nullable = false, length = 32) declares the correspondence between the fields of the data library and class attributes.
6. @ lob declares that the field is of the clob or blob type.
7. @ onetoetype (mappedby = "Order", cascade = cascadetype. All, fetch = fetchtype. Lazy)
@ Orderby (value = "id ASC ")
One-to-multiple statements are similar to the ORM product statement.
@ Manytoone (cascade = cascadetype. Refresh, optional = false)
@ Joincolumn (name = "order_id ")
Declared as bidirectional Association
8. @ temporal (value = temporaltype. Date) converts the date type.
9. @ onetoone (optional = true, cascade = cascadetype. All, mappedby = "person ")
One-to-one association statement
@ Onetoone (optional = false, cascade = cascadetype. Refresh)
@ Joincolumn (name = "person_id", referencedcolumnname = "personid", unique = true)
Declared as bidirectional Association
10. @ manytots (mappedby = "Students ")
Multiple-to-Multiple Association Declaration.
@ 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 ")})
Many-to-many associations generally have an association table, which is declared in this way!
11. @ transiten indicates that this attribute has no ing relationship with the table and is a temporary attribute.
12. @ cache (usage = cacheconcurrencystrategy. read_write) indicates the application cache of this object.
JPA specifications
@ Entity: declares a class as an entity bean through the @ entity Annotation
@ Table: the @ table annotation can be used to specify the table for object bean ing. The name attribute indicates the name of the table corresponding to the object. If @ table is not defined, the system automatically uses the default value: object Class Name (without package name)
@ ID: The primary key used to mark the attribute
@ Column: indicates the fields in the table mapped to the persistence attribute. If the attribute name is the same as the field name in the table, you can omit the @ column annotation. There are two other methods to mark the attribute, one is placed before the property, and the other is placed before the getter method, for example:
@ Column (name = "employee_name ")
Private string employee_name; or
@ Column (name = "employee_name ")
Public String getemployee_name (){
Return employee_name;
} Both methods are positive and can be selected based on your preferences. The elephant prefers the second type and prefers to set the attribute name to the same as the field name, which saves the @ column annotation and makes the code more concise.
@ Temporal (temporaltype. Date): If the attribute is of the time type, you must specify a specific time type because the data table has a more strict division of the time type, as shown in table 4. Three time types are defined in the javax. Persistence. temporaltype enumeration:
Use @ temporal to define the time precision mapped to the database:
@ Temporal (temporaltype. Date) Date
@ Temporal (temporaltype. Time) Time
@ Temporal (temporaltype. timestamp) both
@ Temporal only starts the ing
@ Transient
@ Target ({method, field}) @ retention (runtime)
Public @ interface transient {}
Indicates that an attribute or method cannot be persisted.
@ Tablegenerator: Table Builder, which saves the value of the current primary key to a database table separately. The value of the primary key is obtained from the specified table each time, this method of generating a primary key is very common. This method can be used to generate a primary key for any database without worrying about incompatibility between different databases. It is recommended that you manage primary keys in this way. It is very convenient to manage the primary keys of tables in a centralized manner, and changing databases will not cause great problems. The attributes are described as follows:
Name: indicates the name of the primary key generation policy of the table. This name can be customized. It is referenced in the "generator" value set in @ generatedvalue.
Table: the name of the table that is persisted by the table generation policy. Simply put, it is a table that manages the primary keys of other tables. In this example, the name of this table is generator_table.
Pkcolumnname: name of the column in the Table Builder used to store the primary key names of other tables. This column name corresponds to the fields in the table.
Pkcolumnvalue: The primary key name corresponding to the object table to the generator table. This key name can be customized.
Valuecolumnname: name of the column in the Table Builder, the next value of the primary key of the object table. If the maximum value of employee_id is 2, the value of the key name corresponding to the primary key of the object table in the builder table is 3.
Allocationsize: indicates the size of each increase in the primary key value. For example, if it is set to 1, it indicates that 1 is automatically added after each new record is created. The default value is 50.
@ Target ({method, field}) @ retention (runtime)
Public @ interface onetoworkflow {
String targetentity () Default "";
Cascadetype [] cascade () default {};
Fetchtype fetch () default lazy;
String mappedby () Default "";
}
String targetentity (optional) the fully qualified class name
The entity class that is the target of the association.
Optional only if the collection Property
Is defined using Java generics. Must be
Specified otherwise.
The parameter type of
Collection when defined
Using Generics (generics generic). If generic is used, the targetentity can be omitted.
Cascadetype [] cascade (optional) The operations that shoshould be cascaded
To the target of the Association
No operations are cascaded
Fetchtype fetch (optional) whether the association shocould be
Lazy loaded or eagerly fetched.
Fetchtype. Lazy defaults to delayed loading.
String mappedby (optional) the field that owns the relationship
Attribute name associated with multiple parties
Mappedby specifies the end Of the link that does not need to be maintained ??
@ Target ({method, field}) @ retention (runtime)
Public @ interface manytoone {
String targetentity () Default "";
Cascadetype [] cascade () default {};
Fetchtype fetch () default eager;
Boolean optional () default true;
}
Targetentity (optional) the fully qualified class name of theentity class that is the target of the associationthe type of theproperty that stores the Association
It refers to the class of the object type of one party. The default type is the type of this attribute. In fact, some of them can be omitted.
Cascadetype [] cascade (optional) The operations that shoshould be cascaded
To the target of the association no operations are cascaded
Fetchtype fetch (optional) whether the association shocould be
Lazy loaded or eagerly fetched. fetchtype. Eager is automatically loaded by default.
Boolean optional (optional) whether the association is optional.
If set to false then a non-null relationship must always exist.
True
@ Target ({method, field}) @ retention (runtime)
Public @ interface onetoone {
String targetentity () Default "";
Cascadetype [] cascade () default {};
Fetchtype fetch () default eager;
Boolean optional () default true;
String mappedby () Default "";
Boolean usepkasfk () default false;
}
The joincolumn annotation is used to specify a mapped column for joining an entity association or
Secondary table.
@ Target ({type, method, field}) @ retention (runtime)
Public @ interface joincolumn {
String name () Default "";
String referencedcolumnname () Default "";
Boolean primarykey () default false;
Boolean unique () default false;
Boolean nullable () default true;
Boolean insertable () default true;
Boolean updatable () default true;
String columndefinition () Default "";
String secondarytable () Default "";
}
@ Target ({method, field}) @ retention (runtime)
Public @ interface manytow {
String targetentity () Default "";
Cascadetype [] cascade () default {};
Fetchtype fetch () default lazy;
String mappedby () Default "";
}