未能在Eclipselink上合并日期

时间:2012-02-28 08:30:00

标签: date jpa ejb eclipselink jta

我的会话bean不对托管实体执行更新。

我已经为相关课程提供了代码。当我通过使用主要面部计划组件修改事件的日期字段并将修改后的实体传递给会话bean并调用em.merge(事件)时,实体管理器不会尝试更新实体,并且不会在数据库。

会话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());
    }
}

实体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>

输出

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

在:

  • EJB 3
  • JPA 2 EclipseLink
  • 与Glassfish 3管理的MySQL的JTA连接
  • JSF 2
  • Primefaces 3

<小时/> 的解决方案:

根据詹姆斯的回答,问题在于Eclipselink对时间场的处理。

链接: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 个答案:

答案 0 :(得分:4)

合并是否适用于其他(非日历)属性?

检查要合并的对象的日历是否与托管对象日历的实例(==)相同。

您可能需要设置持久性单元属性, “eclipselink.temporal.mutable”= “真” (或在映射上使用@Mutable)

或确保设置了新的Calendar实例,而不是修改现有的实例。