Java Persistence/manytomany

Source: Internet
Author: User

A ManyToMany relationship in Java are where the source object has an attribute that stores a collection of target objects and (i f) Those target objects had the inverse relationship back to the source object it would also is a ManyToMany relationship. All relationships in Java and JPA was unidirectional, in so if a source object references a target object there is no Gu Arantee that the target object also have a relationship to the source object. This was different than a relational database, in which relationships be defined through foreign keys and querying such th At the inverse query always exists.

JPA also defines a OneToMany relationship, which is similar to a ManyToMany relationship except that the inverse relationship (if I T were defined) is a ManyToOne relationship. The main difference between a and a relationship in JPA are that a all makes use of OneToMany ManyToMany ManyToMany a intermediate rela tional join table to store the relationship, where as a OneToMany can either with a join table, or a foreign key in target Obje CT ' s table referencing the Source object table ' s primary key.

In JPA a ManyToMany relationship is defined through the @ManyToMany annotation or the <many-to-many> element.

All ManyToMany relationships require a JoinTable . The IS JoinTable defined using the @JoinTable annotation and <join-table> XML element. JoinTablethe defines a foreign key to the source object ' s primary key ( joinColumns ), and a foreign key to the target object ' s Prima Ry Key ( inverseJoinColumns ). Normally the primary key of the is the JoinTable combination of both foreign keys.

Example of a manytomany relationship database[edit]

EMPLOYEE (table)

Id FIRSTNAME LASTNAME
1 Bob The
2 Sarah Smith

Emp_proj (table)

emp_id proj_id
1 1
1 2
2 1

PROJECT (table)

Id NAME
1 Gis
2 Sig
Example of a manytomany relationship annotation[edit]
@EntityPublicClassEmployee{@Id@Column(Name="ID")PrivateLongId;...@ManyToMany@JoinTable(Name="Emp_proj"joincolumns= @JoinColumn  span class= "n" >name= "emp_id"  Referencedcolumnname= "ID" ), inversejoincolumns< Span class= "o" >= @JoinColumn  (name=  "proj_id" referencedcolumnname=  "ID" ) private list<project> span class= "n" >projects              /span>                
Example of a manytomany relationship XML[edit]
<entityName="Employee"class="Org.acme.Employee"access="FIELD"><attributes><idName="id"><columnName="Emp_id"/></id><set name= "projects" table= "emp_proj" lazy= "true"  Cascade= "none" sort= "natural" optimistic-lock=  "false" ><key column= " emp_id "not-null=" true "/><many-to-many class= "Com.flipswap.domain.Project" column= " PROJ_ID "/></set></attributes> </ENTITY>              

Https://en.wikibooks.org/wiki/Java_Persistence/ManyToMany

Java Persistence/manytomany

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.