JPA EclipseLink HistoryPolicy 示例

Posted

技术标签:

【中文标题】JPA EclipseLink HistoryPolicy 示例【英文标题】:JPA EclipseLink HistoryPolicy example 【发布时间】:2018-09-21 20:57:46 【问题描述】:

我正在按照here by eclipselink提供的示例进行操作。

当我开始测试时,它失败了:

javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.1.v20171221-bd47e8f):          
org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERROR: relation "event_history" does not exist.

框架没有像我预期的那样创建表格。我有以下配置:

<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>

从这个link,我觉得没有必要将DescriptorCustomizer 类添加到persistence.xml 文件中。但我可能错了。

我的问题是,我必须手动创建表吗?或者我做错了什么?我发现的与该功能相关的示例非常糟糕。

【问题讨论】:

答案可能在这里..link 【参考方案1】:

eclipse link forum 讨论了一些解决方案。

Clovis Wichoski CLA Friend 2016-01-02 15:29:19 EST
The problem still occurs with 2.6.2

Follow a StringTemplate to be used to easy the creation by hand (for Postgres database)

CREATE TABLE <tableName>_audit (
   LIKE <tableName> EXCLUDING ALL,
   audit_date_start timestamp,
   audit_date_end timestamp
)

这是另一种可能的解决方案:

Peter Hansson CLA Friend 2016-03-25 05:30:42 EDT
Yes, I've had the same issue.

I've explored a couple of avenues in order to get EclipseLink to generate the history tables for me (so that they always reflect their base table). I haven't been able to come up with a method that would work, less one that would be db agnostic.

I do believe the only way to solve this is in the core of EclipseLink, for example by adding a new annotation, @HistoryTable.


I'm thinking something along the lines of the following: 

Suppose you have base class, Person: 


@Entity
public class Person 

   @Id
   private Long personId;
   private String firstName;
   private String lastName;
   ..


Then we could define a history entity for that entity as follows:

@Entity
@HistoryTable(base=Person.class, primaryKeyFields="personId,rowStartTime")
public class PersonHist 

   // Add here the extra fields/columns that should exist for the
   // history table.
   private Date rowStartTime;
   private Date rowEndTime;
   ..



The @HistoryTable annotation would replicate all fields from the base entity, including most field annotations, except for annotations related to relations, which wouldn't be relevant on the history table.

By definition the history table's primary key will always be a composite of columns in the base table, typically it will be like in the example. In the example the PersonHist entity will think it has an @Id notation on fields personId and rowStartTime. (yeah, this area needs more brain work :-))

【讨论】:

以上是关于JPA EclipseLink HistoryPolicy 示例的主要内容,如果未能解决你的问题,请参考以下文章

JPA/EclipseLink - 计算列

JPA + EclipseLink+ HSQLDB 不创建表

JPA 从 EclipseLink 到哪个提供商?

jpa2/eclipselink 的合适 DAO 结构是啥?

JPA - 使用 EclipseLink 保持单向一对多关系失败

从子句中的jpa eclipselink子查询