Failing to merge date on Eclipselink

2020-07-22 19:19发布

问题:

My session bean does not execute an update on a managed entity.

I have included code for the classes concerned. When I modify the date field of an event by using a prime faces schedule component and pass the modified entity to a session bean and call em.merge(event) the entity manager does not attempt to update the entity and no changes are registered in the database.

Session Bean

@Stateless
@LocalBean
public class CalendarSessionBean implements Serializable {

    @PersistenceContext
    private EntityManager em;

    public void moveEvent (CalendarEvent event) {
        em.merge(event);
        Logger.getLogger("example").log(Level.INFO, "Moved {0}", event.getStartDate());
    }
}

Entity Bean

@Entity
public class CalendarEvent implements Serializable {

    @Id
    private Long id;

    @PrePersist
    @PreUpdate
    void onUpdate () { // Never called
        Logger.getLogger("example").log(Level.INFO, "Updating event on the {0}", this.getStartDate());
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "DTE_STR", nullable = false)
    @NotNull
    private Date start; // Column that sould update

    // ... rest omitted        
}

JSF Managed Bean

@ManagedBean(name="agenda")
@ViewScoped
public class AgendaMBean implements Serializable {

    @EJB CalendarSessionBean calendarSession;

    public ScheduleModel getModel () {
        if (model == null) {
            model = new DefaultScheduleModel();

            final Collection<CalendarEvent> calendarEvents = calendarSession.findAll();
            for (final CalendarEvent calendarEvent : calendarEvents) {
                Log.log(Level.FINEST, "Adding to model the {0}", calendarEvent.getStartDate()); 
                final DefaultScheduleEvent event = new DefaultScheduleEvent();
                event.setId(String.valueOf(calendarEvent.getId()));
                event.setStartDate(calendarEvent.getStart());                
                event.setData(calendarEvent);
                model.addEvent(event);
            }
        }
        return model;
    }

    public void onEventMove (final ScheduleEntryMoveEvent event) {
        final CalendarEvent calendarEvent = 
                (CalendarEvent) event.getScheduleEvent().getData();
        Log.log(Level.FINEST, "Moving event to {0}", calendarEvent.getStartDate());
        calendarSession.moveEvent(calendarEvent);   
    }
}

JSF Facelet

<p:schedule value="#{agenda.model}">
    <p:ajax event="eventMove" 
            listener="#{agenda.onEventMove}"/>
</p:schedule>

Output

Adding to model the Fri Feb 03 00:00:00 CET 2012 // ManagedBean.getModel()
Moving event to Wed Feb 08 00:00:00 CET 2012 // ManagedBean.onEventMove()
Moved Wed Feb 08 00:00:00 CET 2012 // SessionBean.move()
// No PrePersist log statement

On:

  • EJB 3
  • JPA 2 EclipseLink
  • JTA connection to MySQL managed by Glassfish 3
  • JSF 2
  • Primefaces 3


SOLUTION:

As per James' answer the problem lies with Eclipselink treatment of Temporal fields.

Link: http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_(ELUG)#How_to_Use_the_.40Mutable_Annotation

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "DTE_STR", nullable = false)
@Mutable(true) // Solution
@NotNull
private Date start;

回答1:

Does the merge work on other (non-Calendar) attributes?

Check if the Calendar of the object being merged is the same instance (==) as the managed objects Calendar.

You might need to set the persistence unit property, "eclipselink.temporal.mutable"="true" (or use @Mutable on the mapping)

or ensure that a new Calendar instance is set, instead of modifying the existing one.