hibernate enum @elementcollection deleted after in

2019-05-27 12:41发布

问题:

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

回答1:

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.