Differences between JPA and hibernate for delete operations

Source: Internet
Author: User
Tags hash object object

After calling the session's Delete method inside the hibernate, the deleted object can be deleted, regardless of whether it is referenced by a foreign key, and the foreign key at this time is set to NULL, which means that he will automatically help us to see who has been quoted by someone. Then remove all the references and delete them. In JPA, if the Entitymanager.remove method is invoked, the object passed in is referenced by a foreign key, and it fails. Because the implementation in JPA is to execute the DELETE statement directly, and whether or not he has been referenced by a foreign key, there is of course an error.

The two classes used at the time of the test were as follows:

The example is the relationship between the department and the staff. A department can have more than one employee. Then when the department is deleted, the employee's departmental attributes are null, but, in strict terms, the JPA is more rigorous. This can prevent misoperation, hehe.

Entity objects for the Department

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
Package com.hadeslee.jpaentity;
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;
Import Javax.persistence.OneToMany;
Import javax.persistence.Table;
/**
*
* @author Hadeslee
*/
@Entity
@Table (name = "Jpadepartment")
public class Department implements Serializable {
Private static final long serialversionuid = 1L;
@Id
@GeneratedValue (strategy = Generationtype.auto)
Private Long ID;
@OneToMany (mappedby = "department")
Private set<person> persons = new hashset<person> ();
Private String Deptname;
Private String description;
Public String Getdeptname () {
return deptname;
}
public void Setdeptname (String deptname) {
This.deptname = Deptname;
}
Public String getdescription () {
return description;
}
public void SetDescription (String description) {
this.description = description;
}
Public set<person> getpersons () {
return persons;
}
public void Setpersons (set<person> persons) {
this.persons = persons;
}
Public Long getId () {
return ID;
}
public void SetId (Long id) {
This.id = ID;
}
@Override
public int hashcode () {
int hash = 0;
Hash + = (id!= null id.hashcode (): 0);
return hash;
}
@Override
public Boolean equals (Object object) {
Todo:warning-this method won ' t work the ID fields are not set
if (!) ( Object instanceof Department)) {
return false;
}
Department other = (Department) object;
if ((this.id = null && other.id!= null) | | (this.id!= null &&!this.id.equals (other.id))) {
return false;
}
return true;
}
@Override
Public String toString () {
Return "com.hadeslee.jpaentity.department[id=" + ID + "]";
}
}

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.