Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access subclass properties in Criteria Builder JPA

 
Santhosh P K Kumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to use the JPA's 2.0 criteria query with the underlying implementation of Hibernate. I am having issues while trying to access properties of subclass in the where clause..

Following are brief description of the entities:

- Class A (which is a Entity & a Table by itself)
- Class B (which is a Entity & a Table by itself)
- Class A has OneToMany relationship with B
- Class AB & BA are child classes of B (Both are entities but are not mapped to any tables. It holds few properties specific to it.)
- Let us suppose both classes AB & BA has a property '''symbol'''

Now comes the criteria query:

CriteriaBuilder qb = futuresEntityManager.getCriteriaBuilder();
CriteriaQuery<A> query = qb.createQuery(A.class);
Root<A> a = query.from(A.class);
Join<A,B> b = a.joinSet("aLegs", JoinType.INNER);

Now when I form the predicates:

predicate=qb.and(qb.equal(a.get("id").get("accountId"), "1234"));
predicate=qb.and(predicate,b.get("symbol").in(input.getSymbol()));
----- The above line does not work at run time as b does not hold the reference to symbol. only child classes AB & BA holds it..

How do i access the properties of child classes in this query ? I have to Include both AB & BA for the symbol predicate.

Can anybody help me to resolve this issue ?
 
Alexey Saenko
Ranch Hand
Posts: 30
Chrome Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Santhosh,

take a look at stackoverflow, that problem seems to be quite similar to yours.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic