Hibernate,持久性和@OneToMany和@ManyToOne注释问题 [英] Hibernate, Persistence and @OneToMany and @ManyToOne annotations problems
问题描述
我在@OneToMany和@ManyToOne批注中遇到了一些问题。
我有两个类Suite和SuiteVersion。 SuiteVersion取决于套件。所以我已经在我的代码中实现了这一点:
Class Suite:
@OneToMany(mappedBy =suite)
@Cascade(CascadeType.DELETE_ORPHAN)
private List< SuiteVersion> listSuiteVersion = new ArrayList< SuiteVersion>();
Class SuiteVersion:
@ManyToOne()
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
私人套房套件;
但是当我删除SuiteVersion关联的套件时,我遇到了一些问题。
在Suite.I之前,Hibernate不会删除SuiteVersion。我不知道为什么,因为我已经在我的代码中提到了这一点:
@Cascade(CascadeType.DELETE_ORPHAN)
当我删除套件时, p>
Hibernate:从SUITE中删除,其中ID_SUITE =?
13août2010 09:40:50 org.hibernate.util.JDBCExceptionReporter logExceptions
注意:SQL错误:-8,SQLState:23504
13août2010 09:40:50 org.hibernate .util.JDBCExceptionReporter logExceptions
GRAVE:完整性约束违规:外键无操作; FK42895651EA304E6表:SUITE_VERSION
预先感谢您的帮助。
最好的问候,
Florent,
PS:对不起我的英文我是法语。
但是,当我删除SuiteVersion关联的套件时,我遇到了一些问题。 Hibernate不会在
Suite
之前删除SuiteVersion
。
这是因为您 级联 我使用完全限定名来清楚地显示Hibernate特有的和标准的JPA注释。 特定于Hibernate的 请注意,如果你正在使用JPA 2.0(即Hibernate 3.5+),现在有一个标准的方法来处理孤立记录,即不使用Hibernate特定的注释。您可以在 但是这超出了这个问题,因为这实际上不是您要查找的内容。 I have some problem with @OneToMany and @ManyToOne annotations. I have two class Suite and SuiteVersion. A SuiteVersion is dependent of a suite. So i have implemented this in my code: Class Suite : Class SuiteVersion : But i have some problem when i delete a Suite whose have SuiteVersion associated.
Hibernate don't delete SuiteVersion before Suite.I don't know why because i have mentioned this in my code : This the log i obtained when i delete suite : Hibernate: delete from SUITE where ID_SUITE=?
13 août 2010 09:40:50 org.hibernate.util.JDBCExceptionReporter logExceptions
ATTENTION: SQL Error: -8, SQLState: 23504
13 août 2010 09:40:50 org.hibernate.util.JDBCExceptionReporter logExceptions
GRAVE: integrity constraint violation: foreign key no action; FK42895651EA304E6 table: SUITE_VERSION Thank you in advance for your help. Best Regards, Florent, P.S : Sorry for my english i'm french. But i have some problem when i delete a Suite whose have SuiteVersion associated. Hibernate don't delete That's because you're NOT cascading the I used fully qualified names to clearly show the Hibernate specific and the standard JPA annotations here. The Hibernate specific Note that if you are using JPA 2.0 (i.e. Hibernate 3.5+), there is now a standard way to deal with orphan records i.e. without using the Hibernate specific annotation. You can specify an But this goes beyond this question as this is actually not what you're looking for. 这篇关于Hibernate,持久性和@OneToMany和@ManyToOne注释问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! REMOVE
操作< SuiteVersion
。为了获得所需的结果,你需要类似这样的东西(假设你使用的是JPA 1.0):
pre $ @OneToMany(mappedBy = suite,cascade = javax.persistence.CascadeType.REMOVE)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private List< SuiteVersion> listSuiteVersion = new ArrayList< SuiteVersion>();
CascadeType.DELETE_ORPHAN
是别的,它用来告诉Hibernate 删除特定的 SuiteVersion
>如果您从集合中移除它(没有它,则会更新 SuiteVersion
记录以移除链接到父母套房
,但会留在表中,因此是一个孤儿)。
OneToMany
(和 OneToOne
)中指定 orphanRemoval
>)关联。像这样:
@OneToMany(mappedBy =suite,cascade = CascadeType.REMOVE,orphanRemoval = true)
私人列表< SuiteVersion> listSuiteVersion = new ArrayList< SuiteVersion>();
@OneToMany(mappedBy = "suite")
@Cascade(CascadeType.DELETE_ORPHAN)
private List<SuiteVersion> listSuiteVersion = new ArrayList<SuiteVersion>();
@ManyToOne()
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
private Suite suite;
@Cascade(CascadeType.DELETE_ORPHAN)
SuiteVersion
before Suite
.REMOVE
operation from Suite
to SuiteVersion
. To obtain the desired result, you need something like this (assuming you're using JPA 1.0):@OneToMany(mappedBy = "suite", cascade = javax.persistence.CascadeType.REMOVE)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private List<SuiteVersion> listSuiteVersion = new ArrayList<SuiteVersion>();
CascadeType.DELETE_ORPHAN
is something else, it is used to tell Hibernate to delete a specific SuiteVersion
if you remove it from the collection (without it, the SuiteVersion
record would be updated to remove the link to the parent Suite
but would remain in the table, being thus an "orphan").orphanRemoval
option in your OneToMany
(and OneToOne
) association. Like this:@OneToMany(mappedBy = "suite", cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<SuiteVersion> listSuiteVersion = new ArrayList<SuiteVersion>();