The realization of hibernate annotation-based bidirectional one-to-many mapping relation

Source: Internet
Author: User

A one-to-many entity-class relationship mapping was used in the project, which was previously exposed to the configuration file-based mapping implementation. But most of the company is based on annotations. So the code of the previous reference is the implementation of a one-to-many mapping relationship based on annotations.


Background:

One end: Qingaocenterinfo: Green Olympic venues information.

Many of the end: Qingaoplaceinfo: Green Olympic venues information,

One of the green Olympic venues can include a number of green Olympic stadiums


One end: Qingaocenterinfo, which holds the Qingaoplaceinfo list reference,

Through annotations @OneToMany (mappedby= "Qingaocenterinfo", cascade= Cascadetype.all)


mappedby: defines a bidirectional relationship between classes. Assuming that the class is a one-way relationship, there is no need to provide a definition, assuming that the class and class form a bidirectional relationship, we need to use this attribute to define, Otherwise it may cause data consistency problem .

To be directed by one party to the many, and this attribute should be equal to the property name of the property of the many party , otherwise it will be wrong.

Cascade:cascadetype[] type. This property defines a cascade relationship between classes and classes.

The defined cascading relationships are considered by the container to take the same action on the current class object and its associated class object. and such relationships are called recursively.

For example: Order and OrderItem have cascading relationships, then deleting qingaocenterinfo will delete the corresponding Qingaoplaceinfo object at the same time.

Assuming that Qingaoplaceinfo also has a cascade relationship with other objects, the operation will continue to run recursively.

the value of cascade can only be from cascadetype.persist (Cascade New), Select one or more cascadetype.remove (cascade Delete), Cascadetype.refresh (cascading refresh), Cascadetype.merge (cascade Update). Another option is to use Cascadetype.all, which means that all four items are selected.

