2015-09-30 65 views
1

我有兩個Maven的子項目叫休眠/ QueryDSL - jetty_runner1和jetty_runner2無法從另一個行家子項目

我的目錄結構如下所示:

./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在兩個項目中完全一樣,除了包名(我在這裏創建了這個簡化示例,在實際應用中,這兩個子項目做了非常不同的事情)

這裏是實際的文件內容:

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> 

每個pom.xml中包含QueryDSL和Jetty插件太:

<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> 

當我運行命令mvn碼頭:運行從jetty_runner1和命中URL本地主機:8080/hello /的,一切工作正常,在這個意義上執行查詢和更新。

當我運行從jetty_runner2相同,攻擊同一個URL,它給出了一個例外,俗話說:

org.hibernate.hql.internal.ast.QuerySyntaxException:Misc_Key_Value沒有映射[從Misc_Key_Value選擇miscKeyValue miscKeyValue獲取所有屬性]

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) 

回答

0

我在這裏看到了兩個問題:

  1. 據我所知,你有兩個Web應用程序,一個取決於另一個。您應該將常用的類和配置提取到新的Java庫中,並將此庫包含到您的Web應用程序中。
  2. 如果兩個項目中的persistence.xml具有相同的單元名稱可能會產生問題,那麼您的單元名稱是不同的?
+0

感謝KirkoR。 我不明白爲什麼#1應該是一個問題?這兩個Web應用程序彼此獨立運行。就風格而言,分離是正確的,但邏輯上它不應該是一個問題。沒有? 你能解釋爲什麼#2可能是一個問題?兩個應用程序的數據庫名稱相同。因此,持久性單元的名稱也是一樣的:「世界」。請求您幫助我理解,當兩個應用程序作爲單獨的進程運行時,爲什麼相同的名稱可能成爲問題。 – user2250246

+0

#1。如果您部署兩個分離戰爭,它們可以分開運行。 #2。但請記住,App#2使用App#1。在這種情況下,兩個類都由相同的上下文使用。類加載器極有可能加載兩個persistence.xml - >,第一個被另一個加載。 – KirkoR

1

所以經過一些搜索,我發現了兩個解決方案:

1)添加您的實體類到您的persistence.xml

2)使用org.hibernate.integrator.spi.Integrator服務,但它的使用是不是有據可查對Hibernate 5

所以#1作品,現在有一個刺激性:您需要手動將所有實體類在persistence.xml