Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!

padma warlu

Greenhorn
+ Follow
since Aug 17, 2005
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by padma warlu

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.
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.
Hi,

If i would like to update the data using entitymanager.merge, do i need to first find the object by using em.find(xxx.class,id) and set the modified data to the retrieved object and then call em.merge(POJO).

eg.
Employee employee = em.find(Employee.class,"E1001");
employee.setName("asdf");
employee.setSalary("2323");
em.merge(employee);

OR

what if i pass the updated POJO and without using em.find, if i directly use em.merge(POJO), will it be a problem?

eg.
em.merge(employee);

Pls suggest which patten should i follow.
Hello,

I have written a small example in EJB3.0. This has got a simple session bean and an entity. Iam calling entitymanager.persist() in session bean business method to persist the data into database. But due to some problem, it got a runtime exception and displayed full stacktrace of that exception. I have try catch block in session bean business method and i have not given ex.printStackTrace() in catch, but still it is printing the full stacktrace. I think, it is being thrown by the toplink essentials. Do i need to configure anything in persistence.xml file? Is there anyway to not to print full stack trace whenever there is any exception. Rather i just need the short description of the error which i can get using ex.toString().

Iam using OC4J 10.1.3.3 as Application Server.

Pls help.
I would like to know whether there are any performance problems in EJB3.0. If so, could anyone pls list out those issues.
Hello,

I am bit confused with @SecondaryTable annotation in EJB3.0. By its definition, i understood that, if the data need to be stored across
multiple tables then we have to use @SecondaryTable annotation. Anyway, we have to have different setters and getters for those fields whichever are needed to store in the database table given in the @SecondaryTable annotation. And the fields which were mapped to other table using @Column(name = "AGE", table="EMPLOYEE_AGE") are anyway not saved in the primary entity say EMPLOYEE. Then what is the difference of storing the data into different tables using other child entities and using @SecondaryTable annotation?

Instead of keep all setters and getters which are meant for different table in the same entity, why cant we use seperate entities to store the data into that particular table which is mapped to that particular entity?

Note: EMPLOYEE is Primary Table and EMPLOYEE_AGE is secondary table.

Pls clarify.
But what if the same EJB is being called from the client ie servlet or jsp which is in the different container ?
I want my EJB to behave as remote and local as well. If my EJB is being called from client ( say servlet ) then there sbould be a remote call. If my EJB is being called from any other EJB then it should be a local call.

Using local interfaces we can do. But my EJB should be remote and local as well.

for this i have an aproach. I dont know how far it is correct and reliable.

the aproach is as follows :

I will declare my EJB in the ejb-jar.xml for remote block and also for local block. but with different ejb names. I use remote ejb name to lookup from the servlet. and i use local ejb name to lookup from another EJB.
Will this be a good approach. Or there are any other alternatives to acheive this type of issue.

Thanks with Regards
Venkat