0

Master Table object :

@Entity
@Table(name = "master")
public class Master implements Serializable {
@Id
@GeneratedValue
@Column(name = "id")
private int id;

@Column(name = "name")
private String name;
@OneToMany
@JoinColumns({
    @JoinColumn(updatable = false, insertable = false, name = "name", referencedColumnName = "name"),
    @JoinColumn(updatable = false, insertable = false, name = "id", referencedColumnName = "id")
})
private Set<Child> chld= new HashSet<Child>(0);

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Set<Child> getChld() {
    return chld;
}

public void setChld(Set<Child> chld) {
    this.chld= chld;
}
}

Child Table Object :

@Entity
@Table(name = "temptest")
public class TableObject implements Serializable {


@Column(name = "name")
private String name;
@Id
@GeneratedValue
@Column(name = "p_id")
private int p_id;
@Column(name = "id")
private String id;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}   


public int getP_id() {
    return p_id;
}

public void setP_id(int p_id) {
    this.p_id = p_id;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}  
}

Inner Join code:

        Criteria cr1 = session.createCriteria(Master.class, "m");            
        cr1.createAlias("chld", "c", Criteria.INNER_JOIN);   
        List list = cr1.list();            
        Master m= (Master) list.get(0);
        System.out.println("Master name>" + m.getName());
        Set set = m.getChld();
        System.out.println("set count" + set.size());
        Iterator it = set.iterator();
        while (it.hasNext()) {
            Child chld= (Child ) it.next();
             System.out.println("chld name"+chld.getName());

        }

When i run this code, inner join query is formed and result set was returned.

14:31:09,343 INFO [STDOUT] Hibernate: select this_.name as name189_1_, this_.id as id189_1_, c1_.p_id as p1_188_0_, c1_.id as id188_0_, c1_.name as name188_ 0_ from master this_ inner join child c1_ on this_.name=c1_.name and thi s_.id=c1_.id

Result is consumed in Master object. But the problem is, while retrieve the Child object available inside of Master object.

Set set = m.getChld();

Select query is firing again to get the records from Child table

14:31:09,343 INFO [STDOUT] Hibernate: select chld0_.name as name189_1_, chld0 _.id as id189_1_, chld0_.p_id as p1_1_, chld0_.p_id as p1_188_0_, chld0_.id a s id188_0_, chld0_.name as name188_0_ from child chld0_ where chld0_.name= ? and chld0_.id=?

pls advice, is this the normal behavior of Hibernate or am i missing any thing here,.

Jeevanantham
  • 984
  • 3
  • 19
  • 48
  • You seem to be missing fetch in the join. – danizmax Jul 28 '14 at 11:16
  • @danizmax, Replacing `cr1.createAlias("chld", "c", Criteria.INNER_JOIN);` with `cr1.setFetchMode("chld", FetchMode.JOIN);` works now, but if try to use CreateAlias with Fetchmode it again fires the select query – Jeevanantham Jul 28 '14 at 11:54
  • as I read from the answer of this post http://stackoverflow.com/questions/2347359/hibernate-createcriteria-or-createalias , CreateAlias and CreateCriteria are now identical, so you don't have to use CreateAlias. – danizmax Jul 28 '14 at 13:49
  • @danizmax, in my previous approach join type was InnerJoin, after changed to fetch mode it become left outer join `09:07:05,426 INFO [STDOUT] Hibernate: select this_.id as id8_1_, this_.name as name8_1_, c2_.name as name8_3_, c2_.id as id8_3_, c2_.p_id as p1_3_, c2_.p_id as p1_7_0_, c2_.id as id7_0_, c2_.name as name7_0_ from master this_ left outer join child c2_ on this_.name=m2_.name and th is_.id=c2_.id`. how change this to inner join – Jeevanantham Jul 30 '14 at 03:39

0 Answers0