Converting MySQL query to Hibernate Criteria

和自甴很熟 提交于 2019-12-25 03:14:11

问题


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"))

回答1:


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



来源:https://stackoverflow.com/questions/17055874/converting-mysql-query-to-hibernate-criteria

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!