【问题标题】:java.sql.SQLException: ORA-01747: invalid user.table.column, table.column, or column specificationjava.sql.SQLException: ORA-01747: 无效的 user.table.column、table.column 或列规范
【发布时间】:2013-04-28 00:03:25
【问题描述】:

我收到了错误

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;
    }

我已尝试在我的 Context.xml 文件中启用查看 sql

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

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

【问题讨论】:

    标签: java hibernate oracle11g


    【解决方案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 标志对无效查询无效。

    【讨论】:

    • 好的,谢谢,我会试试这个。知道为什么我看不到我的 hls gettng 被执行吗?
    • @Stanley 我可能不够清楚,或者我误解了你 - 查询字符串无效 - 这就是它不执行的原因
    • 这很好,先生,但我应该能够看到应用程序正在尝试执行的查询,以便在我设置 My `hibernate.show_sql=true;` 时得到该错误,对吗?跨度>
    猜你喜欢
    • 2012-03-16
    • 2018-12-19
    • 2015-05-01
    • 2013-10-21
    • 2016-10-25
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多