ENVIRONMENT
- Glassfish 4.0
- JPA - Eclipselink 2.5.0
- MySQL 5.5.29
- Trigger in MySQL to Survey table:
Trigger's code
CREATE TRIGGER `survey_BINS`
BEFORE INSERT ON survey
FOR EACH ROW
SET NEW.created = NOW();
PROBLEM:
- Insert a new survey to DB to table Survey
- Created field got a proper value but entity in UI is still the old one without created datetime
- If I sign out and refresh the page, I got the created datetime
QUESTION
- How to avoid this situation?
- Could @PrePersist notation be the answer in JPA entity?
- Switching cache off could help, but how is the performance then?
- Any idea or solution how to get updated entity after insert/update to UI?
Survey ENTITY
@Column(name = "created")
@Temporal(TemporalType.TIMESTAMP)
private Date created;
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
SurveyREST-service
@Stateless
@Path("com.insame.entity.survey")
public class SurveyFacadeREST extends AbstractFacade<Survey> {
@PersistenceContext(unitName = "com.sesame_insame_war_1.0-SNAPSHOTPU")
private EntityManager em;
public SurveyFacadeREST() {
super(Survey.class);
}
@POST
@Override
@Consumes({"application/xml", "application/json"})
public void create(Survey entity) {
super.create(entity);
}
@PUT
@Override
@Consumes({"application/xml", "application/json"})
public void edit(Survey entity) {
super.edit(entity);
}
AbstractFacade
public void create(T entity) {
getEntityManager().persist(entity);
}
public void edit(T entity) {
getEntityManager().merge(entity);
}
Thanks, Sami