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)