-->
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.  [ 6 posts ] 
Author Message
 Post subject: formula question
PostPosted: Fri Jan 13, 2006 6:30 am 
Newbie

Joined: Tue Nov 11, 2003 4:22 am
Posts: 2
Hello, all!

I have entity А with two foreign keys (FK) from Y, X.
There is also entity B, also with FKs from Y,X.
Database scema can't be changed.
I need a connection from A to B based on a condition that X and Y are equial in both A and B. I mean, i need to have B in A where a.x=b.x and a.y=b.y

I tried to do that using formula element in mapping:
<class name="A" table="a">
<property name='x' class='X' />
<property name='y' class='Y' />
<one-to-one name="b" class="B" formula="from B b where b.x=x and b.y=y" />
</class>
and
<class name="A" table="a">
<property name='x' class='X' />
<property name='y' class='Y' />
<one-to-one name="b" class="B" >
<formula>id</formula>
<formula>(select b.id from B where b.x=x and b.y=y)</formula>
</one-to-one>
</class>

In both cases I got errors when tried to load A. What's wrong with my formulas? Any help will be appretiated.

Regards, Andrey Grishin.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 29, 2006 7:17 pm 
Beginner
Beginner

Joined: Sun Jan 22, 2006 8:47 pm
Posts: 31
try to use sql instead of hql..Use table names and field names in place of bean class and its properties..


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 29, 2006 7:33 pm 
Beginner
Beginner

Joined: Sun Jan 22, 2006 8:47 pm
Posts: 31
try to use sql instead of hql..Use table names and field names in place of bean class and its properties..


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 29, 2006 7:33 pm 
Beginner
Beginner

Joined: Sun Jan 22, 2006 8:47 pm
Posts: 31
try to use sql instead of hql..Use table names and field names in place of bean class and its properties..


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 29, 2006 8:11 pm 
Beginner
Beginner

Joined: Sun Jan 22, 2006 8:47 pm
Posts: 31
try to use sql instead of hql..Use table names and field names in place of bean class and its properties..


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 29, 2006 9:04 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The formula element/attribute uses plain SQL, not HQL, so your first example won't work.

one-to-one uses primary key (id) properties, and as you can't use that, you must use many-to-one unique="true".

Are X and Y mapped? If they're just legacy tables that aren't being mapped, then you can just use a straightforward many-to-one unique="true" property-ref="xid" or whatever (though you'll need to "merge" the xid and yid columns into one hibernate property using the properties element; see section 5.1.14 of the ref docs).

If X and Y are mapped, then you don't need to model the relationship between A and B. Use queries to get corresponding items:
Code:
select a, b from A a join a.X x join x.Y y join y.B b

Obviously that query assumes that all relationships are bidirectional, but equivalents can be found if any of the relationships are unidirectional.


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