I am somewhat stuck adding a FK constraint via hibernate and thus seeking help/feedback. At first the problem seemed simple: hibenate mappings listed below; the tables will be populated with data from a script, so needed FK to ensure data integrity. I planned to use hibernate only to read data into beans. The fields in the Java classes match columns in the tables.
<class name="FirstBean" table="FirstTable">
<id name="ContextId" />
[other fields…]
</class>
<class name="SecondBean" table="SecondTable">
<composite-id>
<key-property name="ContextId" />
<key-property name="ConceptTypeId" />
[ …]
</composite-id>
[other fields…]
<!-- add a fk contstraint for context id -->
<many-to-one name="ContextId" class="FirstBean" column="ContextId" insert="false" update="false" />
</class>
The database tables were created just fine with the FK in place. However, my unittests were failing with "ERROR BasicPropertyAccessor - expected type: int, actual value: ....FirstBean$$EnhancerByCGLIB$$746d0718
So, the hibernate tried to retrieve a proxy(?) to a FirstBean while populating SecondBean object. That was not my intent at all.. Ok, next I changed <many-to-one> directive to give it a new name
<many-to-one name="FKContextId" class="FirstBean" column="ContextId" insert="false" update="false" />
However, while the database was again created just fine, the tests failed because a “getter” for FKContextId was not found in a bean.
I finally added a required property to the bean and things worked:
private Object fkContextId;
public void setFKContextId(Object o){
this.fkContextId=o;
}
public Object getFKContextId(){
return this.fkContextId;
}
Yet, this pollutes bean object with junk... Is there any way to add a simple FK constraint??
Many thanks in advance for your help!
|