instead. When the merge() method is called, the EntityManager will merge the state of the entity into the
current persistence context.
Let's test the insert operation. Listing 10-18 shows the code snippet for insert a new contact record.
Listing 10-18. Testing Insert Operation
// Other code omitted
public class JpaSample {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:app-context.xml");
ctx.refresh();
ContactService contactService = ctx.getBean(
"jpaContactService", ContactService.class);
// Add new contact
Contact contact = new Contact();
contact.setFirstName("Michael");
contact.setLastName("Jackson");
contact.setBirthDate(new Date());
ContactTelDetail contactTelDetail =
new ContactTelDetail("Home", "1111111111");
contact.addContactTelDetail(contactTelDetail);
contactTelDetail = new ContactTelDetail("Mobile", "2222222222");
contact.addContactTelDetail(contactTelDetail);
contactService.save(contact);
List<Contact> contacts = contactService.findAllWithDetail();
listContactsWithDetail(contacts);
}
private static void listContactsWithDetail(List<Contact> contacts) {
System.out.println("");
System.out.println("Listing contacts with details:");
for (Contact contact: contacts) {
System.out.println(contact);
if (contact.getContactTelDetails() != null) {
for (ContactTelDetail contactTelDetail:
contact.getContactTelDetails()) {
System.out.println(contactTelDetail);
}
}
if (contact.getHobbies() != null) {
for (Hobby hobby: contact.getHobbies()) {
System.out.println(hobby);
}
}
System.out.println();
}
}
}
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home