-->
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.  [ 2 posts ] 
Author Message
 Post subject: One to Many self-referential relationship and @Id
PostPosted: Tue Jan 20, 2009 1:18 pm 
Newbie

Joined: Tue Jul 22, 2008 5:11 pm
Posts: 16
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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 20, 2009 5:13 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
If the parent's "ID" has to be generated outside the databse (questionable by itself) maybe you should treat it as some kind of natural key and not ID itself.

As parent and child are of the same type of Object they should be in the same table (my opinion), so the "real" id can be generated by the DB.

Save the application-side ID in an extra field and leave it set to null for child objects.

What i find a little strange about your mapping is that you use the CHILD_OBJECT_ID to reference child records.
Usually such a field would be called PARENT_OBJECT_ID so many clients could have the same parent
Typo when making up the example?


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

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.