【发布时间】:2017-10-02 08:55:15
【问题描述】:
我正在使用 MySQL 数据库将我的实体保存在这个项目中。我没有从休眠中收到任何错误消息,但是当我签入数据库时,它是空的,并且没有创建任何表。
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 )
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>
我还在持久化后对实体管理器进行了查询,它返回了所有持久化书籍的列表,但它们都不在数据库中。
要求的代码
图书实体:
@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 连接器 jar 放入 modules\system\layers\base\com\mysql\driver\main 中,但它需要转到
modules\system\layers\base\com\mysql\main。
似乎我一直在关注其他一些教程以及我帖子中的教程。 ^^
【问题讨论】:
-
您能否发布相关代码,在其中声明您的实体并持久化它们?
-
@Ish 我发布了代码
标签: java mysql hibernate jpa wildfly-10