File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
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
Author

Callback Listener methods in EJB3.0

padma warlu
Greenhorn

Joined: Aug 17, 2005
Posts: 8
Hello,

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)
{
entitymanager.persist(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
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"vwarlu",

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 - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
padma warlu
Greenhorn

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

@Stateless
public class EmployeeFacadeBean implements EmployeeFacade{
@PersistenceContext
private EntityManager manager;
public void createDepartment(String deptName){
Department department = new Department();
department.setName(deptName);
manager.persist(department);
}
...
....
}

@Entity(name="DEPARTMENT")
@EntityListener(EmployeeListener.class)
@Table(name = "DEPARTMENTS")
public class Department implements java.io.Serializable{
private Long id;
private String lsName;
private Collection<Employee> lEmployees;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId(){
return id;
}
public void setId(Long id){
this.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();
lEmployee.setName(asName);
lEmployee.setAge(aiAge);
lEmployee.setSex(asSex);
lEmployee.setDepartment(this);
lEmployees.add(lEmployee);
setEmployees(lEmployees);
}
}

public class EmployeeListener {
@PostPersist
public void insertLog() {
@PersistenceContext
private EntityManager manager;
Log log = new Log();
log.setName("test");
log.setDescription("Employee Add");
manager.persist(log);
}
...
...
}

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.

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

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.
 
 
subject: Callback Listener methods in EJB3.0
 
Similar Threads
ejbCreate( ) + ejbRemove( )
why noy make all my service class methos or DAO class methods static?
Access to entity manager in Session Bean
Audit Log
Synchronization in DAOs ?