JPA和hibernate对删除操作的不同

  在hibernate里面调用session的delete方法以后,无论这个被删除的对象有没有被人外键引用到,都可以被删除,并且此时的外键设为 null,也就是说他会自动帮我们去查看他被谁引用到了。然后把引用全部去掉后,再把自己删掉。而在JPA里面,如果调用 EntityManager.remove方法时,传进去的对象,有被外键引用到,则会失败。因为JPA里面的实现就是直接执行delete语句,也不管 他有没有被外键引用,此时,当然会出错了。

中国网管联盟www.bitscn.com

    测试时候使用的两个类分别如下: 网管网bitsCN_com

    举的例子是部门和员工的关系。一个部门可以有多个员工。然后把部门删掉的时候,员工的部门属性就为null了,不过,按照严谨来说,还是JPA的严谨一些。这样可以防止误操作,呵呵。

    部门的实体对象 网管网bitsCN.com

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 
*/
package  com.hadeslee.jpaentity;

中国网管联盟www_bitscn_com



import  java.io.Serializable;
import  java.util.HashSet;
import  java.util.Set;
import  javax.persistence.Entity;
import  javax.persistence.GeneratedValue;
import  javax.persistence.GenerationType;
import  javax.persistence.Id; 54com.cn
import  javax.persistence.OneToMany;
import  javax.persistence.Table;

/**
 *
 * 
@author  hadeslee
 
*/
@Entity
@Table(name 
=   " JPADepartment " ) 网管联盟www.bitsCN.com
public   class  Department  implements  Serializable {

    
private   static   final   long  serialVersionUID  =   1L ;

feedom.net


    @Id
    @GeneratedValue(strategy 
=  GenerationType.AUTO)
    
private  Long id;
    @OneToMany(mappedBy 
=   " department " )
    
private  Set < Person >  persons  =   new  HashSet < Person > (); 54com.cn
    
private  String deptName;
    
private  String description;

    
public  String getDeptName() {
        
return  deptName;
    }

    
public   void  setDeptName(String deptName) { feedom.net
        
this .deptName  =  deptName;
    }

    
public  String getDescription() {
        
return  description;
    }

    
public   void  setDescription(String description) {
网管网bitsCN_com

        
this .description  =  description;
    }
    
    
public  Set < Person >  getPersons() {
        
return  persons;
    }

    
public   void  setPersons(Set < Person >  persons) {

中国网管联盟www.bitscn.com


        
this .persons  =  persons;
    }

    
public  Long getId() {
        
return  id;
    }

    
public   void  setId(Long id) {
        
this .id  =  id; 54com.cn
    }

    @Override
    
public   int  hashCode() {
        
int  hash  =   0 ;
        hash 
+=  (id  !=   null   ?  id.hashCode() :  0 );

feedom.net


        
return  hash;
    }

    @Override
    
public   boolean  equals(Object object) {
        
//  TODO: Warning - this method won't work in the case the id fields are not set
         if  ( ! (object  instanceof  Department)) {

54com.cn


            
return   false ;
        }
        Department other 
=  (Department) object;
        
if  (( this .id  ==   null   &&  other.id  !=   null ||  ( this .id  !=   null   &&   ! this .id.equals(other.id))) {
feedom.net

            
return   false ;
        }
        
return   true ;
    }

    @Override
    
public  String toString() {
        
return   " com.hadeslee.jpaentity.Department[id= "   +  id  +   " ] " ; 中国网管论坛bbs.bitsCN.com
    }
}


    人员的实体对象

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 
*/ 54com.cn
package  com.hadeslee.jpaentity;

import  java.io.Serializable;
import  javax.persistence.Entity;
import  javax.persistence.GeneratedValue;
import  javax.persistence.GenerationType;
import  javax.persistence.Id;
import  javax.persistence.ManyToOne;

中国网管联盟www.bitscn.com


import  javax.persistence.Table;

/**
 *
 * 
@author  hadeslee
 
*/
@Entity
@Table(name 
=   " JPAPerson " )
public   class  Person  implements  Serializable {
中国网管联盟www、bitsCN、com


    
private   static   final   long  serialVersionUID  =   1L ;
    @Id
    @GeneratedValue(strategy 
=  GenerationType.AUTO)
    
private  Integer id; 网管网bitsCN_com
    
private  String name;
    
private   int  age;
    @ManyToOne
    
private  Department department;

    
public   int  getAge() {
        
return  age;
中国网管联盟www.bitscn.com

    }

    
public   void  setAge( int  age) {
        
this .age  =  age;
    }

    
public  Department getDepartment() {
        
return  department;
中国网管联盟www.bitscn.com

    }

    
public   void  setDepartment(Department department) {
        
this .department  =  department;
    }

    
public  String getName() {
        
return  name;

中国网管联盟www、bitsCN、com


    }

    
public   void  setName(String name) {
        
this .name  =  name;
    }

    
public  Integer getId() {
        
return  id;
    }
54com.cn


    
public   void  setId(Integer id) {
        
this .id  =  id;
    }

    @Override
    
public   int  hashCode() {
        
int  hash  =   0 ;
54ne.com

        hash 
+=  (id  !=   null   ?  id.hashCode() :  0 );
        
return  hash;
    }

    @Override
    
public   boolean  equals(Object object) {
feedom.net

        
//  TODO: Warning - this method won't work in the case the id fields are not set
         if  ( ! (object  instanceof  Person)) {
            
return   false ;
网管网bitsCN_com

        }
        Person other 
=  (Person) object;
        
if  (( this .id  ==   null   &&  other.id  !=   null ||  ( this .id  !=   null   &&   ! this .id.equals(other.id))) {
中国网管联盟www.bitscn.com

            
return   false ;
        }
        
return   true ;
    }

    @Override
    
public  String toString() {
        
return   " com.hadeslee.jpaentity.Person[id= "   +  id  +   " ] " ;

网管网bitsCN.com


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值