T
Taras_96
i all,
I'm trying to gain a better understanding of some J2EE concepts, in
particular entities. AFAIK, if you make changes to an object using a
CMP Entity Bean, those changes are written to the database
automatically:
Customer customer =
// ... obtain a remote
//reference to the bean
// get the customer's address
Address addr = customer.getAddress();
// change the zip code
addr.zip = "56777";
// update the customer's address
customer.setAddress(addr); <- the address field in the database has
now been updated
In 'Java EE with Glassfish application server', the author writes:
"
customer3 = entityManager.find(Customer.class, 4L);
customer3.setLastName("Johnson");
entityManager.persist(customer3);
"
What is the purpose of the 'persist' method, and how is it different
to the 'merge' method? Notice that in the EJB 2.1 code, I didn't have
to explicitly tell the code to update the DB, this was done
automatically.
Furthermore, the book uses a Session Bean to implement a DAO to the
JPA unit:
@Remote
public interface CustomerDao
{
public void saveCustomer(Customer customer);
public Customer getCustomer(Long customerId);
public void deleteCustomer(Customer customer);
}
what is the purpose of the 'saveCustomer' method? Do you have to
explicitly tell the code to save any changes made to objects that are
stored in a database?
eg:
Customer cust = dao.getCustomer(1);
cust.setLastName('foo');
dao.saveCustomer('cust');
I thought the point of CMP was so that you didn't have to explicitly
save objects; that any changes made to the objects in the code were
automatically reflected in the database representation of the object.
The 2.1 code automatically updates the appropriate field, whereas in
the 3.0 code you have to explicitly call save - in 3.0 do you have to
explicitly save changes to the database, whereas in 2.1 this was done
for you by using the Entity Bean?
Thanks
Taras
I'm trying to gain a better understanding of some J2EE concepts, in
particular entities. AFAIK, if you make changes to an object using a
CMP Entity Bean, those changes are written to the database
automatically:
Customer customer =
// ... obtain a remote
//reference to the bean
// get the customer's address
Address addr = customer.getAddress();
// change the zip code
addr.zip = "56777";
// update the customer's address
customer.setAddress(addr); <- the address field in the database has
now been updated
In 'Java EE with Glassfish application server', the author writes:
"
customer3 = entityManager.find(Customer.class, 4L);
customer3.setLastName("Johnson");
entityManager.persist(customer3);
"
What is the purpose of the 'persist' method, and how is it different
to the 'merge' method? Notice that in the EJB 2.1 code, I didn't have
to explicitly tell the code to update the DB, this was done
automatically.
Furthermore, the book uses a Session Bean to implement a DAO to the
JPA unit:
@Remote
public interface CustomerDao
{
public void saveCustomer(Customer customer);
public Customer getCustomer(Long customerId);
public void deleteCustomer(Customer customer);
}
what is the purpose of the 'saveCustomer' method? Do you have to
explicitly tell the code to save any changes made to objects that are
stored in a database?
eg:
Customer cust = dao.getCustomer(1);
cust.setLastName('foo');
dao.saveCustomer('cust');
I thought the point of CMP was so that you didn't have to explicitly
save objects; that any changes made to the objects in the code were
automatically reflected in the database representation of the object.
The 2.1 code automatically updates the appropriate field, whereas in
the 3.0 code you have to explicitly call save - in 3.0 do you have to
explicitly save changes to the database, whereas in 2.1 this was done
for you by using the Entity Bean?
Thanks
Taras