public void testFiltersWithJoinedSubclass() { Session s = openSession(); s.enableFilter("region").setParameter("userRegion", "US"); Transaction t = s.beginTransaction(); prepareTestData(s); s.clear(); List results; Iterator itr; results = s.createQuery("from Person").list(); assertEquals("Incorrect qry result count", 4, results.size()); s.clear(); results = s.createQuery("from Employee").list(); assertEquals("Incorrect qry result count", 2, results.size()); s.clear(); results = new ArrayList( new HashSet(s.createQuery("from Person as p left join fetch p.minions").list())); assertEquals("Incorrect qry result count", 4, results.size()); itr = results.iterator(); while (itr.hasNext()) { // find john final Person p = (Person) itr.next(); if (p.getName().equals("John Doe")) { Employee john = (Employee) p; assertEquals("Incorrect fecthed minions count", 1, john.getMinions().size()); break; } } s.clear(); results = new ArrayList( new HashSet(s.createQuery("from Employee as p left join fetch p.minions").list())); assertEquals("Incorrect qry result count", 2, results.size()); itr = results.iterator(); while (itr.hasNext()) { // find john final Person p = (Person) itr.next(); if (p.getName().equals("John Doe")) { Employee john = (Employee) p; assertEquals("Incorrect fecthed minions count", 1, john.getMinions().size()); break; } } t.commit(); s.close(); }
@Test @Transactional public void testSaveAndGet() throws Exception { Session session = sessionFactory.getCurrentSession(); Order order = new Order(); order.getItems().add(new Item()); session.save(order); session.flush(); // Otherwise the query returns the existing order (and we didn't set the // parent in the item)... session.clear(); Order other = (Order) session.get(Order.class, order.getId()); assertEquals(1, other.getItems().size()); assertEquals(other, other.getItems().iterator().next().getOrder()); }
@Test @Transactional public void testSaveAndFind() throws Exception { Session session = sessionFactory.getCurrentSession(); Order order = new Order(); Item item = new Item(); item.setProduct("foo"); order.getItems().add(item); session.save(order); session.flush(); // Otherwise the query returns the existing order (and we didn't set the // parent in the item)... session.clear(); Order other = (Order) session .createQuery("select o from Order o join o.items i where i.product=:product") .setString("product", "foo") .uniqueResult(); assertEquals(1, other.getItems().size()); assertEquals(other, other.getItems().iterator().next().getOrder()); }