gpt4 book ai didi

java - EntityManager 不持久化实体类?

转载 作者:行者123 更新时间:2023-11-29 00:08:59 24 4
gpt4 key购买 nike

我正在尝试使用 JPA 在 RESTful WebService 的 Java Web 应用程序中实现持久性。该程序通过 EntityManager 运行,持久化实体类,被事务包围(开始和提交)。虽然交易会带来麻烦。当我检查 MySQL 数据库时,会创建必要的表,但其中没有条目。怎么可能?代码有什么问题?

我的服务类:

@Stateless
public class ReportService {
@PersistenceContext(unitName = "AbcPU") // default type is PersistenceContextType.TRANSACTION
EntityManager em;

public void saveDog() {
System.out.println("BBBBB Start ReportService.saveDog();");
Doggy doggy = new Doggy();
doggy.setDogName("Wuffi");
try {
System.out.println("BBBBB Persist Success ReportService.saveDog();");
em.getTransaction().begin();
em.persist(doggy);
em.flush();
em.getTransaction().commit();
} catch (Exception ex) {
System.out.println("BBBBB Persist Fail ReportService.saveDog();");
System.err.println("Error with em.persist(doggy): " + ex.getMessage());
}
System.out.println("BBBBB Stop ReportService.saveDog();");
}
}

我的资源类:

@Path("report")
@Produces(MediaType.APPLICATION_JSON)
public class ReportResource {
@EJB
private ReportService rs;

@GET
public Response findReports() {
final List<Report> reports = rs.findAllReports();

System.out.println("AAAAA Start rs.saveDog();");
rs.saveDog();
System.out.println("AAAAA Stop rs.saveDog();");

return Response.ok(new GenericEntity<List<Report>>(reports) {})
.build();
}
}

我的实体类:

@Entity
public class Doggy implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@NotNull
private String dogName;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getDogName() {
return dogName;
}

public void setDogName(String dogName) {
this.dogName = dogName;
}

@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Doggy)) {
return false;
}
Doggy other = (Doggy) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}

@Override
public String toString() {
return "com.glasses.pandora.domain.Doggy[ id=" + id + " ]";
}
}

我的 persistence.xml:

<?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="AbcPU" transaction-type="JTA">
<jta-data-source>java:/jdbc/abc</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>

我在 pom.xml 中的 Maven 依赖项:

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.1.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>unknown.binary</groupId>
<artifactId>hibernate-jpamodelgen-4.3.1.Final</artifactId>
<version>SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

运行后我的控制台输出:

01:58:54,158 INFO  [stdout] (default task-23) AAAAA Start rs.saveDog();
01:58:54,160 INFO [stdout] (default task-23) BBBBB Start ReportService.saveDog();
01:58:54,160 INFO [stdout] (default task-23) BBBBB Persist Success ReportService.saveDog();
01:58:54,160 INFO [stdout] (default task-23) BBBBB Persist Fail ReportService.saveDog();
01:58:54,161 ERROR [stderr] (default task-23) Error with em.persist(doggy): A JTA EntityManager cannot use getTransaction()
01:58:54,161 INFO [stdout] (default task-23) BBBBB Stop ReportService.saveDog();
01:58:54,162 INFO [stdout] (default task-23) AAAAA Stop rs.saveDog();

mysql显示表:

mysql> show tables;
+--------------------+
| Tables_in_abc |
+--------------------+
| Doggy |
| hibernate_sequence |
+--------------------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM Doggy;
Empty set (0.00 sec)

mysql>

最佳答案

有几个问题。 hwellmann 提到的第一个是您没有指定数据源。 JTA 需要一个,以便使用与 JTA 事务关联的连接。

第二个是您似乎没有在事务中包装 saveDog 方法。 persist 调用只会在上下文中注册实体;只有在提交关联事务或调用 EntityManager.flush() 时,它才会插入到数据库中。如果您在保存方法中调用 em.flush,这将验证您的 EntityManager 是否正确关联到事务以及插入是否成功。

关于java - EntityManager 不持久化实体类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26166616/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com