-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Unusual "parent/child" relationship not covered in examples
PostPosted: Fri Nov 13, 2009 1:08 am 
Newbie

Joined: Fri Oct 10, 2008 11:04 pm
Posts: 6
Location: California
Okay. I have no trouble with most parent-child relationships. My code is filled with them and they work fine. But I have one that doesn't work because it's built differently, and none of the examples cover it. The case is where my parent and child are the same class, and hence use the same database table, like this:
Code:
CREATE TABLE `item` (
    `itemId` BIGINT NOT NULL AUTO_INCREMENT,
    `parentId` BIGINT,
    `itemName` VARCHAR(330) NOT NULL,
    CONSTRAINT `pk_item` PRIMARY KEY (`itemId`)
);
Notice that the parentId column is equal to the itemId of a different row in the same table.

So my Entity class looks like this:

Code:
@Entity
@Table(name = "item")
public class Item implements java.io.Serializable {

   private Long itemId;
   private Item parentItem;
   private String itemName;

   public Item() { }

   @Id
   @Column(name = "itemId", unique = true, nullable = false)
   public Long getItemId() {
      return this.itemId;
   }

   @ManyToOne(targetEntity = Item.class)
   @JoinColumn(name = "itemId", nullable = true)
   public Item getParentItem() {
      return this.parentItem;
   }

   public void setParentItem(Item parentItem) {
      this.parentItem = parentItem;
   }

   @Column(name = "itemName", nullable = false, length = 330)
   public String getItemName() { return this.itemName; }
   public void setItemName(String itemName) { this.itemName = itemName; }
}
If anybody can point me to an example that covers this case, I'd love to see it, because my code doesn't work. I get an exception that says this:
Code:
org.hibernate.MappingException: Repeated column in mapping for entity: com.evryx.db.Item column: itemId (should be mapped with insert="false" update="false")
This makes sense because I specify the itemId column in both the @Column annotation for the itemId property and the @JoinColumn annotation for the parent column. But that's the nature of the relationship. I don't want to declare the JoinColumn with insert="false" update="false" as it suggests, because that column is updatable. But even if I do, it ignores the data in the table and sets each parentId equal to the itemId for the same row.

Is this case covered by any of the examples? I can't find it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.