0

Lets say that class 'X' is mapped to table 'X' class 'A' is mapped to Table 'A' and Class 'B is mapped to table 'B'.

Table X Structure:(X_ID, some other columns Table A Structure:(A_Id,X_Id, some other columns) Table B Structure:(A_Id, some other columns)...Table B also has A_Id

Class 'B' extends class 'A'. We have the mapping files for both of them as:

Class 'A' Parent Mapping file:

@Entity
@Table(name = 'A')
@Inheritance(stratergy=InheritanceType.Joined)
public abstract class A {
@Id @Clumns(name = "A_Id)
@GeneratedValue
protected Long aId;
-- some more A specific fields
}

Class 'B' Mapping file:

@Entity
@Table(name= 'B')
Public class B extends A{
---- B specific fields
}

Now, I have a SQL Query as below that I need to write using hibernate criteria API.

select * from X
INNER JOIN A 
ON X.id = A.id
INNER JOIN B
ON A.id = B.id
where B.name = 'XYZ'
   and B.Sex = 'M'

I have come up with:

Criteria c = session.createCriteria(x.class, "x");
                    .createAlias("x.a", "a")
                    .createAlias("a.b", "b")          
                    .add(Restrictions.eq("b.sex", "M"))
                    .add(Restrictions.eq("b.name", "XYZ"));

But, if we check the mapping file, there is no direct reference of B in A. Hence hibernate throws out "B not related to A" entity.

Is there any way this inheritance can be mapped in query crteria

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
user1502377
  • 71
  • 1
  • 2
  • 10

2 Answers2

1

You shouldn't need to reference A at all in your criteria, or use any aliases.

Criteria c = session.createCriteria(B.class);
                    .add(Restrictions.eq("sex", "M"))
                    .add(Restrictions.eq("name", "XYZ"));

will give you the result you need.

Because of the InheritanceType.Joined, this will probably produce SQL that includes a join to the the A table (something close to the sql you show), but it isn't necessary to specify that join in the criteria.

The things that look like columns in the criteria are actually (reflective) references to fields in your Java objects. Hibernate figures out the columns to put in the sql from your annotations, and should the join to the A table if it's needed based on the inheritance annotation.

To be sure of this in your context, and to understand all this a bit better, I'd advise trying it and turning on logging of the generated sql as described in this answer to another SO hibernate question.

Community
  • 1
  • 1
Don Roby
  • 40,677
  • 6
  • 91
  • 113
  • Thanks for the response...But, If I do not mention 'A' in the criteria. how would the columns that I need from 'A' be bought? – user1502377 Jul 05 '12 at 00:48
  • lets say, we have another table X, which is joined to 'A' and A has the subclass functionality with 'B'. Then my criteria would become: Criteria c = session.createCriteria(X.class, "x"); .createAlias("x.a", "a") .createAlias("a.b", "b") .add(Restrictions.eq("b.sex", "M")) .add(Restrictions.eq("b.name","X" ""XYZ"));Then how can I bypass 'A' here as X is related to 'A' only but not 'B' – user1502377 Jul 05 '12 at 01:55
  • 1
    That one's worth some experimenting. If you want to get something in 'X', you'll need a join, but you might still not need to mention 'A'. I'd have to play with an example... – Don Roby Jul 05 '12 at 01:59
0

Try this way: Criteria rootCrit = session.createCriteria(A.class); rootCrit.createAlias("B", "B"); rootCrit.add(Restrictions.eq("B.sex", "M")); rootCrit.add(Restrictions.eq("B.name", "XYZ"));

Tarun Jain
  • 262
  • 1
  • 8
  • Hi tarun, I just modified the question. Now there is another join between table x and a and unable to get the query criteria right? – user1502377 Jul 05 '12 at 13:28