2

我在 WebSphere 7 上使用 JPA 和 EJB。

我有以下课程:

@Entity
@Table(name="WIDGET")
public class Widget implements Serializable {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  private String name;
  private String details;
/* getters and setters */
}

我有一个像这样的DAO:

@Stateless
public class WidgetDAO implements WidgetDAOLocal {
  @PersistenceUnit
  private EntityManagerFactory emf;
  private EntityManager em;

  public EntityManager getEntityManager() {
    if (emf == null) {
      throw new Exception();
    }
    return emf.createEntityManager;
  }

  public Widget getWidget(Long id) {
    Widget widget = null;
    EntityManager em = getEntityManager();
    try {
      widget = (Widget)em.find(Widget.class, widgetId);
    } finally {
      em.close();
    }
    return widget;
  }

  public Widget createWidget(Widget widget) {
    EntityManager em = getEntityManager();
    try {
      em.persist(widget);
      em.flush();
    } finally {
      em.close();
    }
    return widget;
  }

  public Widget updateWidget(Widget widget) {
    EntityManager em = getEntityManager();
    try {
      widget = getEntityManager().merge(widget);
      em.flush();
    } finally {
      em.close();
    }
    return widget;
  }

}

创建工作正常,我的小部件出现在数据库中。

但是当我尝试进行合并时,我得到一个错误。进行合并的代码和我得到的错误如下:

public WidgetService {

  @EJB
  private WidgetDAO widgetDAO;

  public WidgetDAO getWidgetDAO() {
    return this.widgetDAO;
  }

  public Widget getWidget(Long id) {
     return this.getWidgetDAO().getWidget(id);
  }

  public void updateDetails(Long widgetId, String details) {
    Widget w = this.getWidget(widgetId);
    w.setDetails(details);
    this.widgetDAO.updateWidget(w);
  }

}

错误是:

Exception caught from before_completion synchronization operation: 
<openjpa-1.2.1-SNAPSHOT-r422266:686069 nonfatal user error> org.apache.openjpa.persistence.InvalidStateException:
The generated value processing detected an existing value assigned to this field: com.mydomain.domain.Widget.id.  
This existing value was either provided via an initializer or by calling the setter method.  
You either need to remove the @GeneratedValue annotation or modify the code to the initializer processing.

非常感谢您对此的任何帮助!

4

2 回答 2

0

合并获取您实体的副本,将其分离并保留新实体,我认为它会尝试从旧实体设置 ID 并导致您生成的值发生冲突。如果您只想更新您的对象,您应该在事务中执行此操作并提交。

于 2012-05-25T23:45:38.350 回答
0

感谢大家的帮助,我想通了,这是问题的原因和对我有用的解决方案。

在我的 DAO 中,我正在这样做:

@Stateless 
public class WidgetDAO implements WidgetDAOLocal {
   @PersistenceUnit
   private EntityManagerFactory emf;
   private EntityManager em;

  public EntityManager getEntityManager() {
     if (emf == null) {
       throw new Exception();
     }
     return emf.createEntityManager;
   } 

因为 EntityManagerFactory 是通过 @PersistenceUnit 注释注入的,所以实体是“应用程序管理的”,导致与 WebSphere 发生某种冲突。

我将代码更改为:

@Stateless 
public class WidgetDAO implements WidgetDAOLocal {
  @PersistenceContext 
  private EntityManager em;

  public EntityManager getEntityManager() {
    return em;
  } 

  public Widget updateWidget(Widget widget) throws Exception {
    return getEntityManager().merge(widget);
  }

@PersistenceContext 注释使实体成为“容器管理”,现在一切正常。

感谢您在这里提供的所有帮助和建议。归根结底,我在重新关注此处文档的“管理实体”部分后制定了解决方案:

http://docs.oracle.com/javaee/5/tutorial/doc/

于 2012-05-28T15:10:30.423 回答