-->
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.  [ 3 posts ] 
Author Message
 Post subject: FK property on child class in many-to-one
PostPosted: Tue May 13, 2008 9:34 am 
Newbie

Joined: Tue May 13, 2008 7:46 am
Posts: 2
I have two classes, Parent and Child:

Code:
public class Parent {
  private Integer parentId;
  private List<Child> children;
 
  public Integer getParentId(){
    return this.parentId;
  }

  public void setParentId(Integer parentId){
    this.parentId = parentId;
  }

  public List<Child> getChildren() {
    return this.children;
  }

  public void setChildren(List<Child> children) {
    this.children = children;
  }

  public Parent(){

  }

}


public class Child{

  private Integer childId;
  private Parent parent;
  private Integer parent_id;

  public Integer getChildId(){
    return this.childId;
  }

  public void setChildId(Integer childId){
    this.childId = childId;
  }

  public Parent getParent() {
    return this.parent;
  }

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

  public Integer getParentId(){
    return this.parentId;
  }

  public void setParentId(Integer parentId){
    this.parentId = parentId;
  }

  public Child(){

  }

}


My hibernate mappings look like:

Code:
<hibernate-mapping>
    <class name="Parent" table="parent">
        <id name="parentId" type="java.lang.Integer">
            <column name="parent_id" />
            <generator class="assigned" />
        </id>
        <bag name="children" inverse="true">
            <key>
                <column name="parent_id" not-null="true" />
            </key>
            <one-to-many class="Child" />
        </bag>
    </class>
</hibernate-mapping>

<hibernate-mapping>
    <class name="Child" table="child">
        <id name="childId" type="java.lang.Integer">
            <column name="child_id" />
            <generator class="assigned" />
        </id>
        <many-to-one name="parent" class="Parent" fetch="select">
            <column name="parent_id" not-null="true" />
        </many-to-one>
    </class>
</hibernate-mapping>


The saving/updating of both Parent and Child objects works great. So does the fetching of both objects, except for the population of the foreign key property “parentId” on the Child class. There are situations in which I have an instance of Child class without the “parent” property initialized (lazy-loading), however I would like to have the “parentId”. The parent_id is a column on my child table, so it should be relatively simple to have access to that property without having to load the entire Parent object. However, after searching the Hibernate documentation and forums, I can’t seem to find a way to instruct Hibernate to do this.

Thanks for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 13, 2008 3:53 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Map the column again to a single value property with insert/update=false:

Code:
    <class name="Child" table="child">
        <id name="childId" type="java.lang.Integer">
            <column name="child_id" />
            <generator class="assigned" />
        </id>
        <many-to-one name="parent" class="Parent" fetch="select">
            <column name="parent_id" not-null="true" />
        </many-to-one>
        <property name="parentId" column="parent_id insert="false" update="false" />
    </class>


Sorry I can't bold inside a code block :(

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 14, 2008 2:01 pm 
Newbie

Joined: Tue May 13, 2008 7:46 am
Posts: 2
Thanks! Exactly what I was looking for.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.