-->
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: Hibernate many-to-one save question
PostPosted: Thu Sep 20, 2007 12:44 pm 
Newbie

Joined: Thu Aug 30, 2007 5:19 pm
Posts: 5
I need to implement a function

private void saveChild(Integer parentId) {
//save a child object to the DB that has Child.parent.id = parentId;
}

As far as I know, the way to do this is to create the Child and Parent objects, and then to call the Session.save() function, passing in the child object.

Below is a sample mapping file for the Child and Parent class.

This wouldn't be a problem except that in my real life example, Parent class has a many to one relationship to a GrandParent table and so on and so forth.

So it seems I only have two choices:

1. Manually create the whole chain of objects related to this mapping, which is huge.
2. Query for a Parent object using the passed in parentId. Then set Child.parent to this queried Parent object

There must be a better way...is there?

Code:
<class name = "Child" table = "Child_Table">
    <many-to-one name="parent" column="PARENT_ID" class="Parent" not-null="true"/>
</class>
<class name = "Parent" table = "Parent_Table">
    <id name="parentId" column="PARENT_ID">
        <generator class="native"/>
    </id>
</class>

class Child {
    private Parent parent;

    public getParent() {
        return parent;
    }

    public setParent(Parent parent) {
        this.parent = parent;
    }
}

class Parent {
    private Integer parentId;
}

CREATE TABLE Child (
  PARENT_ID number

  CONSTRAINT fk_parentId
  FOREIGN KEY PARENT_ID
  REFERENCES PARENT (PARENT_ID))
)

CREATE TABLE PARENT {
  PARENT_ID number
}


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.