Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
3.0.5
Mapping documents:
Entity.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.myco.model.Entity" table="Entity">
<id column="id" name="id" unsaved-value="0"/>
<set name="stringAttributes" table="AttributeRelationship" lazy="false">
<key column="entityid"/>
<one-to-many class="com.myco.model.StringAttribute"/>
</set>
</class>
</hibernate-mapping>
StringAttribute.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.myco.model.StringAttribute" table="AttributeRelationship">
<id column="id" name="id" unsaved-value="0"/>
<property name="insertionDate" column="insertionDate"/>
<property name="startDate" column="startDate"/>
<property name="stopDate" column="stopDate"/>
<many-to-one name="attributeClass" class="com.myco.model.schema.AttributeClass" lazy="false">
<column name="attributeclassid"/>
</many-to-one>
<join table="StringAttribute" >
<key column="id" property-ref="valueid"/>
<property name="value" column="value"/>
</join>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():using Spring's HibernateDaoSupportCode:
public Entity getEntityById(long id) {
return (Entity) getHibernateTemplate().get(Entity.class, new Long(id));
}
Full stack trace of any exception that occurs:No Exception
Name and version of the database you are using:Hsqldb 1.8.0
The generated SQL (show_sql=true):The one i'm most interested in is hereselect stringattr0_.entityid as entityid1_, stringattr0_.id as id1_, stringattr0_.id as id0_, stringattr0_.insertionDate as insertio2_2_0_, stringattr0_.startDate as startDate2_0_, stringattr0_.stopDate as stopDate2_0_, stringattr0_.attributeclassid as attribut5_2_0_, stringattr0_1_.value as value3_0_ from AttributeRelationship stringattr0_ inner join StringAttribute stringattr0_1_ on stringattr0_.id=stringattr0_1_.id where stringattr0_.entityid=?
What i'm trying to do here is take these three tables:
Code:
Entity:
+---------------+--------------+
| Field | Type |
+---------------+--------------+
| ID | bigint(20) |
| NAME | varchar(255) |
| INSERTIONDATE | date |
| MODIFIEDDATE | date |
+---------------+--------------+
AttributeRelationship:
+------------------+------------+
| Field | Type |
+------------------+------------+
| ID | bigint(20) |
| VALUEID | bigint(20) |
| INSERTIONDATE | date |
| ENTITYID | bigint(20) |
| ATTRIBUTECLASSID | bigint(20) |
| STARTDATE | datetime |
| STOPDATE | datetime |
+------------------+------------+
StringAttribute:
+-------+--------------+
| Field | Type |
+-------+--------------+
| ID | bigint(20) |
| VALUE | varchar(255) |
+-------+--------------+
and represent them as an Entity object with a set of StringAttributes. The ENTITYID column of AttributeRelationship provides the foreign key to ENTITY, and our legacy datamodel has this AttributeRelationship table that provides the join point for 3 attribute types (String/Integer/Date), and they are based off a join with AttributeRelationship.valueid=StringAttribute.id (or IntegerAttribute.id/DateAttribute.id)
As you can see in the generated sql:
Code:
select stringattr0_.entityid as entityid1_, stringattr0_.id as id1_, stringattr0_.id as id0_, stringattr0_.insertionDate as insertio2_2_0_, stringattr0_.startDate as startDate2_0_, stringattr0_.stopDate as stopDate2_0_, stringattr0_.attributeclassid as attribut5_2_0_, stringattr0_1_.value as value3_0_ from AttributeRelationship stringattr0_ inner join StringAttribute stringattr0_1_ on [b]stringattr0_.id=stringattr0_1_.id[/b] where stringattr0_.entityid=?
the join is trying to use both id columns. I've tried a few combinations, but i can't figure out how to get "from AttributeRelationship stringattr0_ inner join StringAttribute stringattr0_1_ on stringattr0_.
valueid=stringattr0_1_.id where stringattr0_.entityid=?"
In the end, i'd really like to have a join-subclass going on with the AttributeRelationship table being the mapped (abstract) superclass for these three.... but i guess one step at a time.
Can anyone give me some pointers? I realize this is a "less than optimal" database design.
Thanks in advance,
Jason