有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

HibernateJava。sql。SQLException:ORA01747:无效用户。桌子列,表。列,或列规范

我发现了错误

java.sql.SQLException: ORA-01747: invalid user.table.column, table.column, or column specification

返回此错误的代码如下:

public boolean checkAttendance(String userid,String currentdate){
        boolean status=false;
        List attendanceList=new ArrayList();
        Session session = getSessionFactory().openSession();
        try {
            Transaction tx = session.beginTransaction();
            String hql = "select userid from Attendance where userid='"+ userid +"' and date='"+currentdate+"'";
            Query query = session.createQuery(hql); 
            attendanceList = (ArrayList) query.list();  //This Line returning the error
            if(attendanceList.size()>0)
            {
                status=true;
            }
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }       

        return status;
    }

我已尝试在我的上下文中启用视图sql。xml文件

<property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
                hibernate.show_sql=true;
            </value>
        </property>

我看不到sql正在执行,也不知道为什么会出现此错误,因为我的表和字段是正确的。请帮忙。我正在使用oracle11g


共 (1) 个答案

  1. # 1 楼答案

    select userid from Attendance where userid...是SQL,不是有效的HQL

    你应该把它改成

    select a.userid from Attendance a where a.userid = :userid and a.date=:currentdate

    OT-始终使用查询参数而不是字符串连接是一个好主意。使用参数更安全、更高效。请参见this article以获得良好的解释

    编辑:提前检查查询字符串的有效性。如果无效,则不会创建SQL。如果没有创建SQL,则没有要执行的内容。因此show_sql标志对无效查询无效