I have the following problem. I cannot get id of the associated entity, its always null, but the toString method inserts correct id.
when I run hibernate with debug log level it maps correct parameters to queries when fetching the associated entity.
I get equal results when the bytecode optimization is true and false.
Where I am wrong? Some issues with proxying?
Please, help.
I've prepared the simple showcase.
The persistence object are as follows
Code:
public class FirstObject {
private SecondObject secondObject;
private Integer id;
public SecondObject getSecondObject() {
return secondObject;
}
public void setSecondObject(SecondObject secondObject) {
this.secondObject = secondObject;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String toString() {
return HibernateProxyHelper.getClassWithoutInitializingProxy(this)
.getSimpleName()
+ "-" + getId();
}
}
Code:
public class SecondObject {
private Integer id;
public final Integer getId() {
return id;
}
public final void setId(Integer id) {
this.id = id;
}
public String toString() {
return HibernateProxyHelper.getClassWithoutInitializingProxy(this)
.getSimpleName()
+ "-" + getId();
}
}
The mappingsCode:
<hibernate-mapping default-access="field">
<class name="FirstObject">
<id name="id" type="integer">
<column name="id" />
<generator class="native" />
</id>
<many-to-one name="secondObject"
class="SecondObject" column="so_id" unique="true"/>
</class>
<query name="getFirstObjectById.request">
SELECT fo FROM FirstObject fo WHERE fo.id=:id
</query>
<query name="getFirstObjectList.request">
SELECT fo FROM FirstObject fo
</query>
<query name="getFirstObjectCount.request">
SELECT count(*) FROM FirstObject
</query>
</hibernate-mapping>
Code:
<hibernate-mapping default-access="property">
<class name="SecondObject">
<id name="id" type="integer">
<column name="id" />
<generator class="native" />
</id>
</class>
<query name="getSecondObjectById.request">
SELECT so FROM SecondObject so WHERE so.id=:id
</query>
<query name="getSecondObjectList.request">
SELECT so FROM SecondObject so
</query>
<query name="getSecondObjectCount.request">
SELECT count(*) FROM FirstObject
</query>
</hibernate-mapping>
The piece of code to obtain dataCode:
....
for (FirstObject first : (List<FirstObject>) session
.getNamedQuery("getFirstObjectList.request").list()) {
System.out.println("fid=" + first.getId());
System.out.println("sid="
+ first.getSecondObject().getId());
System.out.println("first=" + first.toString());
System.out.println("second="
+ first.getSecondObject().toString());
System.out.println("sid="
+ first.getSecondObject().getId());
}
....
The output
Code:
.....
Hibernate: select firstobjec0_.id as id0_, firstobjec0_.so_id as so2_0_ from FirstObject firstobjec0_
fid=1
sid=null
first=FirstObject-1
Hibernate: select secondobje0_.id as id1_0_ from SecondObject secondobje0_ where secondobje0_.id=?
second=SecondObject-1
sid=null
.....
The example uses hibernate 3.2.6, mysql-connector-5.1.6, mysql-5.1.31 under WinXp
Do you have any thoughts on this?