2015-07-06 89 views
0

我一直在嘗試使用Play Framework 2.4.0,JPA和Hibernate 4.3.9來創建一個簡單和小型的應用程序來連接到PostgreSQL 9.4數據庫。我希望應用程序根據應用程序上創建的模型創建新的數據庫表。到目前爲止,我沒有成功。Play + JPA + Hibernate + PostgreSQL:無法創建表

這是我的 「build.sbt」 的內容:

name := """aplicacion1""" 

version := "1.0-SNAPSHOT" 

lazy val root = (project in file(".")).enablePlugins(PlayJava) 

scalaVersion := "2.11.6" 

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.3.9.Final", 
    "org.hibernate" % "hibernate-entitymanager" % "4.3.9.Final" 

) 

// Play provides two styles of routers, one expects its actions to be injected, the 
// other, legacy style, accesses its actions statically. 
routesGenerator := InjectedRoutesGenerator 

這是我的 「application.conf」 描述的數據庫連接如內容:

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

這是內容我的「persistence.xml」:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" 
      version="2.1"> 

    <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL"> 
     <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
     <non-jta-data-source>DefaultDS</non-jta-data-source> 
     <properties> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/> 
      <property name="hibernate.hbm2ddl.auto" value="update"/> 
     </properties> 
    </persistence-unit> 

</persistence> 

這是我試圖進入數據庫的小型模型類:

@Entity 
public class ruta { 

    @Id 
    public int idRuta; 
    public String nombre; 
    public boolean esIda; 
    public boolean esvuelta; 
    public String apodo1; 
    public String apodo2; 
    public String ref; 

} 

應用程序編譯並運行時沒有錯誤,但從不在數據庫上創建「ruta」表。有什麼我失蹤?謝謝您的幫助!

編輯: 「本地主機:9000」:通過訪問應用程序時,控制檯上出現以下

[info] Compiling 2 Java sources to C:\Users\Enrique\Desktop\tesis_2\aplicacion1\target\scala-2.11\classes... 
SLF4J: The following set of substitute loggers may have been accessed 
SLF4J: during the initialization phase. Logging calls during this 
SLF4J: phase were not honored. However, subsequent logging calls to these 
SLF4J: loggers will work as normally expected. 
SLF4J: See also http://www.slf4j.org/codes.html#substituteLogger 
SLF4J: org.webjars.CloseQuietly 
[info] - application - Creating Pool for datasource 'default' 
[info] - play.api.db.HikariCPConnectionPool - datasource [jdbc:postgresql://localhost/tesis_play1] bound to JNDI as DefaultDS 
[info] - play.api.db.DefaultDBApi - Database [default] connected at jdbc:postgresql://localhost/tesis_play1 
[info] - play.api.libs.concurrent.ActorSystemProvider - Starting application default Akka system: application 
[info] - play.api.Play$ - Application started (Dev) 
+0

我不是JPA的專家,但也許在你application.conf行'db.default.url'是錯誤的:一般情況下它看起來更像是'jdbc:postgresql:// localhost:5432/test_database'和密碼和用戶名,您可能需要在其他行中定義'db.default.user = myusername'和'db.default.password = mypassword' 。 – Kris

+0

@Kris我試過這樣,但結果是一樣的:表仍然沒有被創建:( – Eduardo

+0

什麼是在你的日誌,如果你添加''到你的persistance.xml? – Kris

回答

2

生成就是hbm2ddl數據庫更改被調用。啓動應用程序不會創建EntityManagerFactory和底層Hibernate SessionFactory

創建EntityManagerFactory時,首次調用JPA時會對數據庫進行更改。要啓動的EntityManagerFactory您的應用程序啓動時,只需要創建Global class與JPA電話:

public class Global extends GlobalSettings { 

    public void onStart(Application app) { 

     JPA.withTransaction(new F.Callback0() { 
      @Override 
      public void invoke() throws Throwable { 
       play.Logger.debug("First JPA call"); 
      } 
     }); 


    } 

    public void onStop(Application app) { 
    } 

} 
+0

我剛剛嘗試了你的建議,在控制器包中創建了一個全局類,然後在application.conf中添加了「application.global = controllers.Global」,但表仍然沒有被創建:(還嘗試將Global類移動到默認包,但仍然沒有結果 – Eduardo

+0

您是否在Global.onStart中包含了JPA.withTransaction?我已經測試過與您的配置相同的配置,並且創建了表 –

+0

是的,我將其包含在內,我不知道這可能是重要的,但是在這個項目我有一個「lib」目錄下面的jar包:hibernate-spatial-4.3,jts-1.13,jtsio-1.13,postgis-jdbc-2.1.7,postgresq 1-9.4-1201.jdbc41。目前爲止沒有結果 – Eduardo

0

或許你可以嘗試把它添加到您的persistence.xml

.... 
<property name="hibernate.archive.autodetection" value="class, hbm"/> 
... 

我m沒有JPA專家本人,但這似乎是您的persistence.xml和我在我的項目中唯一的區別。根據docs此屬性將:

確定哪些元素是由Hibernate實體管理器在解析.par歸檔時自動發現的。 (默認爲班級,HBM)。

它說它應該默認爲class,hbm,但我仍然會嘗試。以外的是,我可以推薦的唯一的另一件事是嘗試添加數據庫驅動程序和憑據在persistence.xml,如:創建Hibernate的SessionFactory

<property name="hibernate.connection.driver_class" value="org.h2.Driver"/> 
<property name="hibernate.connection.password" value="foobar"/> 
<property name="hibernate.connection.url" value="jdbc:h2:mem:play"/> 
<property name="hibernate.connection.username" value="sa"/> 
+0

剛剛添加該行persistence.xml,但仍然沒有結果:( – Eduardo

+0

@Eduardo奇怪..也許你可以嘗試做一個'玩乾淨',然後再次運行它 – jsonmurphy

+0

剛做了一個乾淨的項目,但仍然沒有結果 – Eduardo

相關問題