Results 1 to 4 of 4
- 06-07-2011, 10:46 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 36
- Rep Power
- 0
Maven -> Hibernate -> ENVERS -> JPA Is this possible?
Hi,
I'm using Maven to create my project, after creating my BD Schema using POJOs I wanted to implement Audit Trail to my project, that's when I found out about ENVERS.
Everything is fine, except that I want to know Who and When someone created or edited something, I've found lots and lots of tutorials on the net for this.
After creating my CustomRevisionEntity and my CustomRevisionListener and banging my head against the table for 1000 times why my project still used DefaultRevisionEntity, I found that I need to create a JPA Entity for CustomRevisionEntity and CustomRevisionListener.
I've been looking @ this tutorial Thinking In Software: Auditing entities with Hibernate JPA
How can I create a JPA Entity with my current Maven project? Do I need to go to Project->Properties->Maven->Project Facets->Configuration->Minimal JPA 2.0 Configuration->Select JPA from checkboxes->And then I can create a CustomRevisionEntity and a CustomRevisionListener as JPA Entity?
I forgot I'm using SWING but eventually I'll use something like Struts2 for web.
Can anyone confirm that this is the way to go?
Thank you for your time.Last edited by buyapentiumjerk; 06-07-2011 at 10:53 PM. Reason: Extra Info
There is not knowledge that it is not power!
buyapentiumjerk.blogspot.com
- 06-08-2011, 09:13 AM #2
Hi.
Frankly say I have never used Envers in applications, I don't just imagine what aim you want to solve. Of course I understand that you want Audit your data, but what is deeply aim? Can you explain what you want to reach? It is just my interesting. :)
I noticed that you have a wrong approach for development process. You pointed that you make all configuration through to use some IDE and didn't said that it is IDE. I hope you know what is difference between Hibernate and JPA.
When I was rereading your post I'm more sure that you don't understand what each technologies make. I piece of advice to figure out with separate technologies and then do mix from it.
Regard.Skype: petrarsentev
http://TrackStudio.com
- 06-08-2011, 09:50 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Look at this thread for why the OP is using ENVERS.
It's what ENVERS was designed for.
Why is Maven causing you a problem?
Indeed, where does Maven come into this at all? Maven is really all about managing your dependencies, so don't get confused by the whole facet thing. All that does is ensure you have the correct jars in your pom (as far as I remember). So long as you have the correct hibernate jars included you should be OK.
So what does your auditing mapping look like?
What does your RevisionEntity look like?
Does it have the correct annotation?
- 06-10-2011, 06:47 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 36
- Rep Power
- 0
Hi,
sorry for the late reply but I've taken a few well deserved days.
@Petrs: You are right I have a wrong approach for development process, I've been "fighting" Java for 4 months (2 for figuring Maven/Nexus and still I can't understand why in Maven Repos (Maven Central Search Engine) theres swingx 1.6.1 and my Maven installation only gets 0.9.7, wich I resolved by importing swingx 1.6.1 into my local repo).
@Tolls: I'm sorry but this is just another case of Overburn, too much time around problems without rest and I've just couldn't think straight, I have to say Envers is perfect, works like a charm and is very easy to setup. The problem was that I wasn't mapping my CustomRevisionEntity and after a while I just remembered that as an Entity I should map it in hibernate.cfg.xml or the DefaultRevisionEntity would still be used. That's what I call using the brain.
In the end I now have this:
hibernate.cfg.xml
CustomRevisionEntity:Java Code:<mapping class="com.muzikka.hibernate.CustomRevisionEntity"/>
CustomRevisionListener:Java Code:... Imports ... @Entity @RevisionEntity(CustomRevisionListener.class) @Table(name ="HISTORY_LOG") public class CustomRevisionEntity extends DefaultRevisionEntity { private static final long serialVersionUID = 1L; @Column(name="username") private String userName; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Override public int hashCode() { final int prime = 31; int result = super.hashCode(); result = prime * result + ((userName == null) ? 0 : userName.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!super.equals(obj)) return false; if (getClass() != obj.getClass()) return false; CustomRevisionEntity other = (CustomRevisionEntity) obj; if (userName == null) { if (other.userName != null) return false; } else if (!userName.equals(other.userName)) return false; return true; } }
Example POJO:Java Code:... Imports ... public class CustomRevisionListener implements RevisionListener { @Override public void newRevision(Object revisionEntity) { CustomRevisionEntity customRevisionEntity = (CustomRevisionEntity) revisionEntity; customRevisionEntity.setUserName("dasd"); //Just to test } }
And to retrieve the data with AuditReader:Java Code:... Imports ... @Audited @Entity @Table(name = "PERMISSIONS") public class Permissions implements Serializable { 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; @ManyToMany(cascade = CascadeType.ALL, targetEntity = Roles.class, mappedBy = "rolePermissionList") @ForeignKey(name = "FK_ROLES_PERMISSIONS") private List<Roles> permissionRoleList = new ArrayList<Roles>(); ... Constructors/ Getters/ Setters ...
I still have a problem accessing Envers's history log columns, i get this instead of just the information I want:Java Code:AuditReader reader = AuditReaderFactory.get(new PermissionsDAO() .getSession()); AuditQuery query = reader.createQuery().forRevisionsOfEntity( Permissions.class, false, true); permissionId = 1L; query.add(AuditEntity.id().eq(permissionId)); List<Object> historyLog = query.getResultList(); final String[] columns = new String[] { "Permission name", "Permission description", "Permission active" }; final boolean[] editableRows = { false, false, false }; TableModel tableModel = new CustomTableModel(historyLog, columns, editableRows); tableHistory.setModel(tableModel); tableHistory.getColumnModel().getColumn(0) .setCellRenderer(new CustomTableCellRenderer()); tableHistory.getColumnModel().getColumn(1) .setCellRenderer(new CustomTableCellRenderer()); tableHistory.getColumnModel().getColumn(1) .setCellRenderer(new CustomTableCellRenderer()); tableHistory.setHighlighters(HighlighterFactory.createSimpleStriping());
https://picasaweb.google.com/1000740...33154844320322
I guess with a few more days I'll be able to make it perfect.
Thank you for your help.Last edited by buyapentiumjerk; 06-10-2011 at 06:51 PM. Reason: Extra info
There is not knowledge that it is not power!
buyapentiumjerk.blogspot.com
Similar Threads
-
Using Maven
By tascoa in forum New To JavaReplies: 0Last Post: 08-21-2009, 03:45 PM -
Maven plugin for eclipse + "Updating Maven Dependencies" problem??
By sbutt in forum EclipseReplies: 0Last Post: 04-20-2009, 06:26 PM -
Envers 1.0.0.GA
By Java Tip in forum Java SoftwareReplies: 0Last Post: 07-19-2008, 04:24 PM -
Maven or ANT
By goldhouse in forum Reviews / AdvertisingReplies: 0Last Post: 08-09-2007, 08:01 PM -
org.hibernate.ejb.Version <clinit> INFO: Hibernate EntityManager 3.2.0.CR1
By Heather in forum JDBCReplies: 2Last Post: 06-30-2007, 03:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks