-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: just need a pointer - many-to-many (sort of)
PostPosted: Wed Dec 15, 2004 10:56 am 
Newbie

Joined: Wed Dec 15, 2004 10:38 am
Posts: 5
Location: Maidstone, England
Hibernate version:2.1.7c

Mapping documents:n/a (thats the whole point)

Code between sessionFactory.openSession() and session.close(): none as yet

Full stack trace of any exception that occurs: n/a

Name and version of the database you are using: mysql 4.0.1

The generated SQL (show_sql=true): na

Debug level Hibernate log excerpt: n/a

I have a number of classes that implement an interface IAuditableObject


Code:
public interface IAuditableObject  {
  public void setAudits(Set audits);
  public Set getAudits();
  public void addAudit(Audit audit);
}


and an audit class viz.
Code:
public class Audit  {

  public final static String CREATE = "Create";
  public final static String UPDATE = "Update";
  private String method;
  private Date dateOfAudit;
  private String recordType;
  private Long id;
  private String user;
  private Set auditables = new TreeSet();
 
  public Audit() {
  }
 
  public Audit(String method, Object recordType) {
    this.method = method;
    this.recordType = recordType.getClass().getName();
    this.dateOfAudit = new Date();
  }
... accessor methods....


for which I have an associated audit table. The problem I am having is that I want the audit table to keep all create & update times and date for users against a particular object type (which implements IAuditableObject).

How do I start doing this?
I have thought about having foreign id of IAuditableObject with class name of concrete class to act as some sort of foreign composite key
e.g.
Quote:
auditId recordType method date id
1 Licence CREATE 12/12 10
2 Person CREATE 14/12 9
3 Licence CREATE 14/4 11
4 Licence UPDATE 14/4 10
5 Person CREATE 15/12 10


so the composite key would be recordType & id

This is my first foray into Hibernate so I am struggling pretty much all the way so far, but managed to get data in and out of a database but having hit this now I am out of ideas.

any suggestions?
Thanks
Conrad


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 15, 2004 11:44 am 
Beginner
Beginner

Joined: Sun Oct 03, 2004 8:50 am
Posts: 32
Location: McLean, VA
Hey Conrad

I'm not sure I understand exactly what you're trying to do but I think what you might want to look at is Hibernate's interceptors.

http://www.hibernate.org/hib_docs/refer ... terceptors

That should give you the means to, well, intercept add/update calls in order to log them. Since this gives you the object that is being operated on you should be able to pull any identifying information you might need from it. After that, what you choose to log will be up to you.

One note, if you're going to store the primary key of the object that is being operated on (which you probably want to or else your log wouldn't be all that useful) you might want to think ahead about if you'll have composite keys and if so how you might store them (you could just concatenate them together and store them as a single string).

_________________
- Chad


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 15, 2004 12:08 pm 
Newbie

Joined: Wed Dec 15, 2004 10:38 am
Posts: 5
Location: Maidstone, England
Chad, thanks for the idea of interceptors - I had already considered them in passing and concluded that they were a sort of database independant type of trigger. I hadn't dismissed the idea entirely, but wanted to provide a bit more auditing information than just created time and lastupdated.

I wanted to record a history of all updates not just the last - this is why I was using another table for audit data. There is a one to many relationship between each object I am trying to audit and the audit class. The problem I have is that I have a number of different classes I want to audit but only want the one table of audit data.
If I were to have a number of audit tables - one for each type of record, it would be easy, but this just seems messy and inelegant somehow. I was going to create the 'create' audit message in the constructor of the auditable class, then perhaps used the interceptor to create the 'update' audit objects.
Do you know if you can create another object in an interceptor such as an audit object which is then saved by Hibernate?

*** flash of inspiration ***
Could I subclass by audit object into the various types of audit message I want e.g.
LicencePremisesAudit extends Audit
PersonAudit extends Audit etc.

then use a the subclass mechanism (the first stategy from here
http://www.hibernate.org/hib_docs/reference/en/html/inheritance.html
to provide a discriminator column to identify the specific audit objects for each object type?

What do you think???
Thanks
Conrad


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 15, 2004 12:24 pm 
Beginner
Beginner

Joined: Sun Oct 03, 2004 8:50 am
Posts: 32
Location: McLean, VA
You can provide as much information as you need, not jus the the time information. You have the object that's being operated on, you know what operation is being performed (update, delete, etc) and other information provided by the interceptor.

However, that said, do you need access to this information off of the objects during runtime? If so the interceptor probably isn't what you want. The joined-subclass should work I would think. If you did a table-per-class type.

_________________
- Chad


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 15, 2004 12:28 pm 
Newbie

Joined: Wed Dec 15, 2004 10:38 am
Posts: 5
Location: Maidstone, England
Thanks Chad,
I will try the subclass idea - I guess the interceptor isn't what I want as I want to provide the audit information in the view of the auditable object.
Also, I can see that I can do anything with the currently worked on object but I can't see how that would also apply to creating new objects within the Interceptor that are linked to the current object.
Thanks again.
Conrad


Top
 Profile  
 
 Post subject: updated result - for completeness
PostPosted: Fri Dec 17, 2004 6:00 am 
Newbie

Joined: Wed Dec 15, 2004 10:38 am
Posts: 5
Location: Maidstone, England
Finalised this problem with the idea of subclassing Audit class into various types of audit for different types of object
e.g. PremisesAudit, LicenceAudit, PersonAudit, OrganisationAudit

created mapping document for each

Code:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping
   PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>

  <class name="uk.police.kent.licensing.shared.Audit" table="AUDIT">
    <id name="id" column="AUDIT_ID">
      <generator class="native" />
    </id>
    <discriminator column="RECORD_TYPE" type="string" />
    <property name="method" column="METHOD" type="string" />
    <property name="dateOfAudit" column="DATE_OF_AUDIT" type="java.sql.Timestamp" />
    <property name="user" column="USER" type="string" />
    <subclass name="uk.police.kent.licensing.shared.LicenceAudit" discriminator-value="licence">
      <many-to-one name="licence" column="ID" class="uk.police.kent.licensing.shared.Licence" />
    </subclass>
    <subclass name="uk.police.kent.licensing.shared.OrganisationAudit" discriminator-value="organisation">
      <many-to-one name="organisation" column="ID" class="uk.police.kent.licensing.shared.Organisation" />
    </subclass>   
    <subclass name="uk.police.kent.licensing.shared.PersonAudit" discriminator-value="person">
      <many-to-one name="person" column="ID" class="uk.police.kent.licensing.shared.Person" />
    </subclass>   
    <subclass name="uk.police.kent.licensing.shared.PremisesAudit" discriminator-value="premises">
      <many-to-one name="premises" column="ID" class="uk.police.kent.licensing.shared.Premises" />
    </subclass>
  </class>
</hibernate-mapping>


which gave me the result ing table I wanted with a sort of foreign composite key of discriminator type and id.

I didn't use interceptor in the end - just put the auditing messages where I created or updated (not the best but works). If due course I may investigate AOP (Spring) to take the auditing out of the business logic. This is what I understand AOP to provide - probably very basic understanding, but we all have to learn sometime!

Well it makes sense to me!

Conrad


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.