环境和结构和上一章节一样,不细述。。直接看代码
实例条件:Person 和 Address ,一个人用于多个房子,人可以改变房子,房子也可以变动人
Person类:
package cn.dao;
import java.io.Serializable;
import java.util.HashSet; import java.util.Set;import javax.persistence.CascadeType;
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table;
@Table(name = "person") public class Person implements Serializable{ private Long id; private String name; private String defaultAddress; private Set<Address> as = new HashSet<Address>();
@Id
@Column(name = "id") @GeneratedValue(strategy = GenerationType.IDENTITY) public Long getId() { return id; }
public void setId(Long id) {
this.id = id; }
@Column(name = "name")
public String getName() { return name; }
public void setName(String name) {
this.name = name; } @Column(name = "defaultAddress") public String getDefaultAddress() { return defaultAddress; }
public void setDefaultAddress(String defaultAddress) {
this.defaultAddress = defaultAddress; } @OneToMany (fetch = FetchType.LAZY,cascade = { CascadeType.ALL },mappedBy = "person") public Set<Address> getAs() { return as; }
public void setAs(Set<Address> as) {
this.as = as; }
}
Address类:
package cn.dao;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table;
import com.sun.istack.internal.NotNull;
@Entity
@Table(name = "address") public class Address implements Serializable{ private Long id; private String name; private Person person;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) public Long getId() { return id; }
public void setId(Long id) {
this.id = id; } @Column(name = "name") public String getName() { return name; }
public void setName(String name) {
this.name = name; } @ManyToOne @NotNull @JoinColumn(name = "personId") public Person getPerson() { return person; }
public void setPerson(Person person) {
this.person = person; } }
主类:OneToManyDouble:
package cn.test;
import java.util.HashSet;
import java.util.Set;import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test;
import cn.dao.Address;
import cn.dao.Person; import cn.utils.HibernateUtil;
/**
* 一对多的双向关联 * @author hasee * */ public class OneToManyDouble { public static void main(String[] args){ editFromAddress(); } public static void save(){ Session session = HibernateUtil.getSession(); org.hibernate.Transaction transaction = session.beginTransaction(); Person person = new Person(); person.setName("Lucy"); person.setDefaultAddress("BeiJing");//默认地址 Set<Address> as = new HashSet<Address>();//旗下有许多房子 Address addressOne = new Address(); addressOne.setName("ShangHai"); addressOne.setPerson(person); as.add(addressOne); Address addressTwo = new Address(); addressTwo.setName("ShenZhen"); addressTwo.setPerson(person); as.add(addressTwo); person.setAs(as); session.save(person); transaction.commit(); HibernateUtil.closeSession(); } public static void delete(){ Session session = HibernateUtil.getSession(); Transaction transaction = session.beginTransaction(); //删除一个人的同时删除其关联的住址 Person person = (Person) session.get(Person.class, 5L); session.delete(person); transaction.commit(); //后台打印查看sql语句发现效率很低,可以直接根据ID删除Address表的信息,不必关联查询 HibernateUtil.closeSession(); } public static void edit(){ Session session = HibernateUtil.getSession(); Transaction transaction = session.beginTransaction(); //根据ID获取到人的信息,修改Address表的住址 Person person = (Person) session.get(Person.class, 6L); Set<Address> as = person.getAs(); for (Address address : as) { if(address.getName().equals("ShangHai")){ address.setName("ShangHai_change"); } } session.update(person); transaction.commit(); HibernateUtil.closeSession(); } //根据Address操作Person表 public static void saveFromAddress(){ Session session = HibernateUtil.getSession(); Transaction transaction = session.beginTransaction(); Address address = new Address(); address.setName("newCity"); Person person = (Person) session.get(Person.class, 6L);//新的住址属于已经存在的一个人的(也可以新建一个Person) address.setPerson(person); session.save(address);//这里是保存Person还是Address呢?----> 结果是:Address <----- transaction.commit(); HibernateUtil.closeSession(); } public static void deleteFromAddress(){ Session session = HibernateUtil.getSession(); Transaction transaction = session.beginTransaction(); Address address = (Address) session.get(Address.class, 12L); session.delete(address);//对Person是没什么影响的 transaction.commit(); HibernateUtil.closeSession(); } public static void editFromAddress(){ Session session = HibernateUtil.getSession(); Transaction transaction = session.beginTransaction(); Address address = (Address) session.get(Address.class, 13L); Person person = address.getPerson(); person.setName("Mike");//直接修改掉主人 session.update(address); transaction.commit(); HibernateUtil.closeSession(); } }