1

I have two maven sub-projects - jetty_runner1 and jetty_runner2

My directory structure looks like the 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 and web.xml are exactly the same in the two projects, except for package names (I have created this simplified example for posting here. In the actual application, the two sub-projects do wildly different things)

Here are the 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 the QueryDSL and 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 I run mvn jetty:run from jetty_runner1 and hit the URL localhost:8080/hello/ , everything works fine, in the sense query executes and updates.

When I run the same from jetty_runner2 and hit the same URL, it gives an exception saying:

org.hibernate.hql.internal.ast.QuerySyntaxException: Misc_Key_Value is not mapped [select miscKeyValue from Misc_Key_Value miscKeyValue fetch all properties]

at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:218)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:298)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1821)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291)
at com.mysema.query.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:129)
at com.mysema.query.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:96)
at com.mysema.query.jpa.impl.AbstractJPAQuery.list(AbstractJPAQuery.java:248)
at com.jetty_runner1.MiscKeyValueManager.selectStar(MiscKeyValueManager.java:35)
at com.jetty_runner2.CheckPersistence.beginUpdation(CheckPersistence.java:13)
at com.jetty_runner2.HelloWorldServlet.doGet(HelloWorldServlet.java:16)
user2250246
  • 3,807
  • 5
  • 43
  • 71

2 Answers2

1

So after some more searching, I found two solutions:

1) Add your-entity-class to your persistence.xml

2) Use org.hibernate.integrator.spi.Integrator service but it's usage is not well documented for Hibernate 5

So #1 works for now with one irritant: You need to manually put all your entity classes in the persistence.xml

user2250246
  • 3,807
  • 5
  • 43
  • 71
0

I see here two problems:

  1. AS far as I understood, you have two web application, and one depends on another. You should extract common classes and configuration into new java library and include this library into your web applications.
  2. If your persistence.xml in both project has the same unit-name it can make problems, your unit-name are different?
KirkoR
  • 788
  • 6
  • 13
  • Thanks KirkoR. I do not see why #1 should be a problem? The two web applications run independent of each other. As a matter of style, separation is correct but logically it should not be a problem. No? Can you please explain why #2 could be a problem? The database name is the same for both the applications. Hence the persistence-unit name is also the same : "world". Request you to help me understand why the same name can be a problem when both the applications are running as separate processes. – user2250246 Oct 01 '15 at 17:06
  • #1. They can be run separately if you deploy two separates war's. #2. But remember that App#1 is used by App#2. In this case both classes are used by the same context. It's highly possible that class loader loads two persistence.xml -> and the first one is overloaded by another. – KirkoR Oct 02 '15 at 17:29