I am using Hibernate+JPA as the framework for persistence layer, and currently I meet a issue that Hibernate cannot query the Entity which is in an imported Jar file.
An simple example code:
Quote:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("task");
EntityManager em = emf.createEntityManager();
String query = "select c.id from Content c where c.content= 'scarlett'";
System.out.println(em.createQuery(query).getResultList());
em.close();
The class Content is in the drools-process-task.jar file, and I have added the drools-process-task.jar in the current project's class path.
the persistence-unit:
Quote:
<persistence-unit name="task">
<!-- provider is used when there are several JPA implementations are used -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/DroolsTasks"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="passwd"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<!-- after first run the application, should comment it, else it will drop and create table each time -->
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
When run the code, there is exception:
Quote:
Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: Content is not mapped [select t.id from Content t where t.content= :bk]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1201)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1147)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:275)
at com.icil.interaction.handler.TaskId.getTaskIds(TaskId.java:21)
at com.icil.booking.test.Main1.main(Main1.java:135)
Caused by: org.hibernate.hql.ast.QuerySyntaxException: Content is not mapped [select t.id from Content t where t.content= :bk]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:111)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:327)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3441)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3325)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:733)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:584)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:244)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1770)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:272)
... 2 more
The problem should be Hibernate cannot detect the Entity which is in the external JAR file.
But is this the Hibernate feature or I have something config wrong?
if this is the feature, is there any work around?
Quite appreciate your response!