Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Vladimir Kroupa
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic