-->
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: Hibernate Criteria can't create joins for Embedded objects
PostPosted: Fri Sep 16, 2011 7:33 am 
Newbie

Joined: Fri Sep 16, 2011 7:13 am
Posts: 2
I believe many of you must be already knowing the usage of Embedded Class which we use to define composite primary keys in an Entity class. However, it seems Hibernate Criteria is not able to read joins if I'm using Embedded objects to refer to an Entity class.

Consider the following example:-


I’ve three tables A, B & AB, where AB holds relationship between A & B and hence has a composite primary key which comprises of the primary key of A & primary key of B. I represent the relationship in hibernate as below (i’m using annotations):-

Entity class A.java
——————–-------------


//imports…..
@Entity
@Table(name=”A”)
public class A{
private long aId;
….//other properties
private List bList; //I’ve deliberately given the name bList since using this I can get data of B.
@Id
@Column(name=”A_ID”)
public long getAId(){
return aId;
}
public void setAId(long aId){
this.aId = aId;
}
@OneToMany(mappedBy=”pk.a”)
public List getBList(){
return bList;
}
public void setBList(List bList){
this.bList = bList;
}
}


Entity class B.java
——————–——————–------


//imports…..
@Entity
@Table(name=”B”)
public class B{
private long bId;
private String name;
….//other properties
private List aList; //I’ve deliberately given the name bList since using this I can get data of A.
@Id
@Column(name=”B_ID”)
public long getBId(){
return bId;
}
public void setId(long bId){
this.bId = bId;
}
@Column(name=”B_NM”)
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
@OneToMany(mappedBy=”pk.b”)
public List getAList(){
return aList;
}
public void setAList(List aList){
this.aList = aList;
}
}


Entity class AB.java
——————–——————–------


//imports…..
@Entity
@Table(name=”AB”)
public class AB{
private AB_pk pk;
….//other properties
//Constructor to set PK object
public AB(A a, B b){
pk = new AB_pk();
pk.setA(a);
pk.setB(b);
}
@EmbeddedId
public AB_pk getPk(){
return pk;
}
public void setPk(AB_pk pk){
this.pk = pk;
}
}


Embeddable class AB_pk.java
——————–——————–------


//imports…..
@Embeddable
public class AB{
private A a;
private B b;
public AB_pk(){ } //Empty constructor – required
@ManyToOne
@JoinColumn(name=”A_ID”)
public A getA(){
return a;
}
public void setA(A a){
this.a = a;
}
@ManyToOne
@JoinColumn(name=”B_ID”)
public B getB(){
return b;
}
public void setB(B b){
this.b = b;
}
}


Criteria code block
——————–——————–------


Criteria criteria = session.createCriteria(A.class);
criteria.createCriteria(“bList”, “ab”, CriteriaSpecification.INNER_JOIN);
criteria.createAlias(“ab.pk”, “pk”);
criteria.createCriteria(“pk.B”, “b”, CriteriaSpecification.INNER_JOIN);
ProjectionList projections = Projections.projectionList();
projections.add(Projections.property(“b.name”), “bName”);
criteria.setProjection(projections);
criteria.add(Restrictions.eq(“aId”, Long.valueOf(1)));
List list = criteria.list();


SQL Query formed by Hibernate
——————–——————–------


SELECT b.B_NM FROM A a INNER JOIN AB ab WHERE a.A_ID = 2733

If you notice that it’s able to identify B_NM column through the Embedded ID relationship in Projections but the inner join specified in Criteria wasn’t added by hibernate in the SQL query generated by itself.
Does anybody know where the problem is?


Top
 Profile  
 
 Post subject: Re: Hibernate Criteria can't create joins for Embedded objects
PostPosted: Mon Sep 19, 2011 1:52 am 
Newbie

Joined: Fri Sep 16, 2011 7:13 am
Posts: 2
So, anybody can help on this? Is this possible to achieve in Hibernate Criteria?


Top
 Profile  
 
 Post subject: Re: Hibernate Criteria can't create joins for Embedded objects
PostPosted: Thu Oct 13, 2011 8:24 am 
Beginner
Beginner

Joined: Sun Aug 13, 2006 8:40 am
Posts: 28
I have a similar problem.

I have a class
Code:
A
with an embedded class
Code:
B
which have some properties that is common for that type of class.

When I use this embedded class as a part of a restriction in a criteria search (ex:
Code:
org.hibernate.criterion.Restriction.in("b", Set<B>)
) I get an SqlException for an Illegal SQL. It seems that Hibernate is unable to generate a criteria for an embedded class when using
Code:
org.hibernate.criterion.Restriction.in
Is there another solution for this kind of problem.


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.