我正在使用Hibernate进行数据库访问。 我在我的代码中使用以下查询来获取我需要的数据:
SELECT proasset FROM com.company.claims.participant.AbstractBeneficiary bene JOIN bene.approvals approval JOIN bene.proassetkey proasset join proasset.relatedparties proassetparties WHERE approval.user_dt > :currentDate AND approval.user_type = :userType我在下面使用它作为query :
Query q = this.getSessionFactory().getCurrentSession().createSQLQuery(query.toString()) q.setDate("currentDate", new Date()); q.setString("userType", APPROVER_USER_TYPE); List<ProAsset> proassets = q.list();但是,在尝试运行它时会遇到以下情况:
SQL Error: 933, SQLState: 42000 ORA-00933: SQL command not properly ended如果重要,则使用StringBuilder构造查询,并使用\n来分隔行
有关这个问题的任何想法?
I'm using Hibernate for database access. I'm using the following query in my code to fetch the data I need:
SELECT proasset FROM com.company.claims.participant.AbstractBeneficiary bene JOIN bene.approvals approval JOIN bene.proassetkey proasset join proasset.relatedparties proassetparties WHERE approval.user_dt > :currentDate AND approval.user_type = :userTypeI'm using it as query in the following:
Query q = this.getSessionFactory().getCurrentSession().createSQLQuery(query.toString()) q.setDate("currentDate", new Date()); q.setString("userType", APPROVER_USER_TYPE); List<ProAsset> proassets = q.list();However, I encounter the following when trying to run it:
SQL Error: 933, SQLState: 42000 ORA-00933: SQL command not properly endedIf it matters, the query is being constructed using a StringBuilder and it uses \n to break the lines
Any thoughts on the problem?
最满意答案
看起来您正在尝试将ORM与本机(普通旧SQL)查询混合使用。
createSQLQuery需要本机SQL。 您正在使用类而不是表名。 试试这个:
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html
简而言之,您需要编写如下查询:
select fu from bar where situation = 'snafu'也许你真的想写一个namedQuery? 他们使用ORM语法来加入实体,就像您在示例中所做的那样。
检查这些例子:
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html#querysql-namedqueries
It looks like you are trying to mix ORM with a native (plain old SQL) query.
createSQLQuery requires native SQL. You are using classes instead of table names. Try a read of this:
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html
In short you need to write a query like:
select fu from bar where situation = 'snafu'Perhaps you are really wanting to write a namedQuery? They use ORM syntax where you join entities as it seems you are doing in your example.
Check these examples out:
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html#querysql-namedqueries
更多推荐
发布评论