Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » How to create a history for a Many To Many relationship?
How to create a history for a Many To Many relationship? [message #1014177] Mon, 25 February 2013 09:57 Go to next message
Renso Lohuis is currently offline Renso LohuisFriend
Messages: 44
Registered: July 2012
Member

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 #1014831 is a reply to message #1014177] Tue, 26 February 2013 14:58 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

What is the full code you use for configuring the history policy? You should not add MOVIE_ACTOR to the policy for Movie, you need a separate policy defined for the ManyToManyMapping.


James : Wiki : Book : Blog : Twitter
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 Go to previous messageGo to next message
Renso Lohuis is currently offline Renso LohuisFriend
Messages: 44
Registered: July 2012
Member
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]

Report message to a moderator

Re: How to create a history for a Many To Many relationship? [message #1014842 is a reply to message #1014834] Tue, 26 February 2013 15:11 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

You need to add a HistoryPolicy to the mapping,

ManyToManyMapping mapping = descriptor.getMappingForAttributeName("actorList");
HistoryPolicy policy = new HistoryPolicy();
policy.addHistoryTableName("MOVIE_ACTOR", "TRACK_MOVIE_ACTOR");
mapping.setHistoryPolicy(policy);


James : Wiki : Book : Blog : Twitter
Re: How to create a history for a Many To Many relationship? [message #1014847 is a reply to message #1014842] Tue, 26 February 2013 15:25 Go to previous messageGo to next message
Renso Lohuis is currently offline Renso LohuisFriend
Messages: 44
Registered: July 2012
Member
Yes, that's it. It works now!

Thanks for your help.

[Updated on: Wed, 27 February 2013 08:56]

Report message to a moderator

Re: How to create a history for a Many To Many relationship? [message #1828770 is a reply to message #1014847] Thu, 18 June 2020 10:45 Go to previous message
Gary Kristen is currently offline Gary KristenFriend
Messages: 1
Registered: June 2020
Junior Member
Thank you.

Previous Topic:Polyglot Composite PU - OneToMany/ManyToMany
Next Topic:Error on calling StoredProcedure
Goto Forum:
  


Current Time: Fri Apr 19 16:48:44 GMT 2024

Powered by FUDForum. Page generated in 0.02228 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top