I’m searching for a way to extend auto generated linktables between two entities in an OneToMany relationship with an additional timestamp column.
What is the aim?
Each entity itself has a timestamp that shows the time of the last change. But there is no way
to see the kind of changes that were made. It is nesseccary for us to see at what time the assignment between Place and Event was made.
@Entity
public class Place{
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private final List <Event> events = new ArrayList<Event>();
....
}
The DB contains three tables. PLACE, EVENT and PLACE_EVENT.
The table PLACE_EVENT has got two columns: PLACE_ID and EVENT_ID.
I want a third Column “CREATED” with a simple timestamp.
Is there any way to generate this timestamp in an automated way. I dont want to create a new Entity PLACE_EVENT by myself.
I’m not sure if this is possible, but i’m grateful for any hints.
I’m using jpa 2.0 in eclipselink and a db2 driver.
You may create the table yourself (i.e. not rely on automatic schema creation from the entities), and add a timestamp column with a default value set to
now().But beware that sometimes (I can’t remember exactly in which cases), when Hibernate adds a new entity B to A, it deletes all the rows referencing A from the join table and re-inserts them all.
Also note that the timestamp field will be populated in the database, but will not be available from the Hibernate entities. Only SQL queries will be able to access the timestamp.