简体   繁体   中英

Hibernate not persisting entity to MySql - Wildfly 10

I'm using MySQL db to persist my entities in this project. I don't get any error messages from hibernate, but when I check in the database it's empty and no tables are created.

persistence.xml ( edit: datasource added and transaction-type changed to JTA )

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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">
<persistence-unit name="punit" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc/MySqlDS</jta-data-source>
    <properties>
        <property name="hibernate.hbm2ddl.auto" value="create"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
    </properties>
</persistence-unit>

At server start:

23:42:35,136 INFO  [stdout] (ServerService Thread Pool -- 59) Hibernate: drop table if exists book
23:42:35,136 INFO  [stdout] (ServerService Thread Pool -- 59) Hibernate: drop table if exists hibernate_sequence
23:42:35,136 INFO  [stdout] (ServerService Thread Pool -- 59) Hibernate: create table book (id bigint not null, description varchar(255), illustrations bit, nbOfPage integer, price float, title varchar(255), primary key (id))
23:42:35,137 INFO  [stdout] (ServerService Thread Pool -- 59) Hibernate: create table hibernate_sequence (next_val bigint)
23:42:35,137 INFO  [stdout] (ServerService Thread Pool -- 59) Hibernate: insert into hibernate_sequence values ( 1 )

At runtime after persist:

23:42:56,796 INFO  [stdout] (default task-4) Hibernate: update 
hibernate_sequence set next_val= ? where next_val=?

23:42:56,807 INFO  [stdout] (default task-4) Book "Java book" persisted! // I print this

23:42:56,814 INFO  [stdout] (default task-4) Hibernate: insert into book 
(description, illustrations, nbOfPage, price, title, id) values (?, ?, ?, ?, ?, ?)

pom.xml

    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>


    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <scope>provided</scope>
    </dependency>

I also ran query on entity manager after persist and it returns me a list of all persisted books, but none of them are in the database.

Code as requested

Book entity:

@Entity
@NamedQuery(name = "Book.findAll", query = "SELECT b FROM Book b")
public class Book implements Serializable {

    @Id @GeneratedValue
    private Long id;

    private String title;  
    private Float price;
    private String description;
    private Integer nbOfPage;  
    private Boolean illustrations;

    public Book() {

    }
    // getters, setters...
}

I persist entity in EJB like this:

@Named
@Stateless
public class BookEJB {

    @PersistenceContext(unitName = "punit")
    EntityManager em;

    public Book createNewBook(Book book) {
        em.persist(book);
        System.out.println("Book \"" + book.getTitle() + "\" persisted!");

        return book;
    }
}

Update

I fixed persistence.xml, defined datasource in standalone.xml and put jdbc driver jar in server's directory following this tutorial .

But I get this exception:

23:12:11,374 INFO  [org.jboss.as.jpa] (MSC service thread 1-3) WFLYJPA0002: Read persistence.xml for punit
23:12:11,625 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("add") failed - address: ([
("subsystem" => "datasources"),
("data-source" => "MySqlDS")
]) - failure description: {"WFLYCTL0180: Services with missing/unavailable dependencies" => [
"org.wildfly.data-source.MySqlDS is missing [jboss.jdbc-driver.mysql]",
"jboss.driver-demander.java:jboss/datasources/MySqlDS is missing [jboss.jdbc-driver.mysql]"
]}

PROBLEM SOLVED

In persistence.xml replaced <jta-data-source>jdbc/MySqlDS</jta-data-source> with <jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source> which was the correct value of jndi-name attribute of my datasource definition inside standalone.xml file.

Also I've incorrectly put module.xml and jdbc connector jar inside modules\\system\\layers\\base\\com\\mysql\\driver\\main but instead it needs to go to modules\\system\\layers\\base\\com\\mysql\\main .

Seems i've been following some other tutorial alongside with the one in my post. ^^

Lets see extracts from the JPA 2.1 specification:

8.2.1.2 transaction-type

The transaction-type attribute is used to specify whether the entity managers provided by the entity manager factory for the persistence unit must be JTA entity managers or resource-local entity managers. The value of this element is JTA or RESOURCE_LOCAL . A transaction-type of JTA assumes that a JTA data source will be provided—either as specified by the jta-data-source element or provided by the container . In general, in Java EE environments, a transaction-type of RESOURCE_LOCAL assumes that a non-JTA datasource will be provided. In a Java EE environment, if this element is not specified, the default is JTA. In a Java SE environment, if this element is not specified, the default is RESOURCE_LOCAL.

You haven't specified the transaction-type attribute in the persistence-unit element. So, according to the spec (see the highlighted part above) you have the default which is JTA .

8.2.1.5 jta-data-source, non-jta-data-source

In Java EE environments, the jta-data-source and non-jta-data-source elements are used to specify the JNDI name of the JTA and/or non-JTA data source to be used by the persistence provider. If neither is specified, the deployer must specify a JTA data source at deployment or the default JTA data source must be provided by the container , and a JTA EntityManagerFactory will be created to correspond to it.

You haven't also specified jta-data-source element in your persistence.xml as well, and I assume you haven't specified a JTA datasource during deployment. That means the default datasource will be provided by the container (see the highlighted text in the spec).

And this datasource is not pointing to your MySQL database. The default in Wildfly, unless you reconfigured it, is H2 database. So your program might be creating and populating tables in the default database. So you need to configure a datasource in Wildfly first and specify it in the persistence.xml .

That might be the reason for:

I also ran query on entity manager after persist and it returns me a list of all persisted books, but none of them are in the database.

It seems like you insert in one db, but check another db. Could you show datasource config? PS It should be comment, but not enough rating(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM