Query countQuery = managedEm.createQuery("SELECT Count(c) from Car c");
assertEquals(0l, countQuery.getSingleResult());
ut.begin();
try {
Car car = new Car();
car.setNumberOfSeats(5);
car.setEngineSize(1200);
car.setColour("blue");
car.setNumberPlate("A1AAA");
managedEm.persist(car);
car = new Car();
car.setNumberOfSeats(7);
car.setEngineSize(1800);
car.setColour("green");
car.setNumberPlate("B2BBB");
managedEm.persist(car);
} finally {
ut.commit();
}
assertEquals(2l, countQuery.getSingleResult());
TypedQuery<Car> carQuery = managedEm.
createQuery("Select c from Car c ORDER by c.engineSize", Car.class);
List<Car> list = carQuery.getResultList();
assertEquals(2l, list.size());
assertEquals(5, list.get(0).getNumberOfSeats());
assertEquals(1200, list.get(0).getEngineSize());
assertEquals("blue", list.get(0).getColour());
assertEquals("A1AAA", list.get(0).getNumberPlate());
assertEquals(7, list.get(1).getNumberOfSeats());
assertEquals(1800, list.get(1).getEngineSize());
assertEquals("green", list.get(1).getColour());
assertEquals("B2BBB", list.get(1).getNumberPlate());
ut.begin();
try {
Car car = managedEm.find(Car.class, "A1AAA");
car.setNumberOfSeats(2);
car.setEngineSize(2000);
car.setColour("red");
car = managedEm.find(Car.class, "B2BBB");
managedEm.remove(car);
car = new Car();
car.setNumberOfSeats(2);
car.setEngineSize(800);
car.setColour("black");
car.setNumberPlate("C3CCC");
managedEm.persist(car);
} finally {
ut.commit();
}