我有一个简单的实体对象和历史政策
@Entity
@Customizer(MyHistoryPolicy.class)
@Table(name = "employee")
public class Employee {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
... accessor methods
}
public class MyHistoryPolicy implements DescriptorCustomizer {
public void customize(ClassDescriptor descriptor) {
String historyTableName = descriptor.getTableName() + "_history";
HistoryPolicy policy = new HistoryPolicy();
policy.addHistoryTableName(historyTableName);
policy.addStartFieldName("start_date");
policy.addEndFieldName("end_date");
descriptor.setHistoryPolicy(policy);
}
}
插入新对象可以正常工作。更新对象会导致id设置为零。我从Eclipselink
中看到以下调试日志UPDATE employee SET name = ? WHERE (id = ?)
bind => [test name, 2]
UPDATE employee_history SET end_date = ? WHERE ((end_date IS NULL) AND (id = ?))
bind => [2017-09-27 17:45:25.316, 2]
INSERT INTO employee_history (id, name, start_date) VALUES (?, ?, ?)
bind => [2, test name, 2017-09-27 17:45:25.316]
SELECT LAST_INSERT_ID()
请注意历史表插入后的“SELECT LAST_INSERT_ID()”。历史表没有生成的id,因此该查询返回零,并且该值将被覆盖到对象的id字段中。原始ID丢失,这对应用程序来说是个问题。
有人知道如何克服Eclipselink与MySQL的这种不足吗?
答案 0 :(得分:0)
万一有人偶然发现了这个问题,我确实开发了一个对我有用的解决方法。它很简单,因为它只使用JPA监听器来保存和恢复丢失的Id。这是我的代码:
@MappedSuperclass
public abstract class EclipseLinkHistoryFix {
@Transient
private Long savedId;
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@PreUpdate
public void saveId() {
savedId = getId();
}
@PostUpdate
public void restoreId() {
if (savedId != null) {
if (id == null || id.longValue() != savedId.longValue()) {
setId(savedId);
}
savedId = null;
}
}
public final Long getId() {
return id;
}
public final void setId(Long id) {
this.id = id;
}
}
此hack依赖于EclipseLink的内部行为,因此可能无法在将来的更新中使用。幸运的是在v2.6.4中,在插入历史记录后调用PostUpdate回调,因此这很有用。