博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate学习系列————注解一对多双向实例
阅读量:6543 次
发布时间:2019-06-24

本文共 5179 字,大约阅读时间需要 17 分钟。

hot3.png

环境和结构和上一章节一样,不细述。。直接看代码

 

实例条件: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();
 }
 
}

转载于:https://my.oschina.net/u/2509896/blog/760329

你可能感兴趣的文章
微软邮件系统Exchange 2013系列(七)创建发送连接器
查看>>
程序员杂记系列
查看>>
【树莓派】制作树莓派所使用的img镜像(一)
查看>>
理解网站并发量
查看>>
spring整合elasticsearch之环境搭建
查看>>
TensorFlow 架构与设计-编程模型【转】
查看>>
如何运行Struts2官网最新Demo?
查看>>
'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)
查看>>
XDebug 教程
查看>>
js 去html 标签
查看>>
好久不见
查看>>
小tips:JS中的children和childNodes
查看>>
二叉树的遍历
查看>>
Oracle的FIXED_DATE参数
查看>>
NDK配置
查看>>
(转)@ContextConfiguration注解说明
查看>>
[置顶] ※数据结构※→☆线性表结构(queue)☆============队列 顺序存储结构(queue sequence)(八)...
查看>>
Linux 系统的单用户模式、修复模式、跨控制台登录在系统修复中的运用
查看>>
《http权威指南》阅读笔记(十)
查看>>
JQuery UI Widget Factory官方Demo
查看>>