Hibernate使用orphanRemoval触发约束违规 [英] Hibernate triggering constraint violations using orphanRemoval

查看:1107
本文介绍了Hibernate使用orphanRemoval触发约束违规的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JPA / Hibernate(3.5.3)设置中遇到问题,我有一个实体,一个Account类,它有一个子实体列表Contact实例。我试图将联系人的实例添加/删除到列表<联系人>

在集合中添加一个新实例并调用saveOrUpdate(account)可以保持一切可爱。如果我然后选择从列表中删除联系人并再次调用saveOrUpdate,SQL Hibernate似乎产生涉及将account_id列设置为null,这违反了数据库约束。



我在做什么错了?



下面的代码显然是一个简化的摘要,但我认为它涵盖了问题,因为我在不同的代码中看到了相同的结果,



SQL:

  CREATE TABLE帐户(INT account_id); 
CREATE TABLE contact(INT contact_id,INT account_id REFERENCES account(account_id));

Java:

  @Entity 
class Account {
@Id
@Column
public Long id;

@OneToMany(cascade = CascadeType.ALL,orphanRemoval = true)
@JoinColumn(name =account_id)
public List< Contact>联系人;
}

@Entity
class联系人{
@Id
@Column
public Long id;

@ManyToOne(可选= false)
@JoinColumn(name =account_id,nullable = false)
公共账户;
}

帐户帐户=新帐户();
联系人联系人=新联系人();

account.contacts.add(contact);
saveOrUpdate(account);

//一段时间后,像另一个servlet请求一样....

account.contacts.remove(contact);
saveOrUpdate(account);

结果:

  UPDATE联系人SET account_id = null在哪里contact_id =? 

编辑#1:

因为这实际上是一个错误
http://opensource.atlassian。编辑#2:

有一个似乎可行的解决方案,但涉及到使用Hibernate API

  class Account {
@SuppressWarnings( 弃用)
@OneToMany(cascade = CascadeType.ALL,mappedBy =account)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@JoinColumn(name = account_id,nullable = false)
私人设定<联络人> contacts = new HashSet< Contact>();
}

class联系{
@ManyToOne(可选= false)
@JoinColumn(name =account_id,nullable = false)
私人账户帐户;



$ b

由于Hibernate CascadeType.DELETE_ORPHAN已被弃用,我不得不假设它已经被JPA2版本所取代,但是实现中缺少一些东西。

解决方案

一些评论:


  • 由于您有双向关联,您需要添加 mappedBy 属性来声明自己的一面该协会。

  • 另外不要忘记,在使用双向关联时,您需要管理链接的两侧,我建议使用防御方法(如下所示)。 >
  • 您必须在联系人上执行等于 hashCode / code>。



因此,在 Account 映射如下:

  @Entity 
public class Account {
@Id @GeneratedValue
公开长ID;

@OneToMany(cascade = CascadeType.ALL,mappedBy =account,orphanRemoval = true)
public List< Contact> contacts = new ArrayList< Contact>();

public void addToContacts(Contact contact){
this.contacts.add(contact);
contact.setAccount(this);
}

public void removeFromContacts(Contact contact){
this.contacts.remove(contact);
contact.setAccount(null);
}

// getters,setters
}



<在联系中,重要的部分是 @ManyToOne 字段应该有可选标记设为 false :

  @实体
public class Contact {
@Id @GeneratedValue
public Long id;

@ManyToOne(可选= false)
公共账户;

// getters,setters,equals,hashCode

}

通过这些修改,以下内容可以正常工作:

  Account account = new Account(); 
联系人联系人=新联系人();

account.addToContact(contact);
em.persist(account);
em.flush();

assertNotNull(account.getId());
assertNotNull(account.getContacts()。get(0).getId());
assertEquals(1,account.getContacts()。size());

account.removeFromContact(contact);
em.merge(account);
em.flush();
assertEquals(0,account.getContacts()。size());

并将孤立的 Contact 删除,如下预期。使用Hibernate 3.5.3-Final进行测试。


I'm having trouble with a JPA/Hibernate (3.5.3) setup, where I have an entity, an "Account" class, which has a list of child entities, "Contact" instances. I'm trying to be able to add/remove instances of Contact into a List<Contact> property of Account.

Adding a new instance into the set and calling saveOrUpdate(account) persists everything lovely. If I then choose to remove the contact from the list and again call saveOrUpdate, the SQL Hibernate seems to produce involves setting the account_id column to null, which violates a database constraint.

What am I doing wrong?

The code below is clearly a simplified abstract but I think it covers the problem as I'm seeing the same results in different code, which really is about this simple.

SQL:

CREATE TABLE account ( INT account_id );
CREATE TABLE contact ( INT contact_id, INT account_id REFERENCES account (account_id) );

Java:

@Entity
class Account {
  @Id
  @Column
  public Long id;

  @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
  @JoinColumn(name = "account_id")
  public List<Contact> contacts;
}

@Entity
class Contact {
  @Id
  @Column
  public Long id;

  @ManyToOne(optional = false)
  @JoinColumn(name = "account_id", nullable = false)
  public Account account;
}

Account account = new Account();
Contact contact = new Contact();

account.contacts.add(contact);
saveOrUpdate(account);

// some time later, like another servlet request....

account.contacts.remove(contact);
saveOrUpdate(account);

Result:

UPDATE contact SET account_id = null WHERE contact_id = ?

Edit #1:

It might be that this is actually a bug http://opensource.atlassian.com/projects/hibernate/browse/HHH-5091

Edit #2:

I've got a solution that seems to work, but involves using the Hibernate API

class Account {
    @SuppressWarnings("deprecation")
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "account")
    @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    @JoinColumn(name = "account_id", nullable = false)
    private Set<Contact> contacts = new HashSet<Contact>();
}

class Contact {
    @ManyToOne(optional = false)
    @JoinColumn(name = "account_id", nullable = false)
    private Account account;
}

Since Hibernate CascadeType.DELETE_ORPHAN is deprecated, I'm having to assume that it has been superseded by the JPA2 version, but the implementation is lacking something.

解决方案

Some remarks:

  • Since you have a bi-directional association, you need to add a mappedBy attribute to declare the owning side of the association.
  • Also don't forget that you need to manage both sides of the link when working with bi-directional associations and I suggest to use defensive methods for this (shown below).
  • And you must implement equals and hashCode on Contact.

So, in Account, modify the mapping like this:

@Entity
public class Account {
    @Id @GeneratedValue
    public Long id;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "account", orphanRemoval = true)
    public List<Contact> contacts = new ArrayList<Contact>();

    public void addToContacts(Contact contact) {
        this.contacts.add(contact);
        contact.setAccount(this);
    }

    public void removeFromContacts(Contact contact) {
        this.contacts.remove(contact);
        contact.setAccount(null);
    }

    // getters, setters
}

In Contact, the important part is that the @ManyToOne field should have the optional flag set to false:

@Entity
public class Contact {
    @Id @GeneratedValue
    public Long id;

    @ManyToOne(optional = false)
    public Account account;

    // getters, setters, equals, hashCode

}

With these modifications, the following just works:

Account account = new Account();
Contact contact = new Contact();

account.addToContact(contact);
em.persist(account);
em.flush();

assertNotNull(account.getId());
assertNotNull(account.getContacts().get(0).getId());
assertEquals(1, account.getContacts().size());

account.removeFromContact(contact);
em.merge(account);
em.flush();
assertEquals(0, account.getContacts().size());

And the orphaned Contact gets deleted, as expected. Tested with Hibernate 3.5.3-Final.

这篇关于Hibernate使用orphanRemoval触发约束违规的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