Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2011
    Posts
    36
    Rep Power
    0

    Default 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 11:53 PM. Reason: Extra Info
    There is not knowledge that it is not power!
    buyapentiumjerk.blogspot.com

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    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

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    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?

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

    Default

    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
    Java Code:
      <mapping class="com.muzikka.hibernate.CustomRevisionEntity"/>
    CustomRevisionEntity:
    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;
    	}
    }
    CustomRevisionListener:
    Java Code:
    ...
    
    Imports
    
    ...
    
    public class CustomRevisionListener implements RevisionListener {
     
    	@Override
    	public void newRevision(Object revisionEntity) {
     
            CustomRevisionEntity customRevisionEntity = (CustomRevisionEntity) revisionEntity;
            customRevisionEntity.setUserName("dasd"); //Just to test
    	}
    }
    Example POJO:
    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
    
    ...
    And to retrieve the data with AuditReader:
    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());
    I still have a problem accessing Envers's history log columns, i get this instead of just the information I want:
    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 07:51 PM. Reason: Extra info
    There is not knowledge that it is not power!
    buyapentiumjerk.blogspot.com

Similar Threads

  1. Using Maven
    By tascoa in forum New To Java
    Replies: 0
    Last Post: 08-21-2009, 04:45 PM
  2. Replies: 0
    Last Post: 04-20-2009, 07:26 PM
  3. Envers 1.0.0.GA
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 07-19-2008, 05:24 PM
  4. Maven or ANT
    By goldhouse in forum Reviews / Advertising
    Replies: 0
    Last Post: 08-09-2007, 09:01 PM
  5. Replies: 2
    Last Post: 06-30-2007, 04:01 PM

Posting Permissions

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