-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to retrieve the table name associated with an entity
PostPosted: Thu Jul 16, 2009 1:31 am 
Newbie

Joined: Thu Jul 16, 2009 1:10 am
Posts: 3
Hi,

I'm using hibernate3 and implementing the PreUpdateEventListener to get notified when updates occur. The next aim is to insert a record of the update into Audit tables. For this need to know the table name that the updated entity is mapped to. How do I retrieve the table name.

Synopsis of my code
public final class HibernateAuditLogListener implements
PreDeleteEventListener, PreInsertEventListener, PreUpdateEventListener, Initializable {
....
....
public final boolean onPreUpdate(PreUpdateEvent event) {
if (event.getPersister() instanceof IAuditable) {
try {
final Long actorId = 0L;
final Serializable entityId = event.getPersister().hasIdentifierProperty() ? event.getPersister().getIdentifier(event.getEntity(), event.getPersister().guessEntityMode(event.getEntity())) : null;
final String entityName = event.getEntity().getClass().toString();
final Date transTime = new Date(); // new Date(event.getSource().getTimestamp());
final EntityMode entityMode = event.getPersister().guessEntityMode(event.getEntity());
Object oldPropValue = null;
Object newPropValue = null;

// need to have a separate session for audit save
StatelessSession session = event.getPersister().getFactory().openStatelessSession();
session.beginTransaction();

// get the existing entity from session so that we can extract existing property values
Object existingEntity = session.get(event.getEntity().getClass(), entityId);
String tableName = ???????????????
IAuditable auditableEntity = (IAuditable) event.getPersister();

// cycle through property names, extract corresponding property values and insert new entry in audit trail
for (String propertyName : event.getPersister().getPropertyNames()) {
newPropValue = event.getPersister().getPropertyValue(event.getEntity(), propertyName, entityMode);
// because we are performing an insert we only need to be concerned will non-null values
if (newPropValue != null) {
// collections will fire their own events
if (!(newPropValue instanceof Collection)) {
oldPropValue = event.getPersister().getPropertyValue(existingEntity, propertyName, entityMode);
if (!oldPropValue.equals(newPropValue)) {
if (LOG.isDebug()) {
LOG.debug("{" + OPERATION_TYPE_UPDATE + "} for: {" + entityName + "}, ID: {" + entityId + "}, property: {" + propertyName + "}, old value: {" + oldPropValue + "}, new value: {" + newPropValue + "}, actor: {" + actorId + "}, date: {" + transTime + "}");
}
}
}
}
}

session.getTransaction().commit();
session.close();
} catch (HibernateException e) {
LOG.error("Unable to process audit log for UPDATE operation:" + e.getMessage());
}
return false;
}
return false;
}


Top
 Profile  
 
 Post subject: Re: How to retrieve the table name associated with an entity
PostPosted: Thu Jul 16, 2009 2:15 am 
Newbie

Joined: Mon Jul 13, 2009 7:56 pm
Posts: 5
You may try the foll.

AbstractEntityPersister aep = (AbstractEntityPersister) getHibernateTemplate().getSessionFactory().getClassMetadata(<class>);
return aep.getTableName();


Top
 Profile  
 
 Post subject: Re: How to retrieve the table name associated with an entity
PostPosted: Thu Jul 16, 2009 6:03 am 
Newbie

Joined: Thu Jul 16, 2009 1:10 am
Posts: 3
Thank you hiberius,

I settled for
AbstractEntityPersister abstractPersister = ((AbstractEntityPersister)event.getPersister());
String tableName = abstractPersister.getTableName();

Do let me know if that is not the right way of going about it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.