我有一个以 key_column 作为主键的表 1。 Table2 将 table1 中的 key_column 作为外键。 table2 与 table1 具有多对一关系。
我想运行一个连接查询。
Select table1.*, table2.* from table1, table2 where table2.id = ?
table2.some_column_other_than_key_column = ?
and table1.key_column = table2.key_column
<class name="tbl2class" table="tbl2" lazy="false">
<many-to-one name="tbl1class" column="key_column"
class="tbl1Class"
cascade="none" lazy="false" fetch="join" update="false" insert="true" />
</class>
List<tbl2Class> tbl2List= getSession().createCriteria(tbl2Class.class)
.add(Restrictions.eq("id", id))
.add(Restrictions.eq("tbl1.someColumnOtherThanKeyColumn", messageType))
.add(Restrictions.or(categoryRestriction, strategyRestriction))
.list();
我收到异常 mentiontiong Could not resolve tbl1.someColumnOtherThanKeyColumn - 为什么 - 我做错了什么。
public class Tbl1Class
{
private Tbl2Class tbl2Class
}
最佳答案
你应该使用这样的东西
Criteria tbl2Criteria = getSession().createCriteria(tbl2Class.class);
tbl2Criteria.add(Restrictions.eq("id", id));
Criteria tbl1Criteria = tbl2Criteria.createCriteria("tbl1Class");//assuming thats the name of the tbl1 instance in tbl2 class
tbl1Criteria.add(Restrictions.eq("someOtherThanKeyColumn", messageType));
tb12Criteria.add(Restrictions.or(categoryRestriction, strategyRestriction));
List<tbl2Class> result = tbl2Criteria.list();
关于java - HIbernate 多对一连接条件查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18935627/