首页 > 代码库 > hibernate----1-N--jointable(人与地址)
hibernate----1-N--jointable(人与地址)
package com.ij34.dao; import java.util.HashMap; import java.util.HashSet; import java.util.Set; 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; @OneToMany(targetEntity=Address.class) //与另一个相比,没有mappedBy="people" @JoinTable(name="people_address" ,joinColumns=@JoinColumn(name="peopleId" ,referencedColumnName="people_id") ,inverseJoinColumns=@JoinColumn(name="addressId" ,referencedColumnName="address_id" ,unique=true) ) private Set<Address> address=new HashSet<>(); 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 Set<Address> getAddress() { return address; } public void setAddress(Set<Address> address) { this.address = address; } }
package com.ij34.dao; import javax.persistence.*; @Entity @Table(name="Address_inf") public class Address{ @Id @Column(name="address_id") @GeneratedValue(strategy=GenerationType.IDENTITY) private int addressId; private String message; @ManyToOne(targetEntity=People.class) @JoinTable(name="people_address" ,joinColumns=@JoinColumn(name="addressId" ,referencedColumnName="address_id" ,unique=true) ,inverseJoinColumns=@JoinColumn(name="peopleId" ,referencedColumnName="people_id" ) ) private People people; public int getAddressId() { return addressId; } public void setAddressId(int addressId) { this.addressId = addressId; } 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; } }
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; 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("林彪"); Address a=new Address(); a.setMessage("广州"); a.setPeople(people); Address a2=new Address(); a2.setMessage("香港"); people.getAddress().add(a2); session.persist(a); session.save(people); session.persist(a2); tx.commit(); session.close(); SF.close(); } }
hibernate----1-N--jointable(人与地址)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。