The experience of using hibernate annotations to maintain a many-to-many relationship

Source: Internet
Author: User

Description

In Hibernateannotations, many-to-many associations can be defined by @manytomany annotations. At the same time, it is also necessary to describe the correlation table and the associated conditions through annotation @jointable. For bidirectional associations, one end must be defined as owner, and the other end must be defined as inverse (this end will be ignored when the associated table is more sexually manipulated). The associated end does not have to describe the physical mapping, only a simple mappedby parameter, which contains the property name of the principal end, thus binding the relationship between the two parties.

How to make a PO

1 Find out what classes cube--need to introduce:

import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

2) Find the car person--the main end:

/** *//**
* Theater
* @author Allen
*/
@SuppressWarnings ("serial")
@Entity
@Table (name = "Theater")
@Cache (usage = cacheconcurrencystrategy.read_write)
public class Theater implements Serializable { br> @ManyToMany (
Targetentity=net.allen.domain.audience.class,
Cascade ={CASCADETYPE.PERSIST,CASC        Adetype.merge},
Fetch=fetchtype.lazy
)
@JoinTable (
name= "Theater_audience",
joincolumns={@JoinColumn (name= "theater_id")},
inversejoincolumns={@JoinColumn (name= "audience_id")}
)
@Cache (usage = cacheconcurrencystrategy.nonstrict_read_write)
Private list<audience> audiences = new ARR Aylist<audience> ();   
/** *//**
* @return Returns the audiences.
   */
Public list<audience> getaudiences () {
return audiences;
  
/** *//**
* @param audiences the audiences to set.
*/
Public VOID setaudiences (list<audience> audiences) {
this.audiences = audiences;
}
}

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.