Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Tolls

Thread: Hibernate PreInsertEventListener -> How to create a History Table

  1. #1
    Join Date
    Apr 2011
    Posts
    36
    Rep Power
    0

    Default [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:
    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;
    	}
    }
    And I have a POJO PermissionsHistory:

    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
    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.

    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

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Isn't there an auditing mechanism as part of Hibernate?

    Aha!
    buyapentiumjerk likes this.

  3. #3
    Join Date
    Apr 2011
    Posts
    36
    Rep Power
    0

    Default

    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

  4. #4
    Join Date
    Apr 2011
    Posts
    36
    Rep Power
    0

    Default

    ENVERS is exactly what I wanted, changing to SOLVED.
    There is not knowledge that it is not power!
    buyapentiumjerk.blogspot.com

Similar Threads

  1. create word doucment with Table of contents(TOC)
    By jagadeeshkasa in forum Forum Guides
    Replies: 0
    Last Post: 02-04-2011, 05:50 AM
  2. How to Create TabListener for SWT Table
    By Palani in forum SWT / JFace
    Replies: 0
    Last Post: 12-07-2009, 08:31 AM
  3. how to create a table using gui
    By dandon_1912 in forum SWT / JFace
    Replies: 1
    Last Post: 02-27-2009, 01:21 PM
  4. How to create Table programatically in JSF.
    By JavaEmpires in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 06-25-2008, 11:27 AM
  5. Hibernate table not found
    By orchid in forum JDBC
    Replies: 2
    Last Post: 05-06-2007, 07:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •