-->
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.  [ 4 posts ] 
Author Message
 Post subject: @PreUpdate not working with Seam @Entity
PostPosted: Thu Jul 06, 2006 2:23 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
(Also posted this on the Seam forums here)

I'm building my Seam app from the Seam Hibernate example under Tomcat alone (microcontainer, yada yada). Using Seam 1.0.1GA with Hibernate 3.2 built in. I'm trying to declare a @PreUpdate method to protect certain objects from updates (by throwing an exception and killing the session). But my method is never called.

Code:
Code:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class RepObject {
...
   private Changeset replicatedChangeset;

   /**
    * The changeset in which this object was created.  Null for head objects; set for all
    * versioned copies.
    */
   @ManyToOne
   public Changeset getReplicatedChangeset ()
   {
      return replicatedChangeset;
   }
...
   @PreUpdate
   private void checkUpdate () {
      log.debug("Calling checkUpdate on " + this + "...");
      if (replicatedChangeset != null) {
         throw new IllegalStateException("Can't update snapshotted object " + this);
      }
   }

The idea is that a RepObject can be copied, and the copy has a non-null "Changeset". The copy then should never ever be updated again. Basically I want it to be "dynamically immutable" (I wish there was a way to flag a particular object as immutable at the Hibernate level, but the best I can do is blow up if you change it and then try to flush).

The test is this. (BlogPostImpl is of course a subclass of RepObject.)
Code:
            database.enableFilter("priorVersion").setParameter("version", last.getId());
            List<BlogPostImpl> priorBlogPosts = database.createQuery("from BlogPostImpl").list();

            try {
               BlogPost prior1 = priorBlogPosts.get(0);
               prior1.setTitle("changedTitle");

               // this should call the @PreUpdate on RepObject, which should blow up here
               // ... but doesn't!  and I can't even see where the @PreUpdate gets read!
               database.flush();

               // assert false; // plan to die if we get this far; should get exception from the flush
            } catch (Exception e) {
               // you should never do this, so we don't attempt to support any sort of recovery!
               log.info("Got expected exception from attempted snapshot update", e);
            }

But my @PreUpdate method is never called, no exception gets thrown, and I hit the "assert false". And I can see in the log output that the flush *is* causing a single update:
Code:
DEBUG 06-07 08:49:12,353 (Log4JLogger.java:debug:84)  -Flushed: 0 insertions, 1 updates, 0 deletions to 4 objects
... DEBUG 06-07 08:49:12,393 (Log4JLogger.java:debug:84)  -update RepObject set version=?, replicatedChangeset_id=?, replicatedKey=? where id=? and version=?
... DEBUG 06-07 08:49:12,433 (Log4JLogger.java:debug:84)  -update BlogPostImpl set content=?, title=? where repobject_id=?


In digging through hibernate-annotations source, I can't see where AnnotationBinder ever looks at the Entity methods to see which of them are flagged with EJB3 callbacks! I would like to trace through the code that sets up the callbacks on this Entity, but I can't find it to trace through!

What am I missing? Can anyone confirm or disconfirm that EJB3 persistence lifecycle annotations actually work with Seam @Entities? I will also post this in the Hibernate EJB3 forum....
Cheers,
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 2:53 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
Arrgh, never mind. I fundamentally misunderstood how this works. Should have searched before posting. Now to create a real listener class and add @EntityListeners on my actual @Entity....

Sorry about that,
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 5:29 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
Actually, it looks like I *didn't* misunderstand how this works. This post makes it look like my problem may be that I am using the Seam Hibernate example, which doesn't actually have an embedded EJB container, so therefore the EJB3 callbacks aren't working.

Another possibility is that I need to use an @EntityListener instead, but that may ALSO not work if I don't have an embedded EJB3 container.

Anyway, should know more soon... if anyone has any concrete knowledge here, please share :-)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 9:05 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You do not need an EJB 3.0 container, but you must use Hibernate EntityManager to benefit the EJB 3.0 entity lifecycles.

_________________
Emmanuel


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