Hibernate----1-n--jointable (person and address)

Source: Internet
Author: User

 PackageCom.ij34.dao;ImportJava.util.HashMap;ImportJava.util.HashSet;ImportJava.util.Set;ImportJavax.persistence.*; @Entity @table (name= "People_inf") Public classPeopleImplementsjava.io.serializable{Private Static Final LongSerialversionuid = 1L; @Id @Column (Name= "people_id") @GeneratedValue (Strategy=generationtype.identity)PrivateInteger ID; PrivateString name; Private intAge ; @OneToMany (targetentity=address.class)//compared to the other, there is no mappedby= "people"@JoinTable (name= "People_address", Joincolumns[Email protected] (name= "Peopleid", referencedcolumnname= "people_id"), Inversejoincolumns[Email protected] (name= "Addressid", referencedcolumnname= "address_id", unique=true)    )    PrivateSet<address> address=NewHashset<>();  PublicInteger getId () {returnID; }     Public voidsetId (Integer id) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicSet<address>getaddress () {returnaddress; }     Public voidSetaddress (set<address>address) {         This. Address =address; }    }

 PackageCom.ij34.dao;ImportJavax.persistence.*; @Entity @table (name= "Address_inf") Public classaddress{@Id @Column (name= "address_id") @GeneratedValue (Strategy=generationtype.identity)Private intAddressid; PrivateString message; @ManyToOne (targetentity=people.class) @JoinTable (name= "People_address", Joincolumns[Email protected] (name= "Addressid", referencedcolumnname= "address_id", unique=true), Inversejoincolumns[Email protected] (name= "Peopleid", referencedcolumnname= "people_id" )    )    Privatepeople people;  Public intGetaddressid () {returnAddressid; }     Public voidSetaddressid (intAddressid) {         This. Addressid =Addressid; }     PublicString getMessage () {returnmessage; }     Public voidsetmessage (String message) { This. Message =message; }     PublicPeople GetPeople () {returnpeople; }     Public voidSetpeople (People people) { This. People =people; }    }

 PackageCom.ij34.web; Importorg.hibernate.Session; Importorg.hibernate.SessionFactory; Importorg.hibernate.Transaction; ImportOrg.hibernate.boot.registry.StandardServiceRegistryBuilder; Importorg.hibernate.cfg.Configuration; ImportOrg.hibernate.service.*;Importcom.ij34.dao.Address;Importcom.ij34.dao.People;  Public classtest01 { Public Static voidMain (string[] args)throwsException {//Instantiate a configurationConfiguration conf=NewConfiguration (). Configure (); Serviceregistry SR=NewStandardserviceregistrybuilder (). Applysettings (Conf.getproperties ()). build (); //creating a Sessionfactory instance with a configuration instanceSessionfactory sf=conf.buildsessionfactory (SR); //Create sessionSession session=sf.opensession (); //Start TransactionTransaction tx=session.begintransaction (); People Person=Newpeople (); Person.setage (29); //set values for two members of a composite primary keyPeople people=Newpeople (); People.setage (22); People.setname ("Lin Biao"); Address a=NewAddress (); A.setmessage (Guangzhou);   A.setpeople (people); Address A2=NewAddress (); A2.setmessage (Hong Kong);   People.getaddress (). Add (A2);   Session.persist (a);   Session.save (people);    Session.persist (A2);    Tx.commit ();    Session.close ();    Sf.close (); }    }

Hibernate----1-n--jointable (person and address)

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.