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

Source: Internet
Author: User

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

package com.ij34.dao;import javax.persistence.*;@Entity@Table(name="school_inf")public class School {     @Id @Column(name="school_id")     @GeneratedValue(strategy=GenerationType.IDENTITY)    private Integer id;    private String name;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    }

 

 

 

package com.ij34.dao;import javax.persistence.*;@Entity@Table(name="people_inf")public class People implements java.io.Serializable{    private static final long serialVersionUID = 1L;    @Id @Column(name="people_id")    @GeneratedValue(strategy=GenerationType.IDENTITY)    private Integer id;    private String name;    private int age;    private Address address;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public Address getAddress() {        return address;    }    public void setAddress(Address address) {        this.address = address;    }}

 

 

package com.ij34.dao;import java.util.HashSet;import java.util.Set;import javax.persistence.*;import org.hibernate.annotations.Parent;@Embeddablepublic class Address{    @Column(name="message")    private String message;    @Parent    private People people;    @OneToMany(targetEntity=School.class)    @JoinColumn(name="address_id",referencedColumnName="people_id")    private Set<School> school=new HashSet<>();    public String getMessage() {        return message;    }    public void setMessage(String message) {        this.message = message;    }    public People getPeople() {        return people;    }    public void setPeople(People people) {        this.people = people;    }    public Set<School> getSchool() {        return school;    }    public void setSchool(Set<School> school) {        this.school = school;    }    }
Package com. ij34.web; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. transaction; import org. hibernate. boot. registry. standardServiceRegistryBuilder; import org. hibernate. cfg. configuration; import org. hibernate. service. *; import com. ij34.dao. address; import com. ij34.dao. people; import com. ij34.dao. school; public class test01 {public static void main (String [] args) throws Exception {// instantiate Configuration conf = new Configuration (). configure (); ServiceRegistry SR = new StandardServiceRegistryBuilder (). applySettings (conf. getProperties ()). build (); // create SessionFactory instance SessionFactory SF = conf in the Configuration instance. buildSessionFactory (SR); // create session Session session = SF. openSession (); // start Transaction tx = session. beginTransaction (); People person = new People (); person. setAge (29); // set the value of People people = new People (); people. setAge (22); people. setName ("Lin Biao"); // persistent People object (corresponding to inserting a master table record) session. save (people); Address a = new Address ();. setMessage ("Guangzhou"); people. setAddress (a); School s1 = new School (); School s2 = new School (); s1.setName ("Harvard University"); s2.setName (""); session. save (s1); session. save (s2);. getSchool (). add (s2);. getSchool (). add (s1); tx. commit (); session. close (); SF. close ();}}

 

 

 

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.