-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: table a inner join tableb inner join table C
PostPosted: Thu May 01, 2008 9:50 am 
Beginner
Beginner

Joined: Wed Oct 03, 2007 9:46 pm
Posts: 37
i need a complex inner join like this --> select * from tableA inner join table B on tableA.field1 = tableB.field1 inner join tableC on tableB.field2 = tableC.field2 . how to create the criteria statement for tableC ?

for tableA and tableB, i use sess.createCriteria(TableA.class, tableA).createAlias("tableB","tableB") and working fine


but now, i have statement become complicated, when i have table C
that can onlly join with table B with field call "field2" and my tableA doesn't have any field to join with table C.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 02, 2008 7:16 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Have you tried it with subcriterias instead of aliases ? There you get a new criteria back, on which you should be able to add the join to table C.

Code:
sess.createCriteria(TableA.class, "tableA")
.createCriteria("tableB","tableB")
.createCriteria("tableC", "tableC")


This is the NHibernate forum and looking at your sample code I assume, you're are using the java version of hibernate, but the criteria stuff works in the same way, so I hope this helps.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: cannot work
PostPosted: Fri May 02, 2008 9:12 am 
Beginner
Beginner

Joined: Wed Oct 03, 2007 9:46 pm
Posts: 37
i tried your way , cannot work.

tableC doesnt show up in the sql query in debugging mode


for your info, only in tableB, i have xml that map to tableC . tableA xml doesnt map to table C. is that the cause? there is no field in tableA that able to join with table C. only table b can join with table c


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 02, 2008 10:15 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Can you post the mappings ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: mapping
PostPosted: Fri May 02, 2008 10:41 am 
Beginner
Beginner

Joined: Wed Oct 03, 2007 9:46 pm
Posts: 37
in table B (table B and table C, join with field2)
---------------

<many-to-one
name="TableC"
column="field2" insert="false" update="false"
/>



in table A (table A and table B, join with field1)
---------


<many-to-one
name="TableB"
column="field1" insert="false" update="false"
/>


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 02, 2008 10:48 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Do you have some expressions on the subcriteria for tableC ? Like

Code:
sess.createCriteria(TableA.class, "tableA")
.createCriteria("tableB","tableB")
     .createCriteria("tableC", "tableC")
          .Add(Expression.Eq("tableC.xyz", xyz)


Something like that should create a query over all 3 tables. Can you post your complete criteria ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: criteria
PostPosted: Fri May 02, 2008 11:14 am 
Beginner
Beginner

Joined: Wed Oct 03, 2007 9:46 pm
Posts: 37
crit =sess.createCriteria(tableA.class,"tableA")
.createCriteria("tableB", "tableB")
.createCriteria("tableC","tableC")

.createAlias("tableD","tableD")

.add( Restrictions.like("tableC.field5","ghost"))





--------

this is a complete criteria

in my table A mapping, i map to table B and table D
in my table B mapping, i map to table C


still, on sql debug mode, i dont see tableC show up while, table A, B and D show up.
is it "a must" for table A to map to table C in xml ? i dont have any field in table A that able to map to table C. only table B can map to table C.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 02, 2008 11:27 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
It seems strange that .createAlias("tableD","tableD") works at all on the tableC subcriteria, since that does not have a link to tableD. But maybe hibernate walks up the criteria tree.
I'm not sure, what goes wrong ... basically this should work. Try an easier version:

Code:
crit =sess.createCriteria(tableA.class,"tableA")
   .createCriteria("tableB", "tableB")
      .createCriteria("tableC","tableC")
         .add( Restrictions.like("tableC.field5","ghost"));

Quote:
is it "a must" for table A to map to table C in xml ? i dont have any field in table A that able to map to table C. only table B can map to table C

Definitely not, this should work.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: hmm
PostPosted: Fri May 02, 2008 11:31 am 
Beginner
Beginner

Joined: Wed Oct 03, 2007 9:46 pm
Posts: 37
sorry.

in my table A xml mapping, i map to both table B and table D. i mentioned that in the last post. u u didnt noticed.


inside tableA xml
-----

<many-to-one
name="tableB"
column="field2" insert="false" update="false"
/>


<many-to-one
name="tableD"
column="field4" insert="false" update="false"
/>


Top
 Profile  
 
 Post subject: question
PostPosted: Fri May 02, 2008 11:36 am 
Beginner
Beginner

Joined: Wed Oct 03, 2007 9:46 pm
Posts: 37
to use

.createCriteria("tableC","tableC")


do i need to create tableC pojo object inside tableA pojo object?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 02, 2008 11:37 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
That's not what I'm wondering about.

Code:
crit =sess.createCriteria(tableA.class,"tableA")
    .createCriteria("tableB", "tableB")    // new ICriteria instance for tableB
        .createCriteria("tableC","tableC") // new ICriteria instance for tableC

        .createAlias("tableD","tableD")


You're calling createAlias on a new ICriteria instance that was created for tableC. But tableC has no link to tableD or does it ? But maybe hibernate really walks up the criteria tree here.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: =)
PostPosted: Fri May 02, 2008 11:49 am 
Beginner
Beginner

Joined: Wed Oct 03, 2007 9:46 pm
Posts: 37
table C, doesnt have any mapping to any other table.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 02, 2008 11:51 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Have you tried the easier criteria without the tableD link ?

Code:
crit =sess.createCriteria(tableA.class,"tableA")
   .createCriteria("tableB", "tableB")
      .createCriteria("tableC","tableC")
         .add( Restrictions.like("tableC.field5","ghost"));

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: tried
PostPosted: Fri May 02, 2008 12:06 pm 
Beginner
Beginner

Joined: Wed Oct 03, 2007 9:46 pm
Posts: 37
i get this error

could not resolve property: tableC of: com.health.entity.tableA


Top
 Profile  
 
 Post subject: hmm
PostPosted: Fri May 02, 2008 12:10 pm 
Beginner
Beginner

Joined: Wed Oct 03, 2007 9:46 pm
Posts: 37
i already added object tableC inside tableA pojo. but still the same. that error indicate there is no xml mapping in tableA to tableC.


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

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.