简体   繁体   English

在休眠状态下使用本机查询进行内部联接

[英]Inner join using native query in hibernate

I have a table "questions" which has fields id, version along with other fields. 我有一个表“ questions”,其中包含字段ID,版本以及其他字段。 A question can have multiple records with the same id and different version. 一个问题可以有多个具有相同ID和不同版本的记录。 I need to select the question record for each question with the highest version. 我需要为每个问题的最高版本选择问题记录。

My sql query is 我的SQL查询是

select * from questions v1 inner join 
    (select id, max(version) as highest_version from 
        questions group by id
     )as v2 on v1.id = v2.id and v1.version = v2.highest_version;

This query works from Sequel pro. 此查询可从Sequel pro进行。 But I need to run this from Java and I am using hibernate. 但是我需要从Java运行此程序,并且正在使用休眠模式。

My Java code is: 我的Java代码是:

String assertQuestionQuery = "select v1 from Question v1 inner join "
        + "(select t.id, max(t.version) as highest_version "
        + "from Question t "
        + "group by t.id) "
        + "as v2 on v1.id =  v2.id and v1.version = v2.highest_version";

Query q = sourceEm.createQuery(assertQuestionQuery, Question.class);
List<Question> questionVersions = q.getResultList();

I am getting the following error: 我收到以下错误:

ERROR org.hibernate.hql.internal.ast.ErrorCounter line 1:87: unexpected token: ( 

If I remove the parenthesis I am getting the following error: 如果删除括号,则会出现以下错误:

ERROR org.hibernate.hql.internal.ast.ErrorCounter line 1:87: unexpected token: select

createQuery用于创建JPA / HQL查询,请尝试使用createNativeQuery

I often use Native SQL Query as below: 我经常使用Native SQL Query,如下所示:

@Override
public List<MyObj> findListNew() {
    Session session = null;
    Transaction tx = null;
    List<MyObj> objects = null;

    try {
        session = HibernateUtil.getSessionFactory().openSession();
        tx = session.beginTransaction();    

        // Write Native SQL here
        StringBuffer SqlCommand =new StringBuffer();
        SqlCommand.append("select * \n");               
        SqlCommand.append(" from tableA a , tableB b \n");          
        SqlCommand.append(" where a.tb_id = b.id; ");

        // Map SQL results to your class MyObj
        SQLQuery query = (SQLQuery) session.createSQLQuery(SqlCommand.toString())
                .addScalar("AttributeOfMyObj",IntegerType.INSTANCE)
                .setResultTransformer(Transformers.aliasToBean(MyObj.class));

        objects = query.list();
        tx.commit();            

    } catch (HibernateException e) {
    } catch (Exception e) {
    } finally {
        if (session != null)
            session.close();
    }

    return objects;

}

You can find more instruction & examples at Hibernate official site: https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html 您可以在Hibernate官方网站上找到更多说明和示例: https : //docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html

Try with the following changes. 请尝试以下更改。

  • Use createNativeQuery(..) instead of createQuery(..) 使用createNativeQuery(..)代替createQuery(..)
  • Update the query to use select v1.* from... instead of select v1 from... 更新查询以使用select v1.* from...而不是select v1 from...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM