First, the JPA generic policy generator
With annotation to map hibernate entities, the annotation-based Hibernate primary key is identified as @id,
The generation rules are set by @generatedvalue. The @id and @generatedvalue here are the standard uses of JPA,
JPA provides four standard usages, which can be clearly seen by the @generatedvalue source code.
Java c
First, the JPA generic policy generatorJPA provides four standard usage by @generatedvalue source code:
@Target ({Method,field})
@Retention (RUNTIME) public
@interface generatedvalue{
generationtype Strategy () default AUTO;
String generator () Default "";
}
Among them GenerationType:
public enum generationtype{
TABLE,
SEQUENCE,
Tags: ted use _id value gen ATI Save key lastMyBatis getting the self-increment primary key when inserting Method has two Method 1: Insert into person (NAME,PSWD) VALUES (#{name},#{pswd}) Method 2: Select LAST_INSERT_ID ()Insert into person (NAME,PSWD) VALUES (#{name},#{pswd}) The entity id attribute before insertion is 0; The entity id attribute after insertion is the self-increment ID after saving; @generatedvalueMyBatis gets the self-increment primary key when inserting (1: Write SQL. 2:
true)* ColumnDefinition Optional, overwriting the SQL DDL fragment for this specific column (this may result in a failure to migrate between different databases)* Table optional, define the corresponding tables (default is the primary table)* Length optional, column lengths (default value 255)* Precision optional, column decimal precision (decimal precision) (default value 0)* Scale is optional, if the column decimal value range is available, this setting (default value 0)7 * @
hibernate bidirectional onetoone fetchtype lazy problemCategory: Hibernate2013-06-26 14:54 932 people read comments (2) favorite reports Reproduced in: http://mshijie.javaeye.com/admin/blogs/440057When you use JPA (Hibernate) today to implement a one-to-one association, you find that lazy loading cannot be used. The person is associated with a picture. When the person is read, the picture is displayed for the picture. Read 10 Person A database query occurred 11 times.Finally, after reviewing
columns of the mapping table
@Transient Defining Transient Properties
2.1 Notes related to the primary key
@Id-a property that is mapped to the primary key of a database table must be defined, and an entity can have only one attribute mapped to the primary key, before Getxxxx ().
@GeneratedValue (strategy=generationtype,generator= "")-optional, which defines the primary key generation strategy.
Property:
Strategy-Represents the primary key generatio
@Table (name= "users") public
class users {
@Id
@GeneratedValue (strategy= generationtype.identity)
@Column (name= "user_id")
private int id;
@Column (name= "user_name", length= ")
private String name;
....
}
2) @Transient modify non-persisted propertiesYou can use @transient to decorate properties that do not participate in persisted mappings, and as additional information properties for t
VARCHAR2 ()); ALTER TABLE TEST2 add constraint pk_ TEST2_ID primary KEY (ID);Two tables were created, TEST, TEST2. Table structure is exactly the same, simple primary key ID, name.Create a TriggerCREATE OR REPLACE TRIGGER "Tg_test" before INSERT on TEST for each ROW if (New.id is null) Beginselect testsequence. Nextval into:new.id from Dual;end;/alter TRIGGER "Tg_test" ENABLE; CREATE OR REPLACE TRIGGER "Tg_test2" before INSERT on TEST2 for each ROW if (New.id is null) Beginselect Testsequenc
, equivalent to the primary key of the data table ).
@ Generatedvalue -- Defines the automatic growth primary key generation policy.
@ Transient -- These fields and attributes are ignored and do not need to be persisted to the database. yes. In the current persistent class, some attributes are not used to map to a data table, but to other business logic needs. In this case, these attributes must be annotated with transient. otherwise, the system w
This article can be used as a learning note for the Beijing Academy Horse Soldier Hibernate course.Hibernate mapping, mainly divided into one-to-many, a lot of a, many-to-many, but also one-way and two-way difference.OK, don't dwell on the name, we begin to look at the example.One-way husband is an entity, wife is also an entity.A husband has only one wife, while a wife also has only one husband.The relationship above is called a one-to-one.What do you mean one way?Look at the code:Package com.b
and @embeddable@AttributeOverrides Property OverridesThe contents of the @AttributeOverride property override are nested together with the @attributeoverrides@SecondaryTables Multiple table mappings@SecondaryTable define auxiliary table mappings and @secondarytables nested together with@GeneratedValue identifier generation policy, the default autoTable-to-table relationship mappings@OneToOne: One-to-one mapping. It consists of five properties:Targete
= "PersonId", referencedcolumnname= "PersonId", Unique=true)
Specific as follows:
@Entity
@Table (name = "Tb_person") public
class person implements Serializable {
private static final long SE Rialversionuid = 576770011136181361L;
@Id
@GeneratedValue (strategy = generationtype.auto)
private Long personId;
@OneToOne (fetch=fetchtype.lazy,targetentity=idcards.class,mappedby= "person", Cascade=cascadetype.all)
Private Idcard Idcard;
@Entity
Annotation-based hibernate primary key setting: @ ID.So what are its generation rules? It is specified by @ generatedvalue.
Let's take a look at how it is defined: Java
Code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://writeblog.csdn.net/# ">
@ Target({Method, field })
@ Retention(Runtime)
Public@ InterfaceGeneratedvalue {
Generationtype strategy ()DefaultAuto;
String generator ()Default"";
}
@ Targ
1. In the previous section, we can see in the database that the created tables and fields are created through Entity bean. What are the rules for creating table names and field names? The code is as follows: Package learn. jpa. bean; import javax. persistence. entity; import javax. persistence. generatedValue; import javax. persistence. id; @ Entitypublic class Person {@ Id @ GeneratedValue private Integer
Hibernate supports a total of 13 build policies by default:
1. Increment
2. Identity
3. Sequence
4. Hilo
5. Seqhilo
6. UUID
7. Uuid.hex
8. Guid
9. Native
Ten. Assigned
One. Select
Foreign.
Sequence-identity.
Here are a few of the more co
The hibernate annotation (annoation) uses the Id primary key to generate the main use of the following annotations: @Id, @GeneratedValue, @GenericGenerator. , the @GeneratedValue is the JPA default implementation custom primary key generation strategy, @GenericGenerator is hibernate based on the JPA enhancements.
Custom primary key generation policy, implemented by @genericgenerator. Hibernate is extended
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.