[英]Hibernate not persisting entity to MySql - Wildfly 10
I'm using MySQL db to persist my entities in this project. 我正在使用MySQL数据库来将我的实体保留在该项目中。 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 ) persistence.xml( 编辑:添加了数据源,并且事务类型更改为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 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: 我将实体持久保存在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;
}
}
Update 更新资料
I fixed persistence.xml, defined datasource in standalone.xml and put jdbc driver jar in server's directory following this tutorial . 我修复了persistence.xml,在standalone.xml中定义了数据源,并将jdbc驱动程序jar放在本教程之后的服务器目录中。
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. 在persistence.xml
将<jta-data-source>jdbc/MySqlDS</jta-data-source>
替换为<jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source>
,这是正确的standalone.xml
文件中我的数据源定义的jndi-name
属性的值。
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
. 我也错误地将module.xml
和jdbc连接器jar放在modules\\system\\layers\\base\\com\\mysql\\driver\\main
但它需要转到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: 让我们看一下JPA 2.1规范的摘录:
8.2.1.2 transaction-type 8.2.1.2交易类型
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. transaction-type属性用于指定实体管理器工厂为持久性单元提供的实体管理器必须是JTA实体管理器还是资源本地实体管理器。 The value of this element is JTA or RESOURCE_LOCAL . 该元素的值为JTA或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 . JTA的事务类型假定将提供JTA数据源-由jta-data-source元素指定或由容器提供 。 In general, in Java EE environments, a transaction-type of RESOURCE_LOCAL assumes that a non-JTA datasource will be provided. 通常,在Java EE环境中,事务类型RESOURCE_LOCAL假定将提供非JTA数据源。 In a Java EE environment, if this element is not specified, the default is JTA. 在Java EE环境中,如果未指定此元素,则默认值为JTA。 In a Java SE environment, if this element is not specified, the default is RESOURCE_LOCAL. 在Java SE环境中,如果未指定此元素,则默认值为RESOURCE_LOCAL。
You haven't specified the transaction-type
attribute in the persistence-unit
element. 您尚未在persistence-unit
元素中指定transaction-type
属性。 So, according to the spec (see the highlighted part above) you have the default which is JTA
. 因此,根据规范(请参见上面突出显示的部分),您具有默认值JTA
。
8.2.1.5 jta-data-source, non-jta-data-source 8.2.1.5 jta数据源,非jta数据源
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. 在Java EE环境中,jta-data-source和non-jta-data-source元素用于指定持久性提供程序要使用的JTA和/或非JTA数据源的JNDI名称。 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. 如果未指定,则部署者必须在部署时指定JTA数据源,或者默认JTA数据源必须由容器提供 ,并且将创建一个JTA EntityManagerFactory与其对应。
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. 您还没有在persistence.xml
也指定jta-data-source
元素,并且我假设您在部署期间还没有指定JTA数据源。 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. 并且此数据源未指向您的MySQL数据库。 The default in Wildfly, unless you reconfigured it, is H2 database. 除非您重新配置,否则Wildfly中的默认值为H2数据库。 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
. 因此,您需要首先在Wildfly中配置数据源,然后在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( PS应该是评论,但没有足够的评分(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.