-->
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.  [ 2 posts ] 
Author Message
 Post subject: Do @PostLoad/@PreUpdate etc in pure Hibernate
PostPosted: Wed Jan 16, 2008 8:27 am 
Newbie

Joined: Mon Dec 10, 2007 6:46 am
Posts: 6
I just need a nudge in the right direction from any helpful experts!

I have an entity class using JPA annotations which looks like this;

Code:
@Entity
@Table(name = "view_view", schema = "public")
public class TraxxView extends TraxxObject implements java.io.Serializable {

   private static final long serialVersionUID = -5477777565181800897L;

   @Column(name = "view_definition", nullable = false)
   @NotNull
   @Lob
   private String viewDefinition;

   @Transient
   private ViewConfig viewConfig;

   public TraxxView() {
   }

   public TraxxView(final String description, final String metatableName) {
      this.objtDescription = description;
      this.viewConfig = new ViewConfig(metatableName);
   }

   public ViewConfig getViewConfig() {
      if (viewConfig == null && viewDefinition != null) {
         deserialiseViewConfig();
      }
      return viewConfig;
   }

   public void setViewConfig(final ViewConfig viewConfig) {
      this.viewConfig = viewConfig;
      serialiseViewConfig();
   }

   /**
    * Serialises ViewConfig to XML string before persisting to database
    */
   @SuppressWarnings("unused")
   @PrePersist
   @PreUpdate
   void serialiseViewConfig() {
      final XStream xs = new XStream();
      this.viewDefinition = xs.toXML(this.viewConfig);
   }

   /**
    * De-serialises ViewConfig from XML string after loading object from
    * database
    */
   @SuppressWarnings("unused")
   @PostLoad
   void deserialiseViewConfig() {
      final XStream xs = new XStream();
      this.viewConfig = (ViewConfig) xs.fromXML(this.viewDefinition);
   }

}


Using a JPA EntityManager, this all works as expected. I now need to change this to pure Hibernate - no JPA involved - and I'm having a problem trying to work out how to trigger the serialisation/deserialisation of the viewConfig into viewDefinition (which is an XML string which gets saved/loaded from the database). I've had a look through documentation and I've got a bit lost in events, listeners, tuplizers, etc.

I just need to have the viewConfig object saved in a CLOB database field as XML. Any hints as to the best way to go with pure Hibernate?


Top
 Profile  
 
 Post subject: solution - maybe?
PostPosted: Wed Jan 16, 2008 4:02 pm 
Newbie

Joined: Mon Dec 10, 2007 6:46 am
Posts: 6
Ok, so here is my solution which works - the unit tests pass anyway ;)

I've made a listener...

Code:
public class TraxxViewListener implements PostLoadEventListener,
      SaveOrUpdateEventListener {

   private static DefaultPostLoadEventListener postLoad = new DefaultPostLoadEventListener();
   private static DefaultSaveOrUpdateEventListener saveUpdate = new DefaultSaveOrUpdateEventListener();

   public void onPostLoad(final PostLoadEvent event) {
      if (event.getEntity() instanceof TraxxView) {
         final TraxxView view = (TraxxView) event.getEntity();
         view.deserialiseViewConfig();
      }
      postLoad.onPostLoad(event);
   }

   public void onSaveOrUpdate(final SaveOrUpdateEvent event)
         throws HibernateException {
      if (event.getObject() instanceof TraxxView) {
         System.out.println("Serialising view");
         final TraxxView view = (TraxxView) event.getObject();
         view.serialiseViewConfig();
         event.setEntity(view);
      }
      saveUpdate.onSaveOrUpdate(event);
   }

}


...and put it in the hibernate config like this...

Code:
<listener class="com.fugro.traxx.entities.TraxxViewListener" type="post-load"/>
<listener class="com.fugro.traxx.entities.TraxxViewListener" type="save-update"/>


The entity class is the same. So this works, but it seems messy to me. Apart from the fact that the listener hears ALL post load/save events (and I have to check the type of object which is being persisted) when I only want it to listen for persist events for TraxxView, I also have to delegate to the default listener implementations just to keep everything else working happily. This doesn't look good for having to create listeners for other entities - it seems code for all entities would be in this one listener class (i.e. one listener per event - I just combined the two here because I wanted the code in one place).

So, is this right and just a bit messy because of the way Hibernate does things? Or am I just barking up the wrong tree?


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