entityManager.merge(model) 给我 class [PEntity] 的属性 [id] 映射到数据库中的主键列。不允许更新
entityManager.merge(model) give me The attribute [id] of class [PEntity] is mapped to a primary key column in the database. Updates are not allowed
我在 Java EE 下使用 EclipseLink 有 JPA 项目,实体如下:
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 中,我尝试将模型实体更新为:
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.
我发现了我的错误:),目前我只更改了 id( 意思是修改主键),正确的方法是更改对象。
目前我的代码:
model.getPentity().setId(x);
正确的做法是:
PEntity pentity=PEntityDAO.find(x);
model.setPentity(pentity);
感谢和问候。
我在 Java EE 下使用 EclipseLink 有 JPA 项目,实体如下:
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 中,我尝试将模型实体更新为:
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.
我发现了我的错误:),目前我只更改了 id( 意思是修改主键),正确的方法是更改对象。
目前我的代码:
model.getPentity().setId(x);
正确的做法是:
PEntity pentity=PEntityDAO.find(x);
model.setPentity(pentity);
感谢和问候。