Hi!
I'm trying to do an one-to-one mapping where the primary key of one entity will be composed of two foreign properties and two local fields...and in the other entity I want that the identifier will be the same as mapped entity..how can I do that?...I will provided a example of the code...I'm using XML mapping
The two entities:
Code:
public interface IResidue {
public Chain getChain();
public int getNumber();
public Character getInsCode();
}
public class Air {
IResidue residue
//getter's and setter's for residue
...
}
The entity Air has no own ID, I'm trying to import the ID of IResidue to Air..: that should be the ID of Chain entity: pdb_id,chain_id plus the properties: number,insCode.
here is my XML mapping (removed unimportant code):
Code:
<hibernate-mapping package="br.embrapa.cnptia.cbi.stinglib.descriptors.air">
<class name="Air" table="Air">
<composite-id name="residue">
<key-many-to-one name="chain" class="br.embrapa.cnptia.cbi.stinglib.core.Chain">
<column name="pdb_id"/>
<column name="chain_id"/>
</key-many-to-one>
<key-property name="number" />
<key-property name="insCode" >
<column name="icode" sql-type="varchar(1)"/>
</key-property>
<generator class="foreign">
<param name="property">residue</param>
</generator>
</composite-id>
<one-to-one name="residue" class="br.embrapa.cnptia.cbi.stinglib.core.IResidue" />
</class>
</hibernate-mapping>
<hibernate-mapping package="br.embrapa.cnptia.cbi.stinglib.core">
<class name="IResidue" table="Residues">
<composite-id>
<key-many-to-one name="chain" class="Chain">
<column name="pdb_id"/>
<column name="chain_id"/>
</key-many-to-one>
<key-property name="number" />
<key-property name="insCode" >
<column name="icode" sql-type="varchar(1)"/>
</key-property>
</composite-id>
<many-to-one name="chain" class="Chain" insert="false"
update="false">
<column name="pdb_id" />
<column name="chain_id" />
</many-to-one>
<property name="name" type="string"/>
</class>
<!-- join-subclasses -->
<join-subclass ...
.
.
.
</join-subclass>
</hibernate-mapping>
This mapping is not throwing any exception and database tables are correctly created, but when a try to to a HQL like
"from Air air where air.residue.name = 'AAA'"
it does not work: Exception in thread "main" org.hibernate.QueryException: could not resolve property: residue.name of: br.embrapa.cnptia.cbi.stinglib.descriptors.air.Air [from br.embrapa.cnptia.cbi.stinglib.descriptors.air.Air air where air.residue.name = 'ALA']
at org.hibernate.QueryException.generateQueryException(QueryException.java:137)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1796)
at br.embrapa.cnptia.cbi.stingcalc.main.StingCalc.main(StingCalc.java:398)
but if I do:
"from Air air where air.residue.number = 1" it works....
I guess that the composite-id of class Air is working correctly, but I can't access fields from IResidue due incorrectly use of composite-id..
How can I do this mapping works???
thanks.