Spring Data Jpa (five) inside the common annotation Detailed examples @Entity

Details javax.persistence following Entity commonly used comment.

  Although the Spring Data JPA has helped us to operate on a data package well, the convention is larger than the configuration of thought, a lot of things to help us by default. JPA (Java Persistence API) is an entity associated source storage business entities. It shows how to define a common object-oriented Java (POJO) as an entity, as well as how to provide a set of standards and management of the relationship between entities. Therefore, some javax.persistence following notes must still need to understand in order to better improve work efficiency.

 

1. Basic Notes

  基本注解包括@Entity、@Table、@Id、@IdClass、@GeneratedValue、@Basic、@Transient、@Column、@Temporal、@Enumerated、@Lob

 

1.1@Entity

  @Entity defined objects will be managed by JPA entity, will be mapped to the specified database tables.

1.2@Table

  @Table specify the name of the database table.

1.3@Id

  @Id is defined as the primary key attribute database, which must have a an entity.

1.4@IdClass

  @IdClass using the primary key of the outer class.

  As in line with the primary key class, to meet the following requirements.  

  We must implement the Serializable interface.
  The public must have a default constructor with no arguments.

  You must override equals and hashCode methods. equals method for determining whether two objects are the same, when EntityManger by Entity find to find the equals method to determine the value of the return. In the present embodiment, only the name of the object and returns the value true email identical or when the same object, otherwise false. hashCode method returns the current object's hash code, the probability of generating the same hashCode small as possible, the algorithm can be optimized

1.5 @GeneratedValue

  @GeneratedValue primary key generation strategy

  GenerationType a total of the following four values:

  

1.6 @Basic

  @Basic represents attribute is mapped to a field in a database table. If the field is an entity does not have any comment, the default is the @Basic.

1.7 @Transient

  @Transient indicates that the property is not mapped to a database table field, it represents a non-persistent attributes, and @Basic opposite effect. JPA mapping database when to ignore it.

1.8 @Column

  @Column column name corresponding to the attribute is defined in the database

1.9@Temporal

  Date @Temporal used to set the type attribute field mapped to a corresponding accuracy.

  (1) @Temporal (TemporalType.DATE) mapped to date ∥date (only date)

  (2) @Temporal (TemporalType.TIME) mapped to date ∥time (only time).

  (3) @Temporal (TemporalType.TIMESTAMP) mapped to date ∥datetime (date + time).

1.10@Enumerated

  Direct mapping of enumeration type enum field

  

1.11@Lob

  @Lob the properties are mapped to database-supported large object type, supports the following two several database types of fields.

  (1) Clob (Character Large Ojects ) type long string type, java.sql.Clob, Character [], char [], and is mapped to Clob String class type.

  (2) Blob (Binary Large Objects ) type is byte type, the java.sql.Blob, Byte [], byte [] types and implements Serializable interface is mapped to Blob type.

  (3) Clob, Blob larger memory space occupied, generally with @Basic (fetch = FetchType.LAZY) be set to delay loading.

 

2. The association notes

  JoinColumn relationship annotations include @, @ OneToOne, OneToMany @, @ ManyToOne, @ ManyToMany, JoinTable @, @ the OrderBy.

 

2.1  @JoinColumn define the foreign key field name

  Usage: @JoinColumn mainly with @ OneToOne, @ ManyToOne, used together @OneToMany, use alone does not make sense.

  @JoinColumns defined relationships of multiple fields.

2.2 @OneToOne relationship

  Usage: @OneToOne need to meet @JoinColumn used together. Note: can two-way association, you can configure only one party, you need depending on the actual demand.

 

Guess you like

Origin www.cnblogs.com/youqc/p/11100967.html