File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes Callback Listener methods in EJB3.0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Callback Listener methods in EJB3.0" Watch "Callback Listener methods in EJB3.0" New topic

Callback Listener methods in EJB3.0

padma warlu

Joined: Aug 17, 2005
Posts: 8

I have a requirement like whenever my data is persisted into the the database at the same time i should be able to update or insert a new record into another table. For this, i feel that callback methods are the best place to implement.

I have a Customer entity. and also i have a UserLog entity. I have a method in my session bean - insertCustomer.

public void insertCustomer(Customer customer)
So i thought to use entitymanager.persist(userlog) in postPersist method of Callback Listener class of Customer entity to store the data into UserLog table.
But, First of all it is not allowing me to use the Entity Manager in callback methods. So how do i solve this problem. In that case, what is the purpose of callback methods if we cant perform the operations on entity manager in them.

Pls suggest.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410


Please check your private messages.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
more code is required to help solve your issue,
boil it down to the minimum if you can,
use code tags when you post

Bill Shirley - bshirley -
if (Posts < 30) JavaRanchFAQ);
padma warlu

Joined: Aug 17, 2005
Posts: 8
Hi Bill, here is the detail code about my problem.

public class EmployeeFacadeBean implements EmployeeFacade{
private EntityManager manager;
public void createDepartment(String deptName){
Department department = new Department();

@Table(name = "DEPARTMENTS")
public class Department implements{
private Long id;
private String lsName;
private Collection<Employee> lEmployees;

@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId(){
return id;
public void setId(Long id){ = id;
@Column(name = "NAME")
public String getName(){
return lsName;
public void setName(String asName){
this.lsName = asName;
@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
public Collection<Employee> getEmployees(){
return lEmployees;

public void setEmployees(Collection<Employee> aEmployees){
this.lEmployees = aEmployees;

public void addEmployee(String asName,int aiAge, String asSex){
if(lEmployees== null)lEmployees = new ArrayList<Employee>();

Employee lEmployee = new Employee();

public class EmployeeListener {
public void insertLog() {
private EntityManager manager;
Log log = new Log();
log.setDescription("Employee Add");

Now, whenever i add a record into an employee database, i want to add a record in even Log database table also. so i thought to implement in postPersist callback method. But it is not allowing me to create the entitymanager in call back methods. So how do i perform this operation.

Otherwise, can i use java.sql.Connection object to perform my desired operation instead of relying on the Entities. see the code with java.sql.Connection.

public void insertLog() {
Statement st = con.createStatement();
String sqlStr = "INSERT INTO LOG VALUES(test,Employee Added)";

Am i donig anything wrong in callback method implementation to solve my problem? How far it is a vioable solution of using connection object in the callback methods to update the data into other entities?

Pls suggest how to solve this problem. Thanks in advance.
I agree. Here's the link:
subject: Callback Listener methods in EJB3.0
It's not a secret anymore!