Java Reference
In-Depth Information
public void testPendingOrder() {
PendingOrder po = new PendingOrder();
save(po);
}
}
A simple test will typically catch some basic O/R mapping problems. However, you
will usually want to write a more elaborate test that also updates the persistent
object and possibly deletes it. Listing 6.2 shows a test that takes a PendingOrder
through its lifecycle, which creates and destroys relationships with other objects,
including restaurants and line items. It consists of the following steps:
Create a PendingOrder and save it.
1
Load PendingOrder , update delivery information, and save it.
2
Load PendingOrder , update the restaurant, and save it.
3
Load PendingOrder , update quantities, and save it.
4
Load PendingOrder , update quantities, and save it (again to test deleting
line items).
5
Load PendingOrder , update payment information, and save it.
6
Delete PendingOrder .
7
Each step is executed within a transaction that keeps a Hibernate Session open so
objects can be loaded lazily.
Listing 6.2
HibernatePendingOrderPersistenceTests
public class HibernatePendingOrderPersistenceTests extends
HibernatePersistenceTests {
private RestaurantRepository restaurantRepository;
private String pendingOrderId;
private String restaurantId;
protected Properties getSessionFactoryProperties() {
return new Properties();
}
public void setUp() throws Exception {
super.setUp();
restaurantRepository =
new HibernateRestaurantRepositoryImpl(
getHibernateTemplate());
B Creates Hibernate
RestaurantRepositoryImpl
C Initializes
database
delete(PendingOrder.class);
delete(MenuItem.class);
Search WWH ::




Custom Search