-->
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: Criteria API problem
PostPosted: Thu Oct 26, 2006 6:03 am 
Newbie

Joined: Mon Nov 21, 2005 6:15 am
Posts: 14
Hibernate Version: 3.2.0-cr5

I have an Inheritance Hierarchy with classes B, C and D subclassing a baseclass A. (A is abstract).

Code:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type",  discriminatorType=DiscriminatorType.STRING
)
@DiscriminatorValue("none")
public abstract class A {


I had a built a search screen on entity A, the underlying java code was something like,
Code:
Criteria myCriteria = session.createCriteria(A.class);

//take each and every field from the UI
//add it to myCriteria


Now In the UI, I am also presenting the user to search only classes of type B or C or D (Note that there could be multiple choices from user)

If user checks on B and C then I will have to search only those classes. Class A has a discriminator column at the table level. But if I filter based on that using Hibernate criteria API, it is giving nested exception is org.hibernate.QueryException: could not resolve property: type of: com.A

And the session.createCriteria API takes only one class. Can someone suggest some solution for this?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 6:22 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Use the "class" special property. So for example, if you were to refer to filter entries in class B,
Code:
from com.A a where a.class = com.B and <your other filters>


Top
 Profile  
 
 Post subject: "class" property works
PostPosted: Thu Oct 26, 2006 7:12 am 
Newbie

Joined: Mon Nov 21, 2005 6:15 am
Posts: 14
The "class" property works. However, there is one minor change. I had been using the discriminator value for subclasses. The "class" property when used as a criteria boils down to the following in SQL,

from A this_ where this_.[discriminator-column] = [some value]

So, in case you have a discriminator column for B as "valueb" then you must build criteria API like

Code:
criteria.add(Restrictions.eq("class", "valueb"));


Note the value is "valueb" and not the class itself since we had the discriminator value. (when no discriminator value is declared in hibernate annotations/mapping files then the class name becomes the discriminator value)


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.