Onetoone and Onetomany usage in Spring JPA

Source: Internet
Author: User

In spring Engineering, when you create an entity object, you can identify the database field @Column by using the JPA @entity identity entity and the database table's corresponding relationship. There are annotations that identify the relationship between two entities: @OneToOne, @OneToMany, @ManyToOne, and @manytomany, each identifying a pair of one or one-to-many, many-to-one, and many-to-many. Here, simply record the use of @onetoone and @onetomany. It also involves annotation @joincolumn, whose decorated field is the Relationship maintenance field.

@OneToOne

@OneToOne used in conjunction with @joincolumn, indicates that a foreign key is created in the source entity, the entity class that declares the annotation, and as an example, book and Bookdetail.

Book class, the Book_detail foreign key is created in the Book table:

@Entity @table (name= "book") @DynamicInsert (true) Public classBookImplementsserializable{Private Static Final LongSerialversionuid = 1L; @Id @Column (Name= "id") @GeneratedValue (Strategy=generationtype.identity)Private LongID; @Column (Name= "Name")    PrivateString name; @OneToOne (Cascade=cascadetype.all) @JoinColumn (name= "Bookdetail")    PrivateBookdetail Bookdetail;  PublicBookdetail Getbookdetail () {returnBookdetail; }     Public voidSetbookdetail (Bookdetail bookdetail) { This. Bookdetail =Bookdetail; }     Public LonggetId () {returnID; }     Public voidSetId (LongID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }    }

Bookdetail class, Mappedby represents a foreign key maintained by the bookdetail of the book Class:

@Entity @table (name= "Bookdetail") @DynamicInsert (true) Public classBookdetailImplementsserializable{Private Static Final LongSerialversionuid = 1L; /*** PRIMARY Key*/@Id @Column (name= "id") @GeneratedValue (Strategy=generationtype.identity)Private LongID; @Column (Name= "Numofpages")    Private intnumofpages; @OneToOne (Cascade= Cascadetype.all, Mappedby = "Bookdetail")    PrivateBook Book ;  PublicBook GetBook () {returnBook ; }     Public voidSetbook (book book) { This. Book =Book ; }     Public LonggetId () {returnID; }     Public voidSetId (LongID) { This. ID =ID; }     Public intgetnumofpages () {returnnumofpages; }     Public voidSetnumofpages (intnumofpages) {         This. Numofpages =numofpages; }}

Book table:

Bookdetail table:

When you save the book object, the Bookdetail object is saved, and the book object foreign key is set and saved. That is , save the targetentity first, set the foreign key, and then save the sourceentity.

@OneToMany

@OneToMany in conjunction with @joincolumn, a foreign key is created in the target entity (Targetentity the class specified). Save the sourceentity, set the foreign key, and then save the targetentitywhen the report is saved. The code below is a one-to-many scenario:

@OneToMany (targetentity = Subvo.  Class, cascade=cascadetype.all)    @JoinColumn (name= "Conditionid")    Private New Arraylist<> ();

For an introduction to other cascading and cascading types, you can refer to the JPA Concept resolution: Cascadetype (various cascading operations).

It is important to note that:

(1) Many-to-many situations, can be used without such annotations, you can maintain a cascade relationship through the intermediate table (such as a and B table many-to-many, C table contains only 2 columns A and B of the primary key mappings, maintain a many-to-many relationship), the operation may be more convenient.

(2) This cascade is carefully used, in the query and other operations, check a entity, also will cascade B or C entity also find out, but the business only need a information, so may increase the database burden.

Reference: HTTPS://WWW.JIANSHU.COM/P/8AE19B367A1B

Onetoone and Onetomany usage in Spring JPA

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.