nativequery 的替代命名查询

Posted

技术标签:

【中文标题】nativequery 的替代命名查询【英文标题】:Alternative namedquery for nativequery 【发布时间】:2012-06-29 02:36:06 【问题描述】:

我需要在 jpa 中将我的查询从 native-query 更改为 (named-query or create-query)。

em = getEntityManager();

    String query = "SELECT kcu.table_Name FROM INFORMATION_SCHEMA.key_column_usage kcu,Information_schema.Tables kt " +
            "WHERE kcu.REFERENCED_TABLE_NAME = 'sampleTable1' " +
            "AND kcu.TABLE_SCHEMA='sampleDataBase' " +
            "AND kcu.REFERENCED_COLUMN_NAME = 'sampleRollNoId' " +
            "AND kt.table_name = kcu.table_name " +
            "AND kt.table_rows > 0 " +
            "AND kt.table_schema = kcu.table_schema";

    List tableNameList = (List) em.createNativeQuery(query).getResultList();

此查询返回所有表名(表名外键引用此表(sampleTable1))。

我在更改 createQuery 或 namedQuery 时出错。

喜欢

List tableNameList = (List) em.createQuery(query).getResultList();

or

List tableNameList = (List) em.createNamedQuery(query).getResultList();

如何将此查询更改为 namedQuery 或 createQuery。有可能吗?

使用 createquery 时出现以下错误

------------- Standard Error -----------------
log4j:WARN No appenders could be found for logger 
   (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
log4j:WARN Please initialize the log4j system properly.

Testcase: testEntryInUseNamedQuery(com.JPASampleDAOTest):        Caused an ERROR
org.hibernate.hql.ast.QuerySyntaxException: INFORMATION_SCHEMA.key_column_usage is not mapped

[SELECT kcu.table_Name FROM INFORMATION_SCHEMA.key_column_usage kcu,Information_schema.Tables kt WHERE kcu.REFERENCED_TABLE_NAME = 'sampleTable1' AND kcu.TABLE_SCHEMA='sampleDatabase' AND kcu.REFERENCED_COLUMN_NAME = 'sampleRollNoId' AND kt.table_name = kcu.table_name AND kt.table_rows > 0 AND kt.table_schema = kcu.table_schema]

java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: INFORMATION_SCHEMA.key_column_usage is not mapped [SELECT kcu.table_Name FROM INFORMATION_SCHEMA.key_column_usage kcu,Information_schema.Tables kt WHERE kcu.REFERENCED_TABLE_NAME = 'sampleTable1' AND kcu.TABLE_SCHEMA='sampleDatabase' AND kcu.REFERENCED_COLUMN_NAME = 'sampleRollNoId' AND kt.table_name = kcu.table_name AND kt.table_rows > 0 AND kt.table_schema = kcu.table_schema]
    at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:624)
    at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:96)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:193)
    at $Proxy27.createQuery(Unknown Source)
 at org.springframework.test.context.junit4.SpringTestMethod.invoke(SpringTestMethod.java:198)
    at org.springframework.test.context.junit4.SpringMethodRoadie.runTestMethod(SpringMethodRoadie.java:274)
    at org.springframework.test.context.junit4.SpringMethodRoadie$2.run(SpringMethodRoadie.java:207)
    at org.springframework.test.context.junit4.SpringMethodRoadie.runBeforesThenTestThenAfters(SpringMethodRoadie.java:254)
    at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:234)
    at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:204)
    at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:146)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:151)

Caused by: org.hibernate.hql.ast.QuerySyntaxException: INFORMATION_SCHEMA.key_column_usage is not mapped [SELECT kcu.table_Name FROM INFORMATION_SCHEMA.key_column_usage kcu,Information_schema.Tables kt WHERE kcu.REFERENCED_TABLE_NAME = 'sampleTable1' AND kcu.TABLE_SCHEMA='sampleDatabase' AND kcu.REFERENCED_COLUMN_NAME = 'sampleRollNoId' AND kt.table_name = kcu.table_name AND kt.table_rows > 0 AND kt.table_schema = kcu.table_schema]
    at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
    at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
    at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
    at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:255)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
    at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
    at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
    at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:93)

帮帮我, 提前致谢。

【问题讨论】:

你遇到了什么错误? 感谢 JR Galia 的回复。现在我在我的问题中添加了错误 【参考方案1】:

在 SQL 上使用JPQL。在 JPQL 中,您指的是实体而不是表,以及属性而不是列。

使用em.createQuery() 执行JPQL 查询。 em.createNamedQuery() 用于不同的目的。

【讨论】:

以上是关于nativequery 的替代命名查询的主要内容,如果未能解决你的问题,请参考以下文章

查询、本机查询、命名查询和类型化查询之间的区别 [关闭]

Doctrine2:使用 NativeQuery 和 ResultSetMapping 进行查询

springDataJPQL实现增删改查及分页,原生sql查询,根据方法命名规则实现查询

带有分页的 Spring Data 和 Native Query

JPA 中的 nativeQuery

带有 OpenQuery 和参数的 NativeQuery