【问题标题】:Hibernate HQL Left Join QueryHibernate HQL 左连接查询
【发布时间】:2014-05-08 08:38:49
【问题描述】:

我有两个需要加入的表,但是实体中没有指定关系。我可以写类似的东西

select uc.id, uc.name, mpn.name from UCR uc, MpnMapping mpn

我收到一个名为线程“主”org.hibernate.hql.ast.QuerySyntaxException 中的异常:意外令牌

的错误

【问题讨论】:

标签: java hibernate jpa


【解决方案1】:

是的,您可以使用new 运算符和DTO 类来设置值..

查询

 select new com.example.UCRMNP(uc.id, uc.name, mpn.name) from UCR uc, MpnMapping mpn;

DTO 类

首先可以创建一个DTO类并指定所有列变量,像这样

class UCRMNP{
 int id;
 String name;
 String name1;
 public UCRMNP( int id, String name, String name1){
 this.id=id;
 this.name=name;
 this.name1=name1;
}
}

现在您可以将检索到的数据设置为特定的 DTO 类,您不想 用任何东西注释它,只需指定构造函数并执行 使用 new 运算符的查询。

【讨论】:

    【解决方案2】:

    是的,您可以通过在 where 子句中指定连接来实现。一定是这样的:

    select a, b from A a, B b where a.joinColumn = b.joinColumn
    

    您可以在以下链接中查看更多信息:

    Joining two unrelated tables in hibernate

    http://www.codewrecks.com/blog/index.php/2009/09/04/theta-join-in-hql-join-with-unrelated-entities/

    【讨论】:

    • @angel_navarro 那么如何将其设为左外连接。
    • 对不起@AkhilKNambiar,我认为这是一个内部连接。对于不相关实体的外连接,我认为您不能使用 HQL,因此您可以使用 NativeSQL (docs.jboss.org/hibernate/core/3.6/reference/en-US/html/…) 或在实体中指明关系。
    猜你喜欢
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多