【发布时间】:2017-01-07 20:57:06
【问题描述】:
我正在尝试使用 JAX-RS 构建一个简单的 REST 服务,它将在数据库表上执行标准的 CRUD 操作。我能够成功查询记录,但无法插入新记录。我没有收到任何错误,当我在调试模式下单步执行代码时,一切看起来都很好。我正在使用在 Glassfish 4.1 容器中运行的事务 CDI bean。
感觉就像它永远不会提交事务。我对 Java EE 很陌生,但我的理解是,由于 bean 是事务性的,因此容器应该为我处理提交。有谁知道为什么没有?
@Path("/recipes")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class RecipeResource {
@Inject
RecipesService recipesService;
@GET
public List<Recipe> getRecipes() {
return recipesService.getAllRecipes();
}
@POST
public void addRecipse(Recipe recipe) {
recipesService.addRecipe(recipe);
}
}
public class RecipesService {
@PersistenceContext(unitName="PANTRYDB", type=PersistenceContextType.TRANSACTION)
EntityManager em;
public RecipesService () {
}
public List<Recipe> getAllRecipes () {
List<Recipe> recipes = null;
try {
TypedQuery<Recipe> typedQuery = em.createQuery("select r from Recipe r", Recipe.class);
recipes = typedQuery.getResultList();
} catch (Exception e) {
System.out.println(e);
}
return recipes;
}
@Transactional
//This is the method that seems to not commit it's transaction
//The Recipe object is populated correctly, and the persist() doesn't
//throw any errors
public void addRecipe(Recipe recipe) {
try {
em.persist(recipe);
} catch (Exception e) {
System.out.println(e);
}
}
}
@Entity
@Table(name="RECIPES", schema="COOKBOOK")
public class Recipe {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column
private String name;
@Column(name="CREATED_DATE")
private Calendar createdDate;
@Column(name="LAST_MADE_DATE")
private Calendar lastMadeDate;
@Column
private String description;
@Column
private String notes;
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Calendar getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Calendar createdDate) {
this.createdDate = createdDate;
}
public Calendar getLastMadeDate() {
return lastMadeDate;
}
public void setLastMadeDate(Calendar lastMadeDate) {
this.lastMadeDate = lastMadeDate;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
@Override
public String toString() {
return name;
}
}
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="PANTRYDB" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.domain.Recipe</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby:/Users/development/eclipse/ws_playground/databases/pantry_db/PANTRYDB" />
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="javax.persistence.jdbc.password" value=""/>
</properties>
</persistence-unit>
</persistence>
【问题讨论】:
-
这对我来说看起来不像“交易”。你认为是什么让这些 bean 成为“事务性”的?
-
@SteveC:
RecipesService.addRecipe()是@Transactional,所以它应该可以工作... @lxdr:你确定你有正确的@Transactional吗?你能显示你的persistence.xml吗?RecipesService上是否有任何限定词未在此处显示? -
@Nikos Paraskevopoulos:
@Transactional的导入解析为 javax.transaction.Transactional。我在课堂上唯一遗漏的是样板进口。我也添加了我的 persistence.xml。谢谢! -
这很奇怪!那么,猜测一下:您是否在 Glassfish 中设置了数据源?如果是这样,您是否尝试过使用
<jta-data-source>元素而不是直接连接<property>元素来使用GF 的数据源? -
如何将 Hibernate 添加到 GlassFish?什么版本的休眠?你在任何地方设置了
hibernate.transaction.jta.platform吗?
标签: jpa jakarta-ee cdi