WildFly + Hibernate

后端 未结 2 1036
猫巷女王i
猫巷女王i 2020-12-17 04:40

I\'ve been struggling for days with configuring Hibernate and run it on WildFly.

Here is my code:

META-INF/persistence.xml



        
相关标签:
2条回答
  • 2020-12-17 05:03

    A JTA datasource is managed by the jpa container (inside wildfly). You must define the url, username, password in the standalone.xml. Search for the datasources subsystem <subsystem xmlns="urn:jboss:domain:datasources:4.0"> and add a datasource definition, for example:

    <datasource jta="true" jndi-name="java:/jdbc/myDS" pool-name="MyDS" enabled="true" use-java-context="true" use-ccm="true">
        <connection-url>jdbc:mysql://localhost:3306/blog</connection-url>
        <driver>mysqldriver.jar</driver>
        <security>
            <user-name>username</user-name>
            <password>password</password>
        </security>
    </datasource>
    

    Next you have to create a module for your database driver. For the details check the documentation: https://docs.jboss.org/author/display/WFLY8/DataSource+configuration

    Then your persistence.xml will look like this:

    <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="blog">
            <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
            <jta-data-source>jdbc/myDS</jta-data-source>
            <properties>
                <property name="hibernate.archive.autodetection" value="class" />
                 <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
             <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="hibernate.show_sql" value="true"/>
             </properties>
         </persistence-unit>
     </persistence>
    

    During server and component start watch for exceptions.

    0 讨论(0)
  • 2020-12-17 05:15

    In file persistence.xml you must include class to persist

    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    
    <class>package.User<class>
    
    <properties>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/blog?createDatabaseIfNotExist=true"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.connection.username" value="abc"/>
        <property name="hibernate.connection.password" value="abc"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
        <property name="hibernate.hbm2ddl.auto" value="create"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="jboss.as.jpa.providerModule" value="org.hibernate:5.0"/>
    </properties>
    

    You can include as many classes as you like

    0 讨论(0)
提交回复
热议问题