Hibernate----component-entity (person-address-school)

Source: Internet
Author: User

 PackageCom.ij34.dao;ImportJavax.persistence.*; @Entity @table (name= "School_inf") Public classSchool {@Id @Column (name= "school_id") @GeneratedValue (Strategy=generationtype.identity)PrivateInteger ID; PrivateString name;  PublicInteger getId () {returnID; }     Public voidsetId (Integer id) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }    }

 PackageCom.ij34.dao;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 ; Privateaddress address;  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 ; }     PublicAddress getaddress () {returnaddress; }     Public voidsetaddress (address address) { This. Address =address; }}

 PackageCom.ij34.dao;ImportJava.util.HashSet;ImportJava.util.Set;ImportJavax.persistence.*;Importorg.hibernate.annotations.Parent; @Embeddable Public classaddress{@Column (name= "Message")    PrivateString message; @ParentPrivatepeople people; @OneToMany (targetentity=school.class) @JoinColumn (name= "address_id", referencedcolumnname= "people_id")    PrivateSet<school> school=NewHashset<>();  PublicString getMessage () {returnmessage; }     Public voidsetmessage (String message) { This. Message =message; }     PublicPeople GetPeople () {returnpeople; }     Public voidSetpeople (People people) { This. People =people; }     PublicSet<school>Getschool () {returnSchool; }     Public voidSetschool (set<school>School) {         This. School =School; }    }
 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;ImportCom.ij34.dao.School;  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"); //persisting people objects (corresponding to inserting a master table record)Session.save (people); Address a=NewAddress (); A.setmessage (Guangzhou);   People.setaddress (a); School S1=NewSchool (); School S2=NewSchool (); S1.setname ("Harvard University"); S2.setname ("Social University");   Session.save (S1);   Session.save (S2);   A.getschool (). Add (S2);    A.getschool (). Add (S1);    Tx.commit ();    Session.close ();    Sf.close (); }    }

Hibernate----component-entity (person-address-school)

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.