16

我在使用 JPA/Hibernate (3.5.3) 设置时遇到问题,其中我有一个实体,一个“Account”类,它有一个子实体列表,“Contact”实例。我试图能够将联系人的实例添加/删除到 Account 的 List<Contact> 属性中。

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

我究竟做错了什么?

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

SQL:

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

爪哇:

@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);

结果:

UPDATE contact SET account_id = null WHERE contact_id = ?

编辑#1:

可能这实际上是一个错误 http://opensource.atlassian.com/projects/hibernate/browse/HHH-5091

编辑#2:

我有一个似乎可行的解决方案,但涉及使用 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;
}

由于不推荐使用 Hibernate CascadeType.DELETE_ORPHAN,我不得不假设它已被 JPA2 版本取代,但实现缺少一些东西。

4

1 回答 1

21

一些备注:

  • 由于您具有双向关联,因此您需要添加一个mappedBy属性来声明关联的拥有方。
  • 另外不要忘记在使用双向关联时需要管理链接的两端,我建议为此使用防御方法(如下所示)。
  • 而且您必须实施equalsand hashCodeon Contact

因此,在 中Account,像这样修改映射:

@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
}

Contact中,重要的部分是该@ManyToOne字段应将optional标志设置为false

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

    @ManyToOne(optional = false)
    public Account account;

    // getters, setters, equals, hashCode

}

通过这些修改,以下内容将起作用:

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());

正如预期的那样,孤儿Contact被删除了。使用 Hibernate 3.5.3-Final 测试。

于 2010-06-18T15:48:33.333 回答