0

I am following the example provided here by eclipselink.

When I start my tests, it fails with:

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.

The framework isn't creating the table as I would expect. I have the following configuration:

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

From this link, I don't feel it's necessary to add the DescriptorCustomizer class to the persistence.xml file. But I may be wrong.

My question is, do I have to create the table manually? Or I am doing something wrong? The examples I found relative to the feature are quiet poor.

  • The answer could be here .. [link](https://stackoverflow.com/questions/35459997/jpa-how-to-automatically-generate-shadow-tables-history) – Simon De Uvarow Apr 11 '18 at 22:23

1 Answers1

0

Some solutions are discussed in 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
)

Here is another possible solution:

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 :-))