Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJB 3 Edit database record

I have problem with updating a database record using EJB and JPA. Persistence provider: org.eclipse.persistence.jpa.PersistenceProvider

When I am creating a record I am using this method:

public void create(T entity) {
    getEntityManager().persist(entity);
}

All is ok. Now I want to edit the the same record. For e.g. I have an entity:

@Entity
@Table(name = "OPERATION")
public class Operation implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Long id;

    @Column(name = "OPERATION_AUTHOR")
    private String operationAuthor;

    @Column(name = "OPERATION_TYPE")
    private String operationType;

    @Column(name = "OPERATION_STATUS")
    private String operationStatus;

    @Column(name = "CREATED")
    @Temporal(value = TemporalType.DATE)
    private Date created;

    @Column(name = "COMPLETED")
    @Temporal(value = TemporalType.DATE)
    private Date completed;  

    //Getters and setters
}

For e.g. I want to update only operationStatus. I am creating an entity, setting to it the same record id and new operationStatus. For updating I am using this method:

public void edit(T entity) {
    getEntityManager().merge(entity);
}

The problem is when I update the record the status is updated correctly but all the other columns' values are set to null not left as they were before. I want to update only operationStatus and left other values untouched. Is this possible to do this using EJB? And what should I change to make this happen?

like image 790
Paulius Matulionis Avatar asked Jun 19 '26 14:06

Paulius Matulionis


1 Answers

Load the entity first, using Operation op = getEntityManager().find(Operation.class,id). Then do the op.setOperationStatus(value). That's all, it will get updated on session flush/close.

like image 70
2 revs, 2 users 97% Avatar answered Jun 21 '26 05:06

2 revs, 2 users 97%