我需要在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)
帮帮我, 提前谢谢。
答案 0 :(得分:0)
使用JPQL而非SQL。在JPQL中,您可以引用实体而不是表和属性而不是列。
使用em.createQuery()
执行JPQL查询。 em.createNamedQuery(
)用于不同目的。