Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
565 views
in Technique[技术] by (71.8m points)

jpa - WildFly + Hibernate

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

Here is my code:

META-INF/persistence.xml

<persistence version="1.0"
         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_1_0.xsd">

<persistence-unit name="blog" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

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

</persistence-unit>

pom.xml

<dependencies>

    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.2.Final</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
    </dependency>

</dependencies>

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

User:

@Entity
@Table(name = "users")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@Column(name = "username")
private String username;

@Column(name = "password")
private String password;

@OneToMany(mappedBy = "user")
private Set<Post> posts;
}

When I load my main page no database is created.

Moreover I want to persist a User.

@Stateless
public class UserRepositoryImpl implements UserRepository {

@PersistenceContext(unitName = "blog")
private EntityManager entityManager;

public void create(User user) {
    this.entityManager.persist(user);
}
}

No database is created and the entity manager is null. What do I need to configure to make it run? I am using IntelliJ for testing.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...