-->
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.  [ 3 posts ] 
Author Message
 Post subject: any-to-many side of an any relationship
PostPosted: Fri Dec 02, 2005 4:33 pm 
Newbie

Joined: Fri Dec 02, 2005 1:21 pm
Posts: 2
Frankly speaking, I am running out ideas on how to get it work and I need your help please!!!!


is there a way to describe an association between any two tables where there are no foreign constraints at all in a hibernate mapping file.

Let me give you an example:

We have three tables: table1, table2, table3.
Table 1 and 2 have only a primary key column
Table3 has three columns: pk, relatedTableName, relatedObjPk

Table1 has following items:

PK
1
2

Table2 has following items:

PK
1
2
3

Table3 contains records from Table1 and Table2:

PK RelatedTableName RelatedObjID
1 Table1 1
2 Table1 2
3 Table2 1
3 Table2 2

We have three concrete classes Entity1, Entity2, and Entity3 for each of the Tables.

class Entity1
{
Long pk;

Collection<Entity3> entity3; // how to map this collection
}

class Entity2
{
Long pk;

Collection<Entity3> entity3; // how to map this collection
}

class Entity3
{
Long pk;
String relatedEntityName;
Long relatedEntityPk;

Object relatedObj; // this can be mapped using <any>
}

We want to know how to map the collections in Entity1 and 2 in hibernate mapping file so that we can use HQL to traverse the relationships.

Jianping


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 03, 2005 3:56 am 
Newbie

Joined: Wed Nov 30, 2005 4:13 pm
Posts: 12
can't you use many-to-one tag and formula ?

<hibernate-mapping>
<class name="entity1" table="table1">
<id name="pk" />
<many-to-one
name="meInTab3"
property-ref="meInTab3">
<column name="pk"/>
<formula>'table1'</formula>
</many-to-one>
</class>
<class name="entity3" table="table3" >
<id name="pk" />
<properties name="meInTab3">
<property name="relatedObjPk" />
<property name="relatedTableName" />
</properties>
</class>
</hibernate-mapping>

I have read that it will produce sql like:

select t1.pk, t3.pk from table1 t1, tbale3 t3 where t3.relatedObjPk=t1.pk and p.relatedTableName='table1'


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 07, 2005 1:46 pm 
Newbie

Joined: Fri Dec 02, 2005 1:21 pm
Posts: 2
Well, I had a couple of mappings before, the problem is with the foreign keys. So the solution is not use hbm2ddl.auto at all to prevent hibernate from creating foreign keys like:

alter table table3 add constraint fk_t3_to_t2 foreign key (RelatedObjID) references table2(pk);

alter table table3 add constraint fk_t3_to_t1 foreign key (RelatedObjID) references table1(pk);

Mapping 1:

<class name="Entity1" table="table1">
<id name="pk" type="java.lang.Long" unsaved-value="null">
<column name="pk" sql-type="BIGINT"/>
<generator class="assigned">
</generator>
</id>
<set name="entities3"
lazy="false"
fetch="select"
inverse="true"
where="tablename = 'table1')">
<key>
<column name="relatedCol" sql-type="BIGINT"/>
</key>
<one-to-many class="Entity3"/>
</set>
</class>

Entity2 is very similar to Entity1.

Mapping 2:

Use collection loader. See an example from tests of hibernate download.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.