hibernate枚举@elementcollection初始化后删除 [英] hibernate enum @elementcollection deleted after initialization
问题描述
我有简单的枚举:
public enum Privilege implements Serializable {
P1,
P2,
P3;
}
映射到如下实体:
@Entity
@Table(name =rol_roles,
uniqueConstraints = {...)
public class Role extends AbstractSomething {
...
@ElementCollection(targetClass = Privilege.class,fetch = FetchType.LAZY)
@CollectionTable(name =rol_roles_privileges,
joinColumns =
@JoinColumn(name =role_id))
@Column(name =privilege,nullable = false)
@Enumerated(EnumType.STRING)
private Set< Privilege> privileges = EnumSet.noneOf(Privilege.class);
public Set< Privilege> getPrivileges(){
return privilege;
}
public void setPrivileges(Set< Privilege> privileges){
this.privileges = privileges;
}
}
现在,我尝试用实体 - 这一切都很好 - 实体得到保存没有问题。当我试图从数据库获取实体,hibernate决定他不喜欢我的收藏,删除它。 sql from log:
Hibernate:
select
role0_.id as id1_6_,
role0_ .version_num as version2_6_,
role0_.code as code3_6_,
role0_.comments as comments4_6_,
role0_.title as title5_6_,
role0_.title_en as title6_6_,
role0_ .valid_from as valid7_6_,
role0_.valid_till as valid8_6_
从
rol_roles role0_
其中
role0_.id =?
Hibernate:
select
privileges0_.role_id as role1_6_0_,
privileges0_.privilege as privileg2_7_0_,
从
rol_roles_privileges privileges0_
其中
privileges0_.role_id =?
Hibernate:
delete
从
rol_roles_privileges
其中
role_id =?
由于集合是延迟的,所以权限和删除语句在集合初始化。我尝试添加@OrderColumn注释在类似的线程建议,但那没有帮助。
在类似的情况下没有insert语句,所以读取对象只是向外滑动集合。
表创建如下:
create table rol_roles_privileges(
role_id int8 not null,
privilege varchar(255)not null,
primary key(role_id,privilege)
);
奇怪的是(或许不是)当我设置fetchtype.EAGER - 但它不工作
< h2_lin>解决方案
对于未来的Google用户:
当使用enable_lazy_load_no_trans时,似乎hibernate 4.2是有问题的。
此错误可能与 https://hibernate.atlassian.net/browse/HHH连接-7524
正在记录:
log:
错误AssertionFailure:43 - HHH000099:断言失败Hibernate中的一个错误,但更有可能是由于不安全的使用会话):org.hibernate.AssertionFailure:集合所有者没有与session相关联:org.hibernate.test.ondemandload.Store.inventories
WARN AbstractPersistentCollection:246 - 无法关闭用于加载与无任何会话相关联的延迟集合的临时会话
我现在使用的是hibernate 4.1.7,并且都适用于延迟加载。
I have simple enum:
public enum Privilege implements Serializable {
P1,
P2,
P3;
}
which is mapped in an entity like this:
@Entity
@Table(name = "rol_roles",
uniqueConstraints = {...)
public class Role extends AbstractSomething {
...
@ElementCollection(targetClass = Privilege.class, fetch = FetchType.LAZY)
@CollectionTable(name = "rol_roles_privileges",
joinColumns =
@JoinColumn(name = "role_id"))
@Column(name = "privilege", nullable = false)
@Enumerated(EnumType.STRING)
private Set<Privilege> privileges = EnumSet.noneOf(Privilege.class);
public Set<Privilege> getPrivileges() {
return privileges;
}
public void setPrivileges(Set<Privilege> privileges) {
this.privileges = privileges;
}
}
Now, whent I try to save entity with filled elements - it's all fine - entity gets saved without problems. When i try to get entity from database, hibernate decides that he doesn't like my collection, an deletes it. sql from log:
Hibernate:
select
role0_.id as id1_6_,
role0_.version_num as version2_6_,
role0_.code as code3_6_,
role0_.comments as comments4_6_,
role0_.title as title5_6_,
role0_.title_en as title6_6_,
role0_.valid_from as valid7_6_,
role0_.valid_till as valid8_6_
from
rol_roles role0_
where
role0_.id=?
Hibernate:
select
privileges0_.role_id as role1_6_0_,
privileges0_.privilege as privileg2_7_0_,
from
rol_roles_privileges privileges0_
where
privileges0_.role_id=?
Hibernate:
delete
from
rol_roles_privileges
where
role_id=?
As collection is lazy, the privilege and delete statement goes on collection initialize. I tried adding @OrderColumn annotation as suggested in similar thread, but that didn't help. There are no insert statements as in similar cases, so reading object just swipes out collection. Table is created like this:
create table rol_roles_privileges (
role_id int8 not null,
privilege varchar(255) not null,
primary key (role_id, privilege)
);
Strangely (or maybe not) it works when I set fetchtype.EAGER - but shouldn't it work on lazy too?
I'm using hibernate 4.2.0.Final, SpringData, PostreSQL and hibernate.enable_lazy_load_no_trans
For future googlers: It seems hibernate 4.2 is buggy when using enable_lazy_load_no_trans. This bug might be connected with https://hibernate.atlassian.net/browse/HHH-7524
It was logging: log: ERROR AssertionFailure:43 - HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: collection owner not associated with session: org.hibernate.test.ondemandload.Store.inventories WARN AbstractPersistentCollection:246 - Unable to close temporary session used to load lazy collection associated to no session
I'm now using hibernate 4.1.7 and all works fine with lazy loading.
这篇关于hibernate枚举@elementcollection初始化后删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!