Spring Data JPA Series (ii)--------entity detailed _spring

Source: Internet
Author: User

The Spring Data JPA is developed based on Hibernate, so building entity and mapping relationships requires a good understanding:------Entity Manager Entity Manager Entitymanager is the entity's Bridge to the database (and the transaction works together),   Equivalent to the sqlsession in the Session,mybatis in Hibenrtae. Entity Total 4 states: (Persist for insertion, merge for modification or insertion, remove for deletion)

New State (A): When a new entity is created, the entity is in this state, and the persist method is executed to enter the managed state

Managed state (B): Managed state, where an entity is in this state indicates that the entity has been Entitymanager managed

Delete status (C): slightly

Free State (D): The difference between a free State and a is that the ID of D is a value


b states are managed States, objects in the B state, and their actions are automatically submitted to the database as things are committed, and the object of B becomes the D state when the transaction commits, and the difference between D and a is that the ID of the object in D is a value. When the Entitymanager entity manager executes the Persist,rollback,refresh,merge method, the current object becomes a B state.

------Entity Base Mappings

@Entity means that this class is an entity class

@Table Specify the table name of this entity's corresponding database

@column (name= "name", Length=60,nullable=false, Unique=false,insertable=false,

columndefinition = corresponding database field type)

private String name;

@column (name= "big", precision = a, scale = 2)

private BigDecimal big;


Insertable,updateable indicates whether to allow inserts or updates,

Precision represents the precision, when the property is double or bigdecimal, he represents the total length of the number,

Scale represents the number of digits in a decimal.


Primary key:

@ID

@GenerateValue PRIMARY Key Policy

----------AUTO is the same as the default

----------IDENTITY Automatic growth

----------SEQUENCE sequence, Oracle uses


------Entity Advanced Mapping

1, one-on-one

Persion | Address

address_id |

In the Persion entity:

@oneToOne

@JoinColumn (name = "address_id", Refrececolumnname = "aid")

private address address;

Name represents the associated field in this table, and Refrececolumnname represents the associated field name on the other end

2, a one-to-many (and Onetoone no difference, a pair of more will produce a middle table)

3, more to one (and Onetoone no difference, but also those two attributes)

4, many to many

Student |   Middle Table Student_teacher | Teacher

In the teacher entity:

@ManyToMany

@JoinTable (name = "Teacher_student", Joincolumns = @JoinColumn (name = "teacher_id", Refrececolumnname = "Tid"), Inversejoincolumns = @JoinColumn (name = "student_id", refrececolumnname= "Sid"))

private list<student > students;

@JoinTable represents association to an intermediate table

Name is the middle table names

Joincolumns represents the association between the configuration relationship owner and the intermediate table

Inversejoincolumns represents the association between the owner and the intermediate table of the configuration relationship

(The owner and the owner means, for example, a one-to-many relationship, generating a middle table, departmental depart and employee employees, configuring @onetomany in a departmental entity, the owner of the relationship is depart, and the relationship is owned by the employee)

Cascade:

For example

@OneToOne (cascade = {Cascadetype.all},fetch = fetchtype.lazy)

Cascade is cascading operations, such as additions and deletions, and fetch is a specialized management cascade query

Cascadetype have the following types:

Deletes the current object-----Remove, and the associated objects are deleted together

-----All operations will be associated with deletes

-----DETACH

-----MERGE

-----PERSIST

-----REFRESH

There are two types of fetchtype

-----LAZY Lazy Load

-----EAGER Urgent Load

Here it is, the next one to talk about JPQL, blog writing is not good, there is any problem directly home QQ ask me, or leave a message. Class....


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.