JPA (2)

Source: Internet
Author: User

The previous study of the JPA HelloWorld, but also the initial use of some annotations, then carefully understand what the annotations, and the role of these annotations

Basic Notes for JPA:

① @Entity, @Table, @Id, @GeneratedValue, @Column, @Basic, these are the most basic annotations.
One: @Entity: The callout is used before the entity class declaration statement, stating that the Java class is an entity class and will map to the specified database table. If you declare an entity class user, it is mapped to the user table in the database. If the @table annotation is not used, then what is the name of Vo, then the name of the corresponding data table is also the name of Vo.
Two: @Table: When an entity class has a different name than its mapped database table name, it needs to use @Table callout description, which is used in parallel with the @Entity label, before the entity class declaration statement, can be written on a separate statement line, or it can be accompanied by a declaration statement. The common option for @Table labeling is name, which indicates the table name of the database @Table label there is also two options catalog and schema are used to set the database directory or schema to which the table belongs, typically the database name. The uniqueconstraints option is used to set constraints, usually without setting.
Three: @Id label the attribute that declares an entity class is mapped to the primary key column of the database. This property is typically placed before a property declaration statement and can be used with a declaration statement or on a separate line. @Id labels can also be placed before the getter method of the property.

Four: @GeneratedValue the generation policy used to label the primary key, specified by the Strategy property. By default, JPA automatically chooses a primary key generation strategy that best fits the underlying database: SQL Server corresponds to Identity,mysql for auto increment. The following options are defined in Javax.persistence.GenerationType: IDENTITY: The way in which the database ID grows from the primary key field, which is not supported by Oracle; AUTO:JPA automatically chooses the appropriate policy is the default option; SEQUENCE: Generates a primary key through a sequence, specifying the sequence name by @SequenceGenerator annotations, MYSQL does not support this way table: The primary key is generated by the table, and the framework generates the primary key by the table simulation sequence. Use this policy to make your app easier to migrate to your database.
Five: @Basic represents a simple property to the database table of the mapping of the field, for the Getxxxx () method without any annotations, the default is @basic Fetch: Represents the property of the read policy, there are EAGER and lazy two, respectively, the main support crawl and delay loading, the default is the EA GER. Optional: Indicates whether the property is allowed to be null, which is true by default

@Transient:

Indicates that the property is not a mapping to a field in a database table, which the ORM framework ignores. If a property is not a field mapping for a database table, it must be marked as @transient, otherwise the ORM framework defaults to its annotation as @Basic

@Temporal

The precision of Date types is not defined in the core Java API (temporal precision). In the database, the data that represents the date type has date, time, and TIMESTAMP three precision (that is, simple dates, times, or both). You can use @temporal annotations to adjust the precision when you make attribute mappings.

@Table This note is a lot more.
The meaning of this annotation is that the primary key generation strategy is placed in a data table, and each time it is used, it is queried in the data table, and the primary key is generated according to the policy set by itself.
I created it in the MySQL database:

  

, the data table, Pk_name is the table which we use the strategy, Id_val store is the primary key, each time we create the record, the primary key is to use Id_val as a reference. Here's the record I've stored,

My current annotations are:

 Packagecom.hotusm.commom.entity;Importjava.io.Serializable;ImportJavax.persistence.Column;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;ImportJavax.persistence.GenerationType;Importjavax.persistence.Id;Importjavax.persistence.Table;Importjavax.persistence.TableGenerator; @Table (name= "USER") @Entity Public classUserImplementsserializable{/**     *      */    Private Static Final LongSerialversionuid = 1L; PrivateInteger ID; PrivateString name; PrivateString Userdesc; @TableGenerator (Name= "Generators",//the name needs to be the same as the generator value in @generatedvalueTable= "Id_generator",//table name corresponding to the primary key policy data table we createdPkcolumnname= "Pk_name",//The field that corresponds to the creation of the primary key policy data table that holds the information, together with the back pkcolumnvalue, determines the rowPkcolumnvalue= "Id_user",//This field is the value of Pk_nameValuecolumnname= "Id_val",//A row can be determined by Pkcolumnname,pkcolumnvalue, which determines which field it is.Allocationsize=1,//each time the primary key grows size id_val is just starting to set up, then it doesn't workInitialvalue=1//The size of the initialization, if the data table) @GeneratedValue (Strategy=generationtype.table,generator= "Generators") @Id PublicInteger getId () {returnID; }         Public voidsetId (Integer id) { This. ID =ID; }         PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; } @Column (Name= "User_desc")     PublicString GetDesc () {returnUserdesc; }     Public voidSetdesc (String desc) { This. Userdesc =desc; }    }

Thus, each time we create a table, the primary key can grow according to the policies we set up, but this is not very common, unless it is a specific business requirement.

JPA (2)

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.