entityManager.merge(model)给我类[PEntity]的属性[id]映射到数据库中的主键列。不允许更新

时间:2015-01-28 07:10:00

标签: java-ee jpa eclipselink

我使用EclipseLink在Java EE下使用JPA Project,并使用以下实体:

 public class PEntity{
   @Id
   private long id;
   .....
 }

 public class Model{
    @Id
    private long id;

    @ManyToOne(cascade = CascadeType.REFRESH , optional = false, fetch = FetchType.EAGER)
    @JoinColumn(name="P_ID", nullable=false, insertable=true, updatable=true)
    private PEntity pentity;

    ............
 }

在DAO中我尝试将Model实体更新为:

transaction.begin();
entityManager.joinTransaction();
updatedModel=entityManager.merge(model);
entityManager.refresh(model);           
entityManager.flush();
transaction.commit();

但是我得到了错误:

The attribute [id] of class [PEntity] is mapped to a primary key column in the database. Updates are not allowed.

1 个答案:

答案 0 :(得分:1)

我发现了我的错误:),目前我只更改了id(女巫意味着修改主键),正确的方法是更改​​对象。

目前我的代码:

model.getPentity().setId(x);

正确的方法是:

PEntity pentity=PEntityDAO.find(x);
model.setPentity(pentity);

谢谢&问候。