gran_profaci
gran_profaci

Reputation: 8473

Converting MySQL query to Hibernate Criteria

SELECT * FROM test.pchi new INNER JOIN rlhi old ON new_id = old.menu_id where new.name='?'

Similar to:

Select * from db.employee emp INNER JOIN db.table on emp_tableID = table.id where emp.name = '?'

If you could tell me how to do a projection, that would be awesome... as in:

Select emp.name, emp.sex, table.brand from ....

I tried using fetch, but I'm very new to this and keep getting some weird errors. Could someone please demonstrate how to do this?

How about this?

sess.createCriteria(pchi.class)/**/
              .setFetchMode("rlhi", FetchMode.JOIN)
              .add(Restrictions.eq("new_id", "rlhi.menu_id"))
              .add(Restrictions.eq("name", "SOME INPUT"))

Upvotes: 0

Views: 1514

Answers (1)

nachokk
nachokk

Reputation: 14413

sess.createCriteria(Pchi.class)
              .setFetchMode("rlhi", FetchMode.JOIN) //note that rlh1 is the property name in Pchi class
              .add(Restrictions.eq("name", "SOME INPUT"));

In your class you have something like this

class Pchi{
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="new_id", referencedColumnName="menu_id")
private Rlhi rlhi;
}

class Rlhi{
@OneToMany(mappedBy="rlhi")
private <Set> Pchi pchis;
}

NOTE When you use SET should override equals() and hashCode() method to work properly

Upvotes: 1

Related Questions