on子句中的未知列 - 在查询中从SQL转换为HQL

时间:2016-05-14 12:58:20

标签: mysql hibernate hql

我正在使用Hibernate 4.3.11和MySQL 5.7.11 我想将这个MySQL查询重写为HQL:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >


     <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

         <ListView android:id="@+id/navList"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_gravity="left|start"
            android:background="#ffeeeeee"
            />


         <ScrollView 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true" >

            <LinearLayout 
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="#C2DFFF">

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@color/indigo_100"
                    android:elevation="5dp"
                    android:text="2nd SCREEN" />

                <ImageView
                    android:id="@+id/imageView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/i1x2" />

                <TextView
                    android:id="@+id/textView22"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/dungeon_1_2"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
            </LinearLayout>
        </ScrollView>

    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

我已将此原生查询重写为HQL:

    df <- read.csv("http://www.statistikdatabasen.scb.se/sq/13965", header=TRUE, sep=",", na.strings="NA", dec=".",stringsAsFactor=F)

library(ggplot2)
ggplot(df, aes(x=year))+geom_bar()

但它不起作用。 Hibernate生成此查询:

SELECT COALESCE(g1.id, g2.id) as id, COALESCE(g1.type, g2.type) as type, COALESCE(g1.email_id, g2.email_id) as email_id, COALESCE(g1.url_id, g2.url_id) as url_id FROM notifications n
LEFT JOIN emails ON emails.id = n.email_id
LEFT JOIN urls ON urls.id = n.url_id
LEFT JOIN notifications_group g1 ON g1.email_id = n.email_id
LEFT JOIN notifications_group g2 ON g2.url_id = n.url_id
WHERE (((n.type = 'EMAIL' OR n.type = 'REMINDER') AND n.email_id is not null AND emails.user_id = :userId)
        OR (n.type = 'URL' AND n.url_id is not null AND urls.user_id = :userId))
        AND (g1.id is not null OR g2.id is not null)
       AND ((g1.id is not null AND g1.id < :beforeId) OR (g2.id is not null AND g2.id < :beforeId))
GROUP BY COALESCE(g1.id, g2.id)
ORDER BY MAX(n.id) DESC 

错误是:

  

'on clause'中的未知列'notificati0_.url_id'   SQL警告代码:1054,SQLState:42S22

但是,在删除标记的第8-9行,第12-13行并删除第14-17行中的条件后,此查询将按预期工作 如何将此查询转换为HQL?如果可能的话。

1 个答案:

答案 0 :(得分:0)

我想在这种情况下,因为查询有点复杂,你可以很好地使用本机查询

请参阅以下示例:

    String q="select employee_name from employee";
    Query query= em.createNativeQuery(q,Object[].class);
    List<Object[]> students= query.getResultList();

请参阅以下文档:

http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#createNativeQuery-java.lang.String-