Results 1 to 4 of 4
- 06-07-2011, 01:43 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 36
- Rep Power
- 0
[SOLVED] Hibernate PreInsertEventListener -> How to create a History Table
Hi,
I'm writing a SWING app with hibernate and I want to implement "a kind of" custom made audit trail.
For instance I have one Class "Permissions" and when the user adds or edits a Permission I want to write to a table who, what and when edited/created a Permission.
I have a POJO Permissions:
And I have a POJO PermissionsHistory:Java Code:@Entity @Table(name = "PERMISSIONS", catalog = "DB_MUZIKKA") public class Permissions implements Serializable, PreInsertEventListener { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "PERMISSION_ID", unique = true, nullable = false) private long permissionId; @Column(name = "PERMISSION_NAME", nullable = false, length = 25) private String permissionName; @Column(name = "PERMISSION_DESCRIPTION", nullable = true, length = 255) private String permissionDescription; @Column(name = "PERMISSION_ACTIVE", nullable = false, length = 1) private Boolean permissionActive; @OneToMany(fetch = FetchType.LAZY, mappedBy = "permissionHistory") private List<PermissionsHistory> permissionHistoryList = new ArrayList<PermissionsHistory>(); ... Constructor/ Getters/ Setters ... @Override public boolean onPreInsert(PreInsertEvent arg0) { System.out.println("THIS IS A TEST -------------------------------------"); Object object = arg0.getEntity(); if (object instanceof Permissions) { Permissions entity = (Permissions) object; System.out.println(entity.getPermissionName().toString()); } return false; } }
As you can see I'm using a PreInsertEvent, and my idea is when someone creates or updates a Permission that information should be written on a table "PERMISSIONS_HISTORY" using a One (Permission) to Many (PermissionHistory) association.Java Code:@Entity @Table(name = "PERMISSIONS_HISTORY", catalog = "DB_MUZIKKA") public class PermissionsHistory implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "PERMISSIONHISTORY_ID", unique = true, nullable = false) private long permissionHistoryId; @Temporal(TemporalType.DATE) @Column(name = "PERMISSIONHISTORY_DATE", nullable = false) private Date permissionHistoryDate; @ManyToOne() @ForeignKey(name = "FK_PERMISSIONSHISTORY_PERMISSIONS") @JoinColumn(name = "PERMISSIONHISTORY_PERMISSION", nullable = false) private Permissions permissionHistory; ... Constructors/ Getters/ Setters
My problem is that I'm not sure that this is how it should be implemented. How can I create a new PermissionHistory from Permissions Class??? This doesn't seem a very elegant way of doing this.
Please, any guidance is very appreciated.
Thank you.Last edited by buyapentiumjerk; 06-07-2011 at 09:03 PM. Reason: Solved
There is not knowledge that it is not power!
buyapentiumjerk.blogspot.com
- 06-07-2011, 09:27 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Isn't there an auditing mechanism as part of Hibernate?
Aha!
- 06-07-2011, 03:35 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 36
- Rep Power
- 0
Hi Tolls,
didn't knew about that, but @ first look seems what I was looking for. Java world is a bit different from Micro$oft world, I was used to think in Audit Trail @ DB level.
Thank you.There is not knowledge that it is not power!
buyapentiumjerk.blogspot.com
- 06-07-2011, 09:02 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 36
- Rep Power
- 0
ENVERS is exactly what I wanted, changing to SOLVED.
There is not knowledge that it is not power!
buyapentiumjerk.blogspot.com
Similar Threads
-
create word doucment with Table of contents(TOC)
By jagadeeshkasa in forum Forum GuidesReplies: 0Last Post: 02-04-2011, 05:50 AM -
How to Create TabListener for SWT Table
By Palani in forum SWT / JFaceReplies: 0Last Post: 12-07-2009, 08:31 AM -
how to create a table using gui
By dandon_1912 in forum SWT / JFaceReplies: 1Last Post: 02-27-2009, 01:21 PM -
How to create Table programatically in JSF.
By JavaEmpires in forum JavaServer Faces (JSF)Replies: 2Last Post: 06-25-2008, 11:27 AM -
Hibernate table not found
By orchid in forum JDBCReplies: 2Last Post: 05-06-2007, 07:44 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks