Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » JPA and historic sessions, HistoryPolicy, etc.
JPA and historic sessions, HistoryPolicy, etc. [message #387787] Wed, 13 May 2009 13:15 Go to next message
Frank Sauer is currently offline Frank SauerFriend
Messages: 14
Registered: July 2009
Junior Member
I have a requirement to keep historical data and found that Eclipselink
"Classic" has built-in support for this kind of stuff with HistoryPolicy,
historical sessions and queries with an AsOfClause, etc. My question is
how can I use all that when I approach Eclipselink through the JPA
interfaces, not the classic API?

Thanks,

Frank Sauer
Re: JPA and historic sessions, HistoryPolicy, etc. [message #387923 is a reply to message #387787] Wed, 13 May 2009 14:19 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 should still be able to configure your descriptor with a HistoryPolicy
in JPA using a DescriptorCustomizer.

To use a historical query you can use the EclipseLink JpaEntityManager
interface to create a JPA Query using a EclipseLink DatabaseQuery
(ReadAllQuery, etc.). For a HistoricalSession you would need to access
the EclipseLink Session from the JpaEntityManager and acquire a
HistoricalSession from this using the native EclipseLink API.

Please log an enhancement request to have JPA support added for historical
sessions.

---
James
http://www.nabble.com/EclipseLink---Users-f26658.html


James : Wiki : Book : Blog : Twitter
Re: JPA and historic sessions, HistoryPolicy, etc. [message #387924 is a reply to message #387923] Wed, 13 May 2009 21:42 Go to previous messageGo to next message
Doug Clarke is currently offline Doug ClarkeFriend
Messages: 155
Registered: July 2009
Senior Member
I believe we have a few already

https://bugs.eclipse.org/bugs/show_bug.cgi?id=225073

https://bugs.eclipse.org/bugs/show_bug.cgi?id=270866

https://bugs.eclipse.org/bugs/show_bug.cgi?id=211328

Please vote for or express your interest in these ERs

Doug
Re: JPA and historic sessions, HistoryPolicy, etc. [message #522494 is a reply to message #387787] Mon, 22 March 2010 19:11 Go to previous messageGo to next message
Anna is currently offline AnnaFriend
Messages: 4
Registered: March 2010
Junior Member
Hi, I would also like to use History Policy, however I cannot find an example of how to use that.

So, as an example of eclipse link project, I followed
http://www.vogella.de/articles/JavaPersistenceAPI/article.ht ml

Let's say I want to configure Person in the example to use HistoryPolicy, what do I have to do?

It'd be great if someone can give me a pointer as I cant seem to find any on the eclipse link's wiki.


Many thanks,
Anna
Re: JPA and historic sessions, HistoryPolicy, etc. [message #522778 is a reply to message #522494] Tue, 23 March 2010 21:35 Go to previous messageGo to next message
Doug Clarke is currently offline Doug ClarkeFriend
Messages: 155
Registered: July 2009
Senior Member
I started an effort to publish an example of using HistoryPolicy with JPA under bug 294499 but never published any of my work. I still have the code and just need to add some comments explaining what the various pieces are doing.

I configure history for an entity using a customer as:

/**
* Descriptor customizer that will configure a HistoryPolicy on the descriptor
* using a common pattern for the version column names and the audit table.
*
* @author dclarke
* @since EclipseLink 1.2.0
*/
public class HistoryDescriptorCustomizer implements DescriptorCustomizer {

public static final String START_FIELD_SUFFIX = "AUDIT_START";
public static final String END_FIELD_SUFFIX = "AUDIT_END";
public static final String AUDIT_TABLE_SUFFIX = "_AUDIT";

public void customize(ClassDescriptor descriptor) throws Exception {
String primaryTable = descriptor.getTableName();

HistoryPolicy policy = new HistoryPolicy();
policy.addStartFieldName(primaryTable + "." + START_FIELD_SUFFIX);
policy.addEndFieldName(primaryTable + "." + END_FIELD_SUFFIX);

for (DatabaseTable table : descriptor.getTables()) {
policy.addHistoryTableName(table.getName(), table.getName() + AUDIT_TABLE_SUFFIX);
}

descriptor.setHistoryPolicy(policy);

if (Auditable.class.isAssignableFrom(descriptor.getJavaClass()) ) {
descriptor.getEventManager().addListener(new AuditInfoListener(descriptor));
}
}

}

This customizer is configured using a the persistence unit property:

http://www.eclipse.org/eclipselink/api/2.0.1/org/eclipse/per sistence/config/PersistenceUnitProperties.html#DESCRIPTOR_CU STOMIZER_

I will see if I can get my example at least attached the bug if not published soon.

Doug
Re: JPA and historic sessions, HistoryPolicy, etc. [message #522835 is a reply to message #522778] Wed, 24 March 2010 07:27 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 817
Registered: July 2009
Senior Member
FYI

For some (most?) databases is it possible to put the history tables in a separate database as long as it is running on the same DB instance. In Informix I created such a database "history" and named the tables
"history:" + table.getName()

This keeps the schema's clean.

Tom
Re: JPA and historic sessions, HistoryPolicy, etc. [message #522935 is a reply to message #387787] Wed, 24 March 2010 15:03 Go to previous messageGo to next message
Anna is currently offline AnnaFriend
Messages: 4
Registered: March 2010
Junior Member
I am hoping eclipse link can automagically manage the history data for me - such that my application focus on inserting the current data (without worrying about the start and end date) into a employee_current table, and by setting a property and customiser within EclipseLink, the historic data is managed by EclipseLink, for example, insert a row to employee_history table with start and end date.

@Doug, thanks. If I put in a history descriptor customiser class, how do I make use of it? or is that currently blocked due to bugs?

@Tom, thanks, I think a clean schema always sounds good! (I'll probablty use different tables if not different databases).

This maybe unrelated to this forum (sorry!) but I am very curious about the information around history/temporal data on the internet. Most of the searched results (papers and frameworks) I found seem to have started at the end of 1990s, since then, there seem to be very few updated discussion/framework around it (apart from EclipseLink). Have I been searching with the wrong key word or is there an obvious solution I have missed? Or is everyone just implementing history data storage with triggers or something obvious?

Anna
Re: JPA and historic sessions, HistoryPolicy, etc. [message #522958 is a reply to message #522935] Wed, 24 March 2010 11:00 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 817
Registered: July 2009
Senior Member
> Have I
> been searching with the wrong key word or is there an obvious solution I
> have missed? Or is everyone just implementing history data storage with
> triggers or something obvious?

I can't tell you that. I found that Informix' audit features were terrible (the whole UI is for that matter, it seems to be stuck somewhere in the 80's, but that is even more off topic). I expected keeping track an audit trail to be something that would be fairly common and supported by databases themselves. But that seems not to be the case. So no; I do not thing you were searching wrong.
After considering trigger and not wanting to go there, I was trying to figure out how to bolt this onto my business model, when I the very supportive Eclipselink people pointed me to this solution; and it works like a charm.

The drawback is that I now need to do every change via a Java program (instead of SQL) since otherwise I'm breaking my audit trail.

And you have to be very aware of trivial changes to entities. For example I have a "who done it" field that gets set on a every change, so I can see who made a certain change. But I ended up filling my history table with records that only had a changed "who done it" field. For example: an entity was added to a detail-collection, thus the collection-owning entity was considered "modified" and I updated its who-done-it (and a last-modified timestamp)... But a master-detail is stored in the database using a FK from the detail to the master, so the master record (in database perspective) was not changed at all. But since I'd set its who-done-it/last-modified, it did change and got written to the DB and history.

Tom
Re: JPA and historic sessions, HistoryPolicy, etc. [message #523144 is a reply to message #387787] Thu, 25 March 2010 10:26 Go to previous messageGo to next message
Anna is currently offline AnnaFriend
Messages: 4
Registered: March 2010
Junior Member
Ok, I got HistoryPolicy working - thanks Doug. There seems to be a bug whereby the writes to the history table are being done with the ClassDescriptor of the main table.

My JPA entity/table has an IDENTITY primary key:
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;

This causes the HistoryPolicy writes to expect that inserts on the history table will return an @@IDENTITY despite the fact that it is clearly inserting the id explicitly.

I have tested two workarounds: (1) adding a new synthetic IDENTITY PK on the history table so @@IDENTITY returns something
or (2) proxying ClassDescriptor to always return false from usesSequenceNumbers().

Neither sound like the right thing. Is this a bug or am I using it wrongly?
Re: JPA and historic sessions, HistoryPolicy, etc. [message #523278 is a reply to message #387787] Thu, 25 March 2010 18:23 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Sounds like a bug with history and identity sequencing, please log this issue.

You should be able to use TABLE or SEQUENCE sequencing instead. These are normally preferable to IDENTITY anyway.


James : Wiki : Book : Blog : Twitter
Previous Topic:false but not true
Next Topic:Choose Relationships to Generate Dialog Box does not appear
Goto Forum:
  


Current Time: Tue Mar 19 04:54:11 GMT 2024

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

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

Back to the top