Hibernate 不会将实体持久化到 MySql - Wildfly 10

Hibernate not persisting entity to MySql - Wildfly 10

我正在使用 MySQL 数据库将我的实体保留在此项目中。我没有从休眠中收到任何错误消息,但是当我检查数据库时它是空的并且没有创建任何表。

persistence.xml(编辑:添加了数据源并且 t运行saction-type 更改为 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>

服务器启动时:

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 )

在持久化后的运行时:

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>

我还运行在持久化之后查询实体管理器,它returns我列出了所有持久化的书籍,但其中none在数据库中。

请求代码

图书实体:

@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...
}

我像这样在 EJB 中持久化实体:

@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;
    }
}

更新

我修复了 persistence.xml,在 standalone.xml 中定义了数据源,并将 jdbc 驱动程序 jar 放在服务器目录中 tutorial.

之后

但我得到这个例外:

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]"
]}

问题已解决

persistence.xml 中将 <jta-data-source>jdbc/MySqlDS</jta-data-source> 替换为 <jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source>,这是 standalone.xml 文件中我的数据源定义的 jndi-name 属性的正确值。

此外,我错误地将 module.xml 和 jdbc 连接器罐放入 modules\system\layers\base\com\mysql\driver\main 中,但它需要转到 modules\system\layers\base\com\mysql\main

除了我的 post 中的教程之外,我似乎一直在关注其他一些教程。 ^^

您似乎插入了一个数据库,但检查了另一个数据库。你能显示数据源配置吗? P.S。应该是评论,但评分不够(

让我们看看 JPA 2.1 规范的摘录:

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.

您没有在 persistence-unit 元素中指定 transaction-type 属性。因此,根据规范(请参阅上面突出显示的部分),您的默认值为 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.

您还没有在 persistence.xml 中指定 jta-data-source 元素,我假设您在部署期间没有指定 JTA 数据源。这意味着默认数据源将由容器提供(请参阅规范中突出显示的文本)。

并且此数据源未指向您的 MySQL 数据库。 Wildfly 中的默认设置是 H2 数据库,除非您重新配置它。因此,您的程序可能会在默认数据库中创建和填充表。所以需要先在Wildfly中配置一个数据源,在persistence.xml.

中指定

这可能是以下原因:

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.