我正在使用MySQL db来保存我的实体在这个项目中。我没有收到来自hibernate的任何错误消息,但是当我检入数据库时它是空的并且没有创建表。
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>
在服务器启动时:
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.hml
<!-- 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>
我还在persist之后运行了对实体管理器的查询,它返回了所有持久书籍的列表,但它们都没有在数据库中。
代码请求
书籍实体:
@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>
,这是jndi-name
文件中我的数据源定义的standalone.xml
属性的正确值。
此外,我错误地将module.xml
和jdbc连接器jar放在modules\system\layers\base\com\mysql\driver\main
内,但它需要转到modules\system\layers\base\com\mysql\main
。
似乎我一直在跟随我的帖子中的一个其他教程。 ^^
让我们看看JPA 2.1规范的摘录:
8.2.1.2 transaction-type
transaction-type属性用于指定实体管理器工厂为持久性单元提供的实体管理器是否必须是JTA实体管理器或资源本地实体管理器。此元素的值为JTA或RESOURCE_LOCAL。事务类型的JTA假定将提供JTA数据源 - 由jta-data-source元素指定或由容器提供。通常,在Java EE环境中,事务类型RESOURCE_LOCAL假定将提供非JTA数据源。在Java EE环境中,如果未指定此元素,则默认值为JTA。在Java SE环境中,如果未指定此元素,则缺省值为RESOURCE_LOCAL。
您尚未在transaction-type
元素中指定persistence-unit
属性。因此,根据规范(参见上面突出显示的部分),您有默认值JTA
。
8.2.1.5 jta-data-source, non-jta-data-source
在Java EE环境中,jta-data-source和non-jta-data-source元素用于指定持久性提供程序要使用的JTA和/或非JTA数据源的JNDI名称。如果两者都未指定,则部署者必须在部署时指定JTA数据源,或者必须由容器提供默认JTA数据源,并且将创建JTA EntityManagerFactory以对应它。
您还没有在jta-data-source
中指定persistence.xml
元素,我假设您在部署期间未指定JTA数据源。这意味着容器将提供默认数据源(请参阅规范中突出显示的文本)。
而且这个数据源并没有指向你的MySQL数据库。除非您重新配置,否则Wildfly中的默认值是H2数据库。因此,您的程序可能正在默认数据库中创建和填充表。因此,您需要先在Wildfly中配置数据源,然后在persistence.xml
中指定它。
这可能是以下原因:
我还在persist之后运行了对实体管理器的查询,它返回了所有持久书籍的列表,但它们都没有在数据库中。
看起来你插入一个数据库,但检查另一个数据库。你能展示数据源配置吗?附:它应该是评论,但没有足够的评级(