2013-10-27 84 views
3

我試圖讓一個Play 2.2項目與Hibernate JPA和一個PostgreSQL數據庫一起工作。我之前使用Play 2.1.1完成了它的工作。我現在得到以下錯誤:Play 2.2與休眠JPA和Postgres

play.api.UnexpectedException: Unexpected exception[NoClassDefFoundError: org/w3c/dom/ElementTraversal] 
    at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:152) ~[play_2.10.jar:2.2.0] 

我不知道這是從哪裏來的。我build.sbt看起來是這樣的:

libraryDependencies ++= Seq(
    javaJdbc, 
    cache, 
    javaJpa, 
    "org.apache.directory.api" % "apache-ldap-api" % "1.0.0-M14", 
    "postgresql" % "postgresql" % "9.1-901-1.jdbc4", 
    "org.hibernate" % "hibernate-core" % "4.2.3.Final", 
    "org.hibernate" % "hibernate-entitymanager" % "4.2.3.Final" 
)  

而且我的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="defaultPersistenceUnit" 
    transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <non-jta-data-source>DefaultDS</non-jta-data-source> 
    <properties> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" /> 
     <property name="hibernate.hbm2ddl.auto" value="update" /> 
    </properties> 
</persistence-unit> 

我還沒有編寫任何代碼呢,我只是配置它。

+0

此處瞭解詳情:https://github.com/playframework/play-slick/issues/98 – gavioto

回答

2

好吧,自己想出來。這確實是兩個版本的xml-apis的問題。一個來自Play本身,另一個來自Apache Directory API。我在build.sbt來改變製造品,它是現在工作:

"org.apache.directory.api" % "api-all" % "1.0.0-M14" 
6

你說你沒有寫任何代碼,所以我決定告訴你我是如何創建新的Play! 2.2使用JPA和Postgresql的應用程序。你可以做同樣的事情,並檢查差異。

首先,我創建了新的Play應用程序使用命令:

play new testApp 

然後我testApp/conf目錄/ META-INF目錄下創建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="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <non-jta-data-source>DefaultDS</non-jta-data-source> 
    <properties> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/> 
     <!--<property name="hibernate.show_sql" value="true"/>--> 
     <property name="hibernate.hbm2ddl.auto" value="update"/> 
     <property name="hibernate.format_sql" value="true"/> 
    </properties> 
</persistence-unit> 

添加到我的testApp/conf/application.conf中:

jpa.default=defaultPersistenceUnit 
db.default.driver=org.postgresql.Driver 
db.default.url="postgres://postgres:[email protected]/test" 

# You can expose this datasource via JNDI if needed (Useful for JPA) 
db.default.jndiName=DefaultDS 

我還創建樣本模型類:

@Entity 
@SequenceGenerator(name = "Token_generator", sequenceName = "test_sequence", allocationSize = 1, initialValue = 1) 
public class Test { 

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Token_generator") 
    public Long id; 

    public String name; 
} 

我開始播放應用程序使用命令:

play ~run 

然後,我能看到在HTTP下工作的網站://本地主機:9000 /地址。 我也能夠在postgres測試數據庫中看到新的表測試。

+0

我需要連接什麼庫使用Hibernate註釋?我配置了build.sbt,application.conf和persistence.xml,但是編譯器得到了一個註釋錯誤。 – gooamoko

0

我自己進一步縮小了範圍。它接受它是由我使用的org.apache.directory.api導致的衝突。它在沒有這個庫的情況下工作。我從sbt收到以下消息:

[error] Relocation to an other version number not supported in ivy : xml-apis#xml-apis;2.0.2 relocated to xml-apis#xml-apis;1.0.b2 
. Please update your dependency to directly use the right version. 

但我不知道如何解決這個衝突。

+0

你搜索org.apache.directory.api的新版本嗎?我在他們的頁面上看到了版本1.0.0-M20。 – bandit