-->
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: Queries
PostPosted: Mon Mar 13, 2006 5:48 am 
Newbie

Joined: Mon Mar 13, 2006 5:31 am
Posts: 1
Hello!

I have two questions about Hibernate quering.

1) Suppose I have some entity "ent" with collections "col1" of "ent1", "col2" of "ent2". Both "ent1" and "ent2" have "name" attribute. Both are mapped as "lazy". So, I need to retrive "ent" where in "col1" entities restricted by some "name". I am using something like "from ent as e left join fetch e.col1 as e1 where e1.name = 'name1'", but
I need "col2" to be empty (and initialized). I wan't to use redudant join for col2 with restriction by unexistent name. How to do it easy?

2) Is there any way to cast inside HQL query to subclass?
Suppose I have entity "ent" and a collection "values" of entities of class BaseValue. TextValue and LangValue are subclasses of BaseValue. I want to do some queries like "from ent left join fetch ent.values as v where v.lang = 'en'", where "lang" is attribute of LangValue, and there is no such attribute in BaseValue.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 6:31 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
1) This isn't the sort of thing that hibernate was designed for, so doing it easily isn't easy (heh). If you load an object with a collection, hibernate is treating it as an object, not a result row with a dependent result set: while you can ignore the dependent result set when you're working with SQL, the java object unconditionally has that collection, it's part of the object. You're probably thinking too close to the DB if you want hibernate to return DB rows transformed into POJOs: hibernate wants to return POJOs that happened to have been persisted to a DB.

I don't believe that your example can be correctly modelled using "normal" HQL or criteria, but you can do it using the select new syntax:
Code:
select new Ent1(param1, param2, param3) from ent1 e where e.name=:Name
Hibernate won't fill in anything you don't want it to, when you're using the select new syntax.

2) I don't believe so, but you can get part way there using the .class feature:
Code:
from ent e
left join fetch e.values v
where v.class = 'LangValueDiscriminator'
Then do the rest in java. Obviously this doesn't work if you're using an inheritance technique that doesn't use discriminators (e.g. joined-subclass).


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.