• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Callback Listener methods in EJB3.0

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"vwarlu",

Please check your private messages.
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
more code is required to help solve your issue,
boil it down to the minimum if you can,
use code tags when you post
 
padma warlu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic