I am using hibernate and jpa to store an object. I have a class that has a OneToMany relationship with itself. Basically the Main Object contains objects of the same type in a Map.
An example class of what I mean (if I am not being clear):
Code:
@Entity
public class ExampleObject {
Map<String, ExampleObject> childObjects = new HashMap<String, ExampleObject>();
Integer id;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "EXAMPLE_OBJECT_ID", unique = true, nullable = false, precision = 22, scale = 0)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@OneToMany(cascade = CascadeType.ALL, targetEntity = ExampleObject.class, fetch = FetchType.EAGER)
@JoinColumn(name = "CHILD_OBJECT_ID", referencedColumnName = "EXAMPLE_OBJECT_ID")
public Map<String, ExampleObject> getChildObjects() {
return childObjects;
}
public void setChildObjects(Map<String, ExampleObject> childObjects) {
this.childObjects = childObjects;
}
}
The problem I have is that the Main (Parent) Object's ID is generated outside of the Database, while I want the Children Objects to have an ID generated by the database.
Right now I just have both the Parent and Child objects in the same database table with IDs generated by the database for Parent and Child objects, but this is not ideal.
Ideally I would have separate tables with an ID assigned outside of the database for the parent table, and an ID assigned inside the database for the child table.
Any ideas? I am open to using Annotations and XML.
Note: This is an existing class being used all over an existing system, so I cannot just create new classes. Persisting of instances of this class is being "shoehorned" into the system.