0

我需要在 jpa 中将我的查询从native-query更改为(named-query 或 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)

帮助我,提前谢谢。

4

1 回答 1

0

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

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

于 2012-06-29T08:55:47.637 回答