Package Com.yuqiaotech.nttelcom.model;import Java.util.date;import Java.util.list;import Javax.persistence.cascadetype;import Javax.persistence.entity;import Javax.persistence.generatedvalue;import Javax.persistence.generationtype;import Javax.persistence.id;import Javax.persistence.onetomany;import javax.persistence.table;/** * Green Olympic key places information table. * */@Entity (name= "Qing_ao_center_info") @Table (name= "Qing_ao_center_info") public class Qingaocenterinfo {private Long id;private string centername;//key place name private Long alarmnum;//Alarm number private string note;//memo private String iconname;// Icon Name private string cityname;//City private string type;//key place, activity guarantee private Date createtime;private list< Qingaoplaceinfo> Qingaoplaceinfo; Venue-owned Venues @id@generatedvalue (Strategy=generationtype.auto) public Long getId () {return Id;} public void SetId (Long id) {this.id = ID;} /** * @searchItem * displaytype= "text" * * Key place name * @return */public String getcentername () {return centername;} public void Setcentername (String centernAME) {this.centername = Centername;} /** * Alarm number * @return */public Long getalarmnum () {return alarmnum;} public void Setalarmnum (Long alarmnum) {this.alarmnum = Alarmnum;} /** * remarks * @return */public String getnote () {return note;} public void Setnote (String note) {this.note = Note;} /** * Icon Name * @return */public String geticonname () {return iconname;} public void Seticonname (String iconname) {this.iconname = Iconname;} Public String Getcityname () {return cityname;} public void Setcityname (String cityname) {this.cityname = CityName;} Public String GetType () {return type;} public void SetType (String type) {this.type = type;} Public Date Getcreatetime () {return createtime;} public void Setcreatetime (Date createtime) {this.createtime = Createtime;} @OneToMany (mappedby= "Qingaocenterinfo", cascade= cascadetype.all) public list<qingaoplaceinfo> Getqingaoplaceinfo () {return qingaoplaceinfo;} public void Setqingaoplaceinfo (list<qingaoplaceinfo> qingaoplaceinfo) {this.qingaoplaceinfo = QingAoPlaceInfo;}} 


Many end: Qingaoplaceinfo. Hold a reference to Qingaocenterinfo

Setting Association relationships by @manytoone (Fetch=fetchtype.lazy) @JoinColumn (name= "f_center_id")

@ManyToOne indicate a many-to-one relationship between Qingaoplaceinfo and Qingaocenterinfo, multiple Qingaoplaceinfo instances are associated with the same Qingaocenterinfo object

Fetch and lazy are the ways to define cascading queries:

Fetch: In the official documentation for fetch there are, for example, the following descriptive narratives, Hibernate3 defines for example the following crawl strategies:

  • connection fetching (join fetching): Hibernate SELECT OUTER JOIN obtains an associated instance or associated collection of an object by using the statement (outer join).

  • query Fetch (Select fetching): Also sends a SELECT statement to fetch the associated entity or collection of the current object. Unless you explicitly specify lazy="false" to prohibit lazy fetching (lazy fetching), the second SELECT statement is only run when you actually access the correlation relationship.

  • subquery Fetch (subselect fetching): Also sends a SELECT statement to fetch the associated collection of all the entity objects that were previously queried (or crawled to). Unless you explicitly specify lazy="false" to prohibit lazy fetching (fetching), it's only when you actually visit the correlation. Before the second SELECT statement is run.

  • Batch crawl (batch fetching): An optimization scheme for query fetching by specifying a primary key or foreign key list. Hibernate uses a single SELECT statement to get a batch of object instances or collections.


Package Com.yuqiaotech.nttelcom.model;import Java.util.date;import Java.util.list;import Javax.persistence.cascadetype;import Javax.persistence.entity;import Javax.persistence.fetchtype;import Javax.persistence.generatedvalue;import Javax.persistence.generationtype;import Javax.persistence.Id;import Javax.persistence.joincolumn;import Javax.persistence.manytoone;import Javax.persistence.onetomany;import javax.persistence.table;/** * Venue information.

* */@Entity (name= "Qing_ao_place_info") @Table (name= "Qing_ao_place_info") public class Qingaoplaceinfo {private Long ID ;p rivate qingaocenterinfo qingaocenterinfo;//key Place idprivate string placename;//venue name private string note;//notes private String openstat;//open State private Long displayorder;private String cityname;private Date createtime;private list< Qingaoplacecdmasector> qingaoplacecdmasector;//owns the cdmaprivate list<qingaoplaceltesector> qingaoplaceltesector;//owned by Lteprivate List<qingaoap> qingaoap;//owned ap@id@generatedvalue (strategy = Generationtype.auto) public Long getId () {return ID;} public void SetId (Long id) {this.id = ID;} @ManyToOne (Fetch=fetchtype.lazy) @JoinColumn (name= "f_center_id") public qingaocenterinfo Getqingaocenterinfo () { return qingaocenterinfo;} public void Setqingaocenterinfo (Qingaocenterinfo qingaocenterinfo) {this.qingaocenterinfo = Qingaocenterinfo;} /** * @searchItem * displaytype= "text" * Place name * @return */public String getplacename () {return placename;} PublIC void Setplacename (String placename) {this.placename = placename;} Public String Getnote () {return note;} public void Setnote (String note) {this.note = Note;} Public String Getopenstat () {return openstat;} public void Setopenstat (String openstat) {this.openstat = Openstat;} Public Long Getdisplayorder () {return displayorder;} public void Setdisplayorder (Long displayorder) {this.displayorder = Displayorder;} Public String Getcityname () {return cityname;} public void Setcityname (String cityname) {this.cityname = CityName;} Public Date Getcreatetime () {return createtime;} public void Setcreatetime (Date createtime) {this.createtime = Createtime;} @OneToMany (mappedby= "Qingaoplaceinfo", cascade= cascadetype.all) public list<qingaoplacecdmasector> Getqingaoplacecdmasector () {return qingaoplacecdmasector;} public void Setqingaoplacecdmasector (list<qingaoplacecdmasector> qingaoplacecdmasector) { This.qingaoplacecdmasector = Qingaoplacecdmasector;} @OneToMany (mappedby= "Qingaoplaceinfo", cascade= CaScadetype.all) public list<qingaoplaceltesector> Getqingaoplaceltesector () {return qingaoplaceltesector;} public void Setqingaoplaceltesector (list<qingaoplaceltesector> qingaoplaceltesector) { This.qingaoplaceltesector = Qingaoplaceltesector;} @OneToMany (mappedby= "Qingaoplaceinfo", cascade= cascadetype.all) public list<qingaoap> Getqingaoap () {return Qingaoap;} public void Setqingaoap (list<qingaoap> qingaoap) {this.qingaoap = Qingaoap;}}





The realization of hibernate annotation-based bidirectional one-to-many mapping relation

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.