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.