java - HIbernate 多对一连接条件查询

标签 java spring hibernate

我有一个以 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/

相关文章:

java - JPA Hibernate::实体的继承,带有附加的 OneToMany 列表

java - 一会儿,两个服务器线程互相阻塞

java - 通过反射更改最终变量,为什么静态和非静态最终变量之间存在差异

java - 作为收件人列表路由器的收件人的链

java - 当我尝试获取 Long 类型的属性时,为什么会收到 ClassCastException?

mysql - 对xhtml页面的多列hibernate查询

java - Hibernate validator 自定义约束取决于基类

java - 如何规避 "code too large"错误(Java)?

java - EC 客户端和服务器共享 key 不匹配(可能是由于生成服务器 shs 时客户端公钥格式不正确)

Spring mvc 添加多行