5 Replies Latest reply on Sep 5, 2006 3:20 PM by Alex Titov

    How to know when entity is removed?

    Alex Titov Newbie

      I am studying EJB 3 and there is one thing, that I cannot understand yet. In EJB 2.x in entity beans there is method named ejbRemove. I can override it and do some work when entity is been removed. For example I can send JMS messages to registered subscribers. And how can I do the same in EJB 3.0? I could send messages from session facade, but what should I do if entity is removed by cascade? Entity must be serializable because it will be used remotely.

        • 1. Re: How to know when entity is removed?
          Andrew Rubinger Master

          The lifecycle methods in EJB3 are defined through annotations - looks like you'd like @PostRemove.

          http://trailblazer.demo.jboss.com/EJB3Trail/persistence/entitylifecycle/index.html

          S,
          ALR

          • 2. Re: How to know when entity is removed?
            Alex Titov Newbie

             

            The lifecycle methods in EJB3 are defined through annotations - looks like you'd like @PostRemove.


            But how can I send JMS messages in this method? I think I cannot use neither EntityManager nor local interfaces inside entity (like in EJB 2.x)... Or I can?

            • 3. Re: How to know when entity is removed?
              Andrew Rubinger Master

              Hmm...never tried throwing an EntityManager into an entity itself - probably not the best practice. I don't remember reading anything prohibiting that injection...But why do you need it do send JMS?

              First solution that occurs to me would be to separate your listeners from the entity using @EntityListener. From that externalized class you can either send your JMS messages or delegate out to a Session bean to do it for you...

              S,
              ALR

              • 4. Re: How to know when entity is removed?
                Alex Titov Newbie

                Well perhaps, I really do not need to throw an EntityManager to send JMS. But I would like to use local beans (because they work faster) in entity listener.
                I need JMS (or some thing like this) to notify remote clients when something happens with entity (insert, update or remove). So, I have to code this logic in life cycle methods.
                After some researches I found the way to do that.

                @EntityListeners(EntityListener.class)
                public class RelatedEntity implements Serializable {
                ...
                }
                
                public class EntityListener{
                 @PostLoad
                 public void onLoad(Object obj){
                 try {
                 InitialContext ctx=new InitialContext();
                 EntityLocalHome localHome=(EntityLocalHome) ctx.lookup("Entity1Home/local");
                 localHome.onLoad(obj);
                 } catch (NamingException e) {
                 e.printStackTrace();
                 }
                 }
                
                 public EntityListener() {
                 System.out.println("Creating entity listener");
                 }
                }
                

                May be there is a way to do it better (and more elegant)?
                I was disappointed, that I could not use dependency injection here (instead of lookup). But it works.
                There is only one thing, that confuses me. Why the constructor of the EntityListener is never invoked?
                I think that EJB 3.0 is not designed for remote clients, only for web interfaces...

                • 5. Re: How to know when entity is removed?
                  Alex Titov Newbie

                  About the constructor... I understood, that it is invoked on deployment.