Home JBoss howto EJB How do you keep your EJB callbacks separated from biz. methods?
30 | 07 | 2010
JBoss 5 AS Book
"JBoss AS 5 development" reviews
Please share your feedback/review with other readers!
Banner
Dashboard
Advertise with Us
Banner
RSS Feed
Java EE 1.6 resources
Login
Sign here for the NewsLetter.



JBoss admin resources
Banner
Java EE 1.6 resources
JBoss howto

How to avoid the 50 seconds start up limit in Eclipse ?

JBoss recipe of the day ...
Read More

How can you solve deployment errors caused by large war/jar/ear files ?

jboss recipe of the day ...
Read More

How do you configure your .war to be deployed after your EJB ?

jboss recipe of the day ...
Read More
How do you keep your EJB callbacks separated from biz. methods?
Written by Mark S.   

JBoss daily recipe

If you have need to use callback methods in your EJB Entity Beans then it's likely that your code is a mess! With EJB 3.0 you can add the  @EntityListeners annotation to indicate that the callback methods are located in another class.

Example: recall the Note class in the EJB 3.0 tutorial:

 

 

  
import java.io.Serializable;     
import javax.persistence.Entity;   
import javax.persistence.GeneratedValue;   
import javax.persistence.Id;   
import javax.persistence.Table;   
  
@Entity  
@Table(name = "NOTE")   
@EntityListeners(NoteCallbackListener.class)
  
public class Note implements Serializable {   
long noteId;   
  
String text;   
  
String actor;   
  
String attachment;   
  
@Id  
@GeneratedValue  
public long getNoteId() {   
 return noteId;   
}   
  
public void setNoteId(long noteId) {   
 this.noteId = noteId;   
}   
  
public String getActor() {   
 return actor;   
}   
  
public void setActor(String actor) {   
 this.actor = actor;   
}   
  
public String getAttachment() {   
 return attachment;   
}   
  
public void setAttachment(String attachment) {   
 this.attachment = attachment;   
}   
  
public String getText() {   
 return text;   
}   
  
public void setText(String text) {   
 this.text = text;   
}   
}


and this is the Class holding the listener:

import javax.persistence.PreRemove;
import javax.persistence.PostRemove;
import javax.persistence.PreUpdate;
import javax.persistence.PostUpdate;
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.PostPersist;

public class NoteCallbackListener
{
   @PrePersist
   public void doPrePersist(Note note)
   {
      System.out.println("doPrePersist: About to create Note: " + Note.getId());
   }

   @PostPersist
   public void doPostPersist(Object Note)
   {
      System.out.println("doPostPersist: Created Note: " + ((Note)Note).getId()));
   }

   @PreRemove
   public void doPreRemove(Note note)
   {
      System.out.println("doPreRemove: About to delete Note: " + Note.getId());
   }

   @PostRemove
   public void doPostRemove(Note note)
   {
      System.out.println("doPostRemove: Deleted Note: " + Note.getId());
   }

   @PreUpdate
   public void doPreUpdate(Note note)
   {
      System.out.println("doPreUpdate: About to update Note: " + Note.getId());
   }

   @PostUpdate
   public void doPostUpdate(Note note)
   {
      System.out.println("doPostUpdate: Updated Note: " + Note.getId());
   }

   @PostLoad
   public void doPostLoad(Note note)
   {
      System.out.println("doPostLoad: Loaded Note: " + Note.getId());
   }

JBoss.org Search
Custom Search
Comments
Search
Jonny   |2008-10-08 14:46:03
Cool! Learnt a new thing
Only registered users can write comments!

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."