Search code examples
jakarta-eejpaeclipselink

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


I have JPA Project under Java EE using EclipseLink, and the entity as the following:

 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;

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

and in the DAO i try to update the Model entity as:

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

But i got the error:

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

Solution

  • I found my mistake :), currently i change only the id (witch meaning modify the primary key) and the right way is change the object.

    Currently my code:

    model.getPentity().setId(x);
    

    The correct way is:

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

    Thanks & Regards.