-->
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.  [ 5 posts ] 
Author Message
 Post subject: join in the same table
PostPosted: Thu Apr 13, 2006 1:20 pm 
Newbie

Joined: Thu Apr 13, 2006 10:52 am
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.1.0 beta 4

Full stack trace of any exception that occurs:
ERROR [PARSER] line 1:70: unexpected token: on

**************************************

Hi,

I'd like to know if there is a way to get objects using "join" with the same class. What I mean is: I want to get all the objects that don't have a corresponding object joined to it.

I need to know if there is an item in a list that is not in another list. I need to copy items form a list to another and I need to check if there is as inconsistency between the lists after some changes my user can do.

The SQL I'd use is:

select * from myTable t
left join mytable t2 on t2.myField = t.myField and t2.myGroup = :originalGroup
where
t.myGroup = :newGroup and t2.Id is null

thanks in advance.


Top
 Profile  
 
 Post subject: Re: join in the same table
PostPosted: Fri Apr 14, 2006 3:22 am 
Newbie

Joined: Sat Nov 22, 2003 9:21 pm
Posts: 18
Location: Malaysia
Hibernate recognises HQL not SQL.

Quote:
I want to get all the objects that don't have a corresponding object joined to it.

I would do this in HQL:
Code:
FROM myTable AS t
WHERE
t.fieldToMyTable IS NULL


And in regards to your second question. The SQL stament is weird:
- t2.Id is null? how does a table has a row that does not have an id? should this write t2.FK is null?

Anyway, I would do this instead:
Code:
FROM item AS i
WHERE
i.myGroup = :newGroup AND
i NOT EXISTS (
FROM item AS i2
WHERE
i2.myGroup = :myGroup
)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 14, 2006 9:31 pm 
Regular
Regular

Joined: Wed Jul 07, 2004 2:00 pm
Posts: 64
I can't help much without understanding the table structure more - what is the PK, and what columns for the FK for the join?

But anyways, if you can come up with the SQL SELECT that satisfies your requirement, Hibernate CAN do a query based on SQL. Just look at the examples for createSQLQuery in the Hibernate doc.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 17, 2006 8:23 am 
Newbie

Joined: Thu Apr 13, 2006 10:52 am
Posts: 3
Hi,

Is there a way to use my own join condition in replacemant of the conditions used by hibernate. For example, when I use the HQL "from tableA left join tableB where tableA.id = 10", the resulting SQL generated by hibernate is "select field1, field2 from tableA left join tableB ON A.PK = B.FK where tableA.id = 10", but I don't want to use this ON clause, but my own ON clause to join without using the correct PK and FK in a specific situation in my software.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 12:21 am 
Regular
Regular

Joined: Wed Jul 07, 2004 2:00 pm
Posts: 64
You could do something like:

SQLQuery query = session.createSQLQuery("select {p.*},{ph.*} from person p left outer join phone ph on ph.person_id=p.id and ph.active=1 where p.id=3");
query.addEntity("p", Person.class);
query.addEntity("ph", Phone.class);
List res = query.list();

The problem here is that the list res will not be the expected size. Say the above query should give 1 person with 2 phones. The res list will contain 2 entries, each being an Object[2]. res[0][0] and res[1][0] will both contain the same person, and res[0][1] and res[1][1] will contain the first and 2nd phone number.

A criteria-type query (which of course you can't use for custom joins) has support for result transformations that would help, but this hasn't been added yet for other query types, as far as I know.

But why not just use a regular join and just add extra stuff in the where clause?


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