-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Indirect Relationship Mapping
PostPosted: Fri Aug 12, 2005 9:59 pm 
Beginner
Beginner

Joined: Mon Oct 11, 2004 12:30 pm
Posts: 21
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 HibernateDaoSupport
Code:
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 here
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 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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 12, 2005 11:50 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
check this -

http://forum.hibernate.org/viewtopic.ph ... ropertyref

Perhaps you can try out 3.1 beta and see if it's implemented there.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.