Id of an entity is not set when it is a proxy in a collection

Description

In plain English:

With three entities, Item, Comment and Category such that

Item has multiple Comment (one to many)
Comment has one or no Category (many to one)

have one item with one comment that is linked to one category. Load the item, read the category's id. It's not being set by Hibernate.

In unit test (fails at the very last assertNotSame):

// creating an item
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

Item item = new Item();

Comment comment = new Comment();
comment.item = item;
item.comments.add(comment);

comment.category = new Category();

session.save(item);
session.save(comment.category);

assertNotSame("after save, category's id must not be 0",
0, comment.category.id);

tx.commit();
session.close();

// load the item and read the rating's id
session = sessionFactory.openSession();

item = (Item) session.get(Item.class, item.id);
assertNotSame("after load, category's id must not be 0",
0, item.comments.iterator().next().category.id);

session.close();

Attachments

1

Activity

PascalLJuly 8, 2008 at 1:55 AM

This is not a bug, but rather a feature improvement:

If the getId() method is final, the proxy cannot override it. Hibernate should output a warning message/crash when it notices that.

Details

Assignee

Reporter

Components

Priority

Created June 24, 2008 at 6:26 AM
Updated December 19, 2013 at 9:58 PM