Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Hibernate Criteria can't create joins for Embedded objects

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 am 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 am using annotations):-

Entity class

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.
public long getAId(){
return aId;
public void setAId(long aId){
this.aId = aId;
public List getBList(){
return bList;
public void setBList(List bList){
this.bList = bList;

Entity class

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.
public long getBId(){
return bId;
public void setId(long bId){
this.bId = bId;
public String getName(){
return name;
public void setName(String name){ = name;
public List getAList(){
return aList;
public void setAList(List aList){
this.aList = aList;

Entity class

public class AB{
private AB_pk pk;
….//other properties
//Constructor to set PK object
public AB(A a, B b){
pk = new AB_pk();
public AB_pk getPk(){
return pk;
public void setPk(AB_pk pk){ = pk;

Embeddable class

public class AB{
private A a;
private B b;
public AB_pk(){ } //Empty constructor – required
public A getA(){
return a;
public void setA(A a){
this.a = a;
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(“”, “pk”);
criteria.createCriteria(“pk.B”, “b”, CriteriaSpecification.INNER_JOIN);
ProjectionList projections = Projections.projectionList();
projections.add(“”), “bName”);
criteria.add(Restrictions.eq(“aId”, Long.valueOf(1)));
List list = criteria.list();

SQL Query formed by Hibernate


If you notice that it is able to identify B_NM column through the Embedded ID relationship in Projections but the inner join specified in Criteria was not added by hibernate in the SQL query generated by itself.
Does anybody know where the problem is?
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic