Hi,
We are working with Hebrnate 3.6 (Java 6, Weblogic 10.3.4, Oracle 11). We have the following entities in our system: BaseUser, which have eager fetched pockets collection (OneToMany with mappedBy on pockets -pocket has FK and ManyToOne reference to consumer). Pocket entity have eager fetched ElementsCollections which contains pocket locks - map of the lock types and the locks details.
The problem: When the BaseUser (specifically a consumer which extends BaseUser) is read from DB, it is read with it's pockets and their locks. If we have one consumer with one pocket, having 2 locks - then thequery returns 1*2*2 results - and the consumer pockets list is initialized with 2(!) pockets instead of one. Each pocket is brought several times - according to the number of locks it has. As a workaround we changed the locks to be read lazily and this solved the problem, but this cause other problems for us.
The entities definitions are below:
@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class BaseUser extends ModelEntity { @OneToMany(mappedBy="user",fetch=FetchType.EAGER) private final Collection<Pocket> pockets = new ArrayList<Pocket>();
@Entity @Table(name="POCKETS") @Inheritance(strategy=InheritanceType.SINGLE_TABLE) public abstract class Pocket extends ModelEntity {
@ManyToOne(fetch=FetchType.LAZY) protected BaseUser user;
@ElementCollection(fetch=FetchType.Eager) @CollectionTable(name="locks",joinColumns={@JoinColumn(name="pocket_id")}) @MapKeyEnumerated(EnumType.STRING) @MapKeyColumn(name="lockType") private Map<LockType,Lock> locks;
The query that is executed when the locks are read eagerly is: /* load trivnet.core.model.consumer.Consumer */ select consumer0_.id as id8_1_, consumer0_.version as version8_1_, pockets1_.user_id as user33_8_3_, pockets1_.id as id3_, pockets1_.id as id5_0_, pockets1_.version as version5_0_, pockets1_.dtype as dtype5_0_, locks2_.pocket_id as pocket6_5_4_, locks2_.lock_reason_time_map as lock2_4_, locks2_.locked as locked4_, locks2_.lock_type as lock4_4_ from ( select id, version, from consumers ) consumer0_ left outer join pockets pockets1_ on consumer0_.id=pockets1_.user_id left outer join locks locks2_ on pockets1_.id=locks2_.pocket_id where consumer0_.id=?
When changing the locks to be read lazily the query that is exceuted is : /* load trivnet.core.model.consumer.Consumer */ select consumer0_.id as id16_1_, consumer0_.version as version16_1_, pockets1_.user_id as user33_16_3_, pockets1_.id as id3_, pockets1_.id as id1_0_, pockets1_.version as version1_0_, pockets1_.dtype as dtype1_0_ from ( select id, version from consumers ) consumer0_ left outer join pockets pockets1_ on consumer0_.id=pockets1_.user_id where consumer0_.id=?
and then an extra query for the locks.
Is anyone familaire with this problem? should I opren a bug for it?
thanks, Tovi
|