未能在Eclipselink上合并日期 [英] Failing to merge date on Eclipselink
问题描述
我的会话bean不会对被管实体执行更新。
My session bean does not execute an update on a managed entity.
我已经包含了有关课程的代码。当我通过使用主要面孔调度组件修改事件的日期字段并将修改的实体传递给会话bean并调用em.merge(event)时,实体管理器不会尝试更新实体,并且在数据库。
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.
会话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托管Bean
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
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管理的JTA连接
- JSF 2
- 原始3
- EJB 3
- JPA 2 EclipseLink
- JTA connection to MySQL managed by Glassfish 3
- JSF 2
- Primefaces 3
解决方案:
根据詹姆斯的回答,问题在于Eclipselink处理的时间域。
As per James' answer the problem lies with Eclipselink treatment of Temporal fields.
链接: 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;
推荐答案
合并工作在其他(非日历)属性?
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.
您可能需要设置持久性单位属性
eclipselink.temporal.mutable=true
(或在映射上使用@Mutable)
You might need to set the persistence unit property, "eclipselink.temporal.mutable"="true" (or use @Mutable on the mapping)
或确保设置新的Calendar实例,而不是修改现有的。
or ensure that a new Calendar instance is set, instead of modifying the existing one.
这篇关于未能在Eclipselink上合并日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!