2016-07-05 39 views
0

我在Eclipse中創建了一個JSF EJB JPA應用程序,現在我想使用Hibernate。我有一個正在運行的MariaDB,爲我的Standalone.xml添加了一個數據源,併爲MySQL創建了一個module.xml。如何在Wildfly 10上運行的Maven WebApp中配置Hibernate?

在我上一個項目中(我沒有自己設置),我以前只是用註釋創建實體,表格會自動創建,我不需要編寫任何映射。我如何設置?例如:

@Entity 
@Table(name = "user") 
public class User extends BaseEntity { 

    private static final long serialVersionUID = 1L; 

    @NotNull 
    @Size(min = 4, max = 50) 
    @Column(name = "username", unique = true, nullable = false) 
    private String username; 

    @NotNull 
    @Size(min = 4, max = 255) 
    @Column(name = "password", nullable = false) 
    private String password; 

    public String getUsername() { 
    return this.username; 
    } 

    public void setUsername(String username) { 
    this.username = username; 
    } 
    // ... 
} 

我讀了幾個教程,但我不確定我是否理解了所有內容。

  • 我需要一個hibernate.cfg.xml或hibernate.properties嗎?
  • 如果Hibernate庫提供了WildFly 10,我需要在我的POM中添加依賴關係嗎?
  • 如何以及在哪裏指定表應該由Hibernate創建?
  • 我用來產生這樣一個EntityManager:

    @ApplicationScoped 
    public class EntityManagerProducer { 
    
        @PersistenceUnit(unitName = "primary") 
        private EntityManagerFactory entityManagerFactory; 
    
        @Produces 
        @Default 
        @TransactionScoped 
        protected EntityManager exposeEntityManagerProxy() { 
        return entityManagerFactory.createEntityManager(); 
        } 
    
        protected void onTransactionEnd(@Disposes @Default EntityManager entityManager) { 
        if (entityManager.isOpen()) { 
         entityManager.close(); 
        } 
        } 
    } 
    

我想使用相同的類 - 如何休眠知道要加載的類?這是如何配置的?

UPDATE1:本WildFly CLI添加JDBC驅動程序後,返回此:

[[email protected]:9990 /] /subsystem=datasources:installed-drivers-list 
{ 
    "outcome" => "success", 
    "result" => [{ 
     "driver-name" => "mysql", 
     "deployment-name" => undefined, 
     "driver-module-name" => "com.mysql", 
     "module-slot" => "main", 
     "driver-datasource-class-name" => "", 
     "driver-xa-datasource-class-name" => "com.mysql.jdbc.jdbc2.optional.Mysq 
lXADataSource", 
     "driver-class-name" => "com.mysql.jdbc.Driver", 
     "driver-major-version" => 5, 
     "driver-minor-version" => 1, 
     "jdbc-compliant" => false 
    }] 
} 
+1

如果您正在使用JPA(像你說的),那麼你應該永遠不需要休眠特定CFG或屬性文件!這是使用JPA –

+0

的整點。那麼,我選擇了JPA方面。我認爲我需要它。現在你問,我不知道了。糾正我,如果我正在擺脫,我認爲Hibernate實現了JPA,但也帶來了新的功能。什麼是「更好」的解決方案?換句話說,哪個更容易實現? – Tim

+0

閱讀JPA規範... JPA從persistence.xml開始工作。 Hibernate CFG/properties文件與JPA無關。是的,Hibernate實現了JPA ...並且在做這件事時使用了persistence.xml ......按照JPA規範 –

回答

1
  • 使用生產商生產的EntityManager。 (已經這樣做)
  • Wild has有hibernate jars。使用它們在你的pom.xml中提供。使用與wildfly提供的版本相同的版本。

使用例如

<dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-core</artifactId> 
    <version>5.0.7.Final</version> 
    <scope>provided</scope> 
</dependency> 
  • 使用您的META-INF文件夾中的persistence.xml文件。這是一個可以幫助你的片段。 這要求你已經在Wildfly中設置了一個myDS數據源。

    <persistence-unit name="myDS"> 
           <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
           <jta-data-source>java:/myDS</jta-data-source> 
           <properties> 
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/> 
            <property name="hibernate.show_sql" value="false"/> 
            <property name="hibernate.default_schema" value="public"/> 
            <property name="hibernate.max_fetch_depth" value="4"/>        
            <property name="javax.persistence.schema-generation.database.action" value="drop-and-create" /> 
            <property name="javax.persistence.schema-generation.create-source" value="script"/> 
            <property name="javax.persistence.schema-generation.create-script-source" value="META-INF/sql/create-script.sql"/> 
            <property name="javax.persistence.schema-generation.drop-source" value="script"/> 
            <property name="javax.persistence.schema-generation.drop-script-source" value="META-INF/sql/drop-script.sql"/> 
            <property name="javax.persistence.sql-load-script-source" value="META-INF/sql/data.sql"/> 
    
            <property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/> 
            <property name="javax.persistence.schema-generation.scripts.create-target" value="target/jpa/sql/create-schema.sql"/> 
            <property name="javax.persistence.schema-generation.scripts.drop-target" value="target/jpa/sql/drop-schema.sql"/> - 
    
            <property name="hibernate.hbm2ddl.auto" value="create"/> 
            <property name="hibernate.hbm2ddl.import_files" value="META-INF/sql/data.sql"/> 
           </properties> 
    

爲了從你在你的projevt指定下降,創建和插入/更新SQL文件的實體自動創建表來執行。已包含在上面的代碼片段中。閱讀每個物業的目的,並使用符合您項目需求的物業。

here

+0

謝謝,我會盡力。我會回來更多的問題:) – Tim

+0

當你沒事的時候別忘了接受答案。如果您有任何其他問題,請告訴我。 – Apostolos

+0

我已經建立了數據源並將其添加到我的standalone.xml中。但是現在,當啓動服務器時,我得到「由:java.lang.UnsupportedOperationException引發:應用程序必須提供JDBC連接」。我需要在哪裏指定?我認爲這是數據源的重點? – Tim

相關問題