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; @Embeddable public 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 { //实例化Configuration Configuration conf=new Configuration().configure(); ServiceRegistry SR=new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build(); // 以Configuration实例创建SessionFactory实例 SessionFactory SF=conf.buildSessionFactory(SR); //create session Session session=SF.openSession(); //start 事务 Transaction tx=session.beginTransaction(); People person = new People(); person.setAge(29); // 为复合主键的两个成员设置值 People people=new People(); people.setAge(22); people.setName("林彪"); // 持久化People对象(对应于插入主表记录) session.save(people); Address a=new Address(); a.setMessage("广州"); people.setAddress(a); School s1=new School(); School s2=new School(); s1.setName("哈佛大学"); s2.setName("社会大学"); session.save(s1); session.save(s2); a.getSchool().add(s2); a.getSchool().add(s1); tx.commit(); session.close(); SF.close(); } }