Issue with self reference on ManyToOne relationship
592198Sep 19 2007 — edited Apr 14 2008Hi,
I have a scenario where one of the columns on a table refers to the ID of the same table for a @ManyToOne relationship. Here is an example class below to better explain the situation:
@Entity
@Table(name = "DEPARTMENT")
public class Department implements java.io.Serializable
{
@Id
private int ID;
@ManyToOne(targetEntity = Department.class, fetch = FetchType.EAGER)
@JoinColumn(name = "PARENT_ID")
private Department parent;
@OneToMany(mappedBy = "parent", targetEntity = Department.class, fetch = FetchType.LAZY)
private Set<Category> children = new HashSet<Category>(0);
}
Here if you notice the parent is the ID of another Department. When I use this during a create it seems to be trying to update the ID of an existing Department record, which is causing an exception as follows:
SEVERE: Error processing request from 127.0.0.1 for <anon>: javax.persistence.RollbackException: Exception [TOPLINK-7251] (Oracle TopLink Essentials - 2.0 (Build b41d-beta2 (04/24/2007))): oracle.toplink.essentials.exceptions.ValidationException
Exception Description: The attribute [ID] of class [Department] is mapped to a primary key column in the database. Updates are not allowed.
Any thoughts on what I might be doing wrong. Appreciate your help.
Thanks,
Sharanya