Альтернативный именованный запрос для nativequery

Мне нужно изменить свой запрос с нативного запроса на (именованный запрос или создать запрос) в jpa.

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)

Помогите мне, спасибо заранее.

1 ответ

Решение

Используйте JPQL поверх SQL. В JPQL вы ссылаетесь на сущности, а не на таблицы, и на свойства, а не на столбцы.

использование em.createQuery() выполнить запрос JPQL. em.createNamedQuery() для разных целей.

Другие вопросы по тегам