Using hibernate 3.0.5:
I have a problem using "identifier" id with a lazy associations mapped as a bag. The bidirectionnal association is mapped as usual : children side is a "bag" inverse lazy one-to-many association. parent side is a many-to-one association.
> Entity parent = session.load(2);
> Entity child = new Entity();
> child.setParent(parent);
> parent.getChildren.add(chlid);
> session.save(child);
> int childrenCount = parent.getChildren.size();
So, here childrenCount value is 2, which is incorrect. Note that no session.flush() has occured.
Using the same mapping, but using the "sequence" Id strategy, the value returned is childrenCount is 1, which is correct.
Is this a known problem or a bad programming practice?
What I see using a debugger is that using sequence, the saved object is not persisted when the collection is initialized, and the collection contains only the local addition. A contrario, with the identifier stategy, the saved object is inserted in the database in order to get its Id, and is found when the collection is initialized. With the local addition, the bag contains twice the saved object child.
Thanks for your attention.
Alexandre
|