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.