How to create a history for a Many To Many relationship? [message #1014177] |
Mon, 25 February 2013 09:57  |
Eclipse User |
|
|
|
I want to create history for a many to many relationship similar to the following example:
@Entity
class Movie {
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "MOVIE_ACTOR", joinColumns = { @JoinColumn(name = "MOVIE_ID") }, inverseJoinColumns = { @JoinColumn(name = "ACTOR_ID") }, uniqueConstraints = { @UniqueConstraint(columnNames = {
"MOVIE_ID",
"ACTOR_ID" }) })
private List<Actor> actorList
}
@Entity
class Actor {
}
I have the following historyPolicy defined:
HistoryPolicy policy = new HistoryPolicy();
policy.addHistoryTableName("MOVIE", "TRACK_MOVIE");
policy.addHistoryTableName("ACTOR", "TRACK_ACTOR");
policy.addHistoryTableName("MOVIE_ACTOR", "TRACK_MOVIE_ACTOR");
policy.addStartFieldName(START_DATE);
policy.addEndFieldName(END_DATE);
But it doesn't work. I get the following exception:
Runtime Exceptions:
---------------------------------------------------------
java.lang.ArrayIndexOutOfBoundsException: -1
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:638)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:574)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:533)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.postConnectDatasource(DatabaseSessionImpl.java:777)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:722)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:213)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:542)
... 9 more
Does anybody know what I am doing wrong?
Or
How to create a history for a Many To Many relationship?
|
|
|
|
Re: How to create a history for a Many To Many relationship? [message #1014834 is a reply to message #1014831] |
Tue, 26 February 2013 15:05   |
Eclipse User |
|
|
|
I have an SessionCustomizer like this
public class HistorySessionCustomizer implements SessionCustomizer {
private static final String START_DATE = "START_DATE";
private static final String END_DATE = "END_DATE";
protected List<Class<?>> historyEntityList = new ArrayList<Class<?>>();
public HistorySessionCustomizer() {
buildHistoryEntityList();
}
public HistorySessionCustomizer() {
historyEntityList.add(Movie.class);
historyEntityList.add(Actor.class);
}
public void customize(Session session) throws Exception {
for (Class<?> clazz : historyEntityList) {
ClassDescriptor sourceDescriptor = session.getDescriptor(clazz);
customize(sourceDescriptor);
}
}
private void customize(ClassDescriptor sourceDescriptor) throws Exception {
String sourceTableName = (String) sourceDescriptor.getTableNames().get(sourceDescriptor.getTableNames().size() - 1);
String historyTableName = "TRACK_" + sourceTableName;
HistoryPolicy policy = new HistoryPolicy();
policy.addHistoryTableName(sourceTableName, historyTableName);
policy.addStartFieldName(START_DATE);
policy.addEndFieldName(END_DATE);
if (sourceDescriptor.getJavaClass() == Movie.class) {
policy.addHistoryTableName("MOVIE_ACTOR", "TRACK_MOVIE_ACTOR");
}
sourceDescriptor.setHistoryPolicy(policy);
}
}
I try to add the many to many policy to the sourceDescriptor from the Movie class, but that doesn't work (see exception in my previous post)
I don't know where to add the history policy from the manytomany table, since it doesn't have his own class.
[Updated on: Tue, 26 February 2013 15:06] by Moderator Report message to a moderator
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04591 seconds