java - Unable to call Hibernate/QueryDSL from another maven subproject -
i have 2 maven sub-projects - jetty_runner1 , jetty_runner2
my directory structure looks following:
./jetty_runner1/pom.xml ./jetty_runner1/src/main/java/com/jetty_runner1/checkpersistence.java ./jetty_runner1/src/main/java/com/jetty_runner1/helloworldservlet.java ./jetty_runner1/src/main/java/com/jetty_runner1/misckeyvalue.java ./jetty_runner1/src/main/java/com/jetty_runner1/misckeyvaluemanager.java ./jetty_runner1/src/main/resources/meta-inf/persistence.xml ./jetty_runner1/src/main/webapp/web-inf/web.xml ./jetty_runner2/pom.xml ./jetty_runner2/src/main/java/com/jetty_runner2/checkpersistence.java ./jetty_runner2/src/main/java/com/jetty_runner2/helloworldservlet.java ./jetty_runner2/src/main/resources/meta-inf/persistence.xml ./jetty_runner2/src/main/webapp/web-inf/web.xml ./pom.xml
checkpersistence.java, helloworldservlet.java, persistence.xml , web.xml same in 2 projects, except package names (i have created simplified example posting here. in actual application, 2 sub-projects wildly different things)
here actual file contents:
checkpersistence.java
public class checkpersistence { public void beginupdation() { string val = null; list<misckeyvalue> keyvalues = misckeyvaluemanager.get().selectstar(); if (keyvalues != null && keyvalues.size() == 1) val = keyvalues.get(0).getstringvalue(); if (val == null) val = "hello world"; misckeyvalue keyvalue = new misckeyvalue(); keyvalue.setstringkey("modifieddate"); keyvalue.setstringvalue(val); misckeyvaluemanager.get().put(keyvalue); } }
helloworldservlet.java
public class helloworldservlet extends httpservlet { @override protected void doget(httpservletrequest request, httpservletresponse response) { new checkpersistence().beginupdation(); } }
misckeyvalue.java
import javax.persistence.column; import javax.persistence.entity; import javax.persistence.id; @entity (name="misc_key_value") public class misckeyvalue { @id @column(name="misc_key_value_id") private string id; @column(name="string_key") private string stringkey; @column(name="string_value") private string stringvalue; public string getid() { return id; } public void setid(string id) { this.id = id; } public string getstringkey() { return stringkey; } public void setstringkey(string stringkey) { this.stringkey = stringkey; } public string getstringvalue() { return stringvalue; } public void setstringvalue(string stringvalue) { this.stringvalue = stringvalue; } }
misckeyvaluemanager.java
import java.util.list; import java.util.uuid; import javax.persistence.entitymanager; import javax.persistence.entitymanagerfactory; import javax.persistence.entitytransaction; import javax.persistence.persistence; import com.mysema.query.jpa.impl.jpaquery; public class misckeyvaluemanager { private static misckeyvaluemanager instance = new misckeyvaluemanager(); protected entitymanagerfactory emf; public static qmisckeyvalue qmisckeyvalue = qmisckeyvalue.misckeyvalue; private misckeyvaluemanager() { this.emf = persistence.createentitymanagerfactory("world"); } public static misckeyvaluemanager get() { return instance; } public list<misckeyvalue> selectstar() { entitymanager em = emf.createentitymanager(); entitytransaction tx = em.gettransaction(); tx.begin(); jpaquery query = new jpaquery(em); list<misckeyvalue> results = query.from(qmisckeyvalue) .fetchall().list(qmisckeyvalue); tx.commit(); em.close(); return results; } public misckeyvalue put(misckeyvalue e) { entitymanager em = emf.createentitymanager(); entitytransaction tx = em.gettransaction(); tx.begin(); if (e.getid() == null) { e.setid(uuid.randomuuid().tostring()); } misckeyvalue mergede = em.merge(e); em.flush(); tx.commit(); em.clear(); em.close(); return mergede; } }
persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="world"> <provider>org.hibernate.jpa.hibernatepersistenceprovider</provider> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.mysqldialect" /> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/world"/> <property name="javax.persistence.jdbc.user" value="root"/> <property name="javax.persistence.jdbc.password" value="password"/> <property name="hibernate.format_sql" value="true"/> </properties> </persistence-unit> </persistence>
web.xml
<?xml version="1.0" encoding="iso-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5" metadata-complete="true"> <servlet> <servlet-name>hello</servlet-name> <servlet-class>com.jetty_runner1.helloworldservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello/*</url-pattern> </servlet-mapping> </web-app>
each pom.xml contains querydsl , jetty plugin too:
<build> <plugins> <plugin> <groupid>org.eclipse.jetty</groupid> <artifactid>jetty-maven-plugin</artifactid> <version>${jettyversion}</version> </plugin> <plugin> <groupid>com.mysema.maven</groupid> <artifactid>apt-maven-plugin</artifactid> <version>1.1.3</version> <executions> <execution> <goals> <goal>process</goal> </goals> <configuration> <outputdirectory>target/generated-sources/java</outputdirectory> <processor>com.mysema.query.apt.jpa.jpaannotationprocessor</processor> </configuration> </execution> </executions> <dependencies> <dependency> <groupid>com.mysema.querydsl</groupid> <artifactid>querydsl-apt</artifactid> <version>${querydsl.version}</version> </dependency> </dependencies> </plugin> </plugins> </build>
when run mvn jetty:run jetty_runner1 , hit url localhost:8080/hello/ , works fine, in sense query executes , updates.
when run same jetty_runner2 , hit same url, gives exception saying:
org.hibernate.hql.internal.ast.querysyntaxexception: misc_key_value not mapped [select misckeyvalue misc_key_value misckeyvalue fetch properties]
at org.hibernate.hql.internal.ast.querysyntaxexception.generatequeryexception(querysyntaxexception.java:79) @ org.hibernate.queryexception.wrapwithquerystring(queryexception.java:103) @ org.hibernate.hql.internal.ast.querytranslatorimpl.docompile(querytranslatorimpl.java:218) @ org.hibernate.hql.internal.ast.querytranslatorimpl.compile(querytranslatorimpl.java:142) @ org.hibernate.engine.query.spi.hqlqueryplan.<init>(hqlqueryplan.java:115) @ org.hibernate.engine.query.spi.hqlqueryplan.<init>(hqlqueryplan.java:76) @ org.hibernate.engine.query.spi.queryplancache.gethqlqueryplan(queryplancache.java:150) @ org.hibernate.internal.abstractsessionimpl.gethqlqueryplan(abstractsessionimpl.java:298) @ org.hibernate.internal.abstractsessionimpl.createquery(abstractsessionimpl.java:236) @ org.hibernate.internal.sessionimpl.createquery(sessionimpl.java:1821) @ org.hibernate.jpa.spi.abstractentitymanagerimpl.createquery(abstractentitymanagerimpl.java:291) @ com.mysema.query.jpa.impl.abstractjpaquery.createquery(abstractjpaquery.java:129) @ com.mysema.query.jpa.impl.abstractjpaquery.createquery(abstractjpaquery.java:96) @ com.mysema.query.jpa.impl.abstractjpaquery.list(abstractjpaquery.java:248) @ com.jetty_runner1.misckeyvaluemanager.selectstar(misckeyvaluemanager.java:35) @ com.jetty_runner2.checkpersistence.beginupdation(checkpersistence.java:13) @ com.jetty_runner2.helloworldservlet.doget(helloworldservlet.java:16)
so after more searching, found 2 solutions:
1) add your-entity-class persistence.xml
2) use org.hibernate.integrator.spi.integrator service it's usage not documented hibernate 5
so #1 works 1 irritant: need manually put entity classes in persistence.xml
Comments
Post a Comment