This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Hibernate - id column of an entity as list-index column in another entity? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate - id column of an entity as list-index column in another entity?" Watch "Hibernate - id column of an entity as list-index column in another entity?" New topic
Author

Hibernate - id column of an entity as list-index column in another entity?

Vladimir Kroupa
Greenhorn

Joined: Dec 26, 2008
Posts: 28
Hi,
I'm writing a Hibernate mapping file for my class Order.java. I'm trying to map a List collection that stores OrderItems and I would like to use id column of the OrderItem entity as the list-index column in the Order entity.

Order.hbm.xml:


OrderItem.hbm.xml:


I'm getting the following error:


I had already searched the board and I've found that I need to set the column that is mapped repeatedly as read-only, but I was unable to find how this is done in the reference documentation for Hibernate.

Thanks for any help.
Vladimir
Angel Taveras
Ranch Hand

Joined: Nov 13, 2008
Posts: 84

Hello Vladimir, you have an error on your mapping files. Hibernate complains on a repeated column on the table OrderItem. The problem it's not originated on this mapping but rather in the other one. You're using the same column name for the index in the list and on the id of the OrderItem identity. These columns must be different, they are not the same. The list-index it's used to save the index of the java.util.List index not the id of the OrderItem.

To solve this you only have to rename the list-index column to orderitem_id_idx or something like it.

Regards,
Vladimir Kroupa
Greenhorn

Joined: Dec 26, 2008
Posts: 28
Thank you very much for the quick explanation! I thought that Hibernate uses list-index to figure out ordering of the elements, not to store their indeces.

Thanks again.

Vladimir
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Hibernate - id column of an entity as list-index column in another entity?
 
Similar Threads
1-to-M mapping confusion
Hibernate - Row was updated or deleted by another transaction..
problem saving with foreign key error
Mapping a qualified association with Hibernate
Null Value in @JoinColumn