use of org.hibernate.Session in project hibernate-orm by hibernate.
the class JoinedSubclassFilterTest method testFiltersWithJoinedSubclass.
@Test
@SuppressWarnings({ "unchecked" })
public void testFiltersWithJoinedSubclass() {
Session s = openSession();
s.enableFilter("region").setParameter("userRegion", "US");
Transaction t = s.beginTransaction();
prepareTestData(s);
s.clear();
List 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());
Iterator 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", 2, john.getMinions().size());
break;
}
}
s.clear();
// TODO : currently impossible to define a collection-level filter w/ joined-subclass elements that will filter based on a superclass column and function correctly in (theta only?) outer joins;
// this is consistent with the behaviour of a collection-level where.
// this might be one argument for "pulling" the attached class-level filters into collection assocations,
// although we'd need some way to apply the appropriate alias in that scenario.
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", 2, 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", 2, john.getMinions().size());
break;
}
}
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.createQuery("delete Customer where contactOwner is not null").executeUpdate();
s.createQuery("delete Employee where manager is not null").executeUpdate();
s.createQuery("delete Person").executeUpdate();
t.commit();
s.close();
}
use of org.hibernate.Session in project hibernate-orm by hibernate.
the class SubqueryTest method withLimit.
private void withLimit(Consumer<Session> consumer) {
rebuildSessionFactory(c -> c.addSqlFunction("limit", LIMIT_FUNCTION));
try {
Session s = openSession();
consumer.accept(s);
} finally {
// Rebuild to remove the function
rebuildSessionFactory();
}
}
use of org.hibernate.Session in project hibernate-orm by hibernate.
the class CompositeIdTypeBindingTest method testCompositeTypeBinding.
@Test
public void testCompositeTypeBinding() {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// prepare test data
Session session = openSession();
session.beginTransaction();
EmployeeGroup employeegroup = new EmployeeGroup(new EmployeeGroupId("a", "b"));
employeegroup.addEmployee(new Employee("stliu"));
employeegroup.addEmployee(new Employee("david"));
session.save(employeegroup);
employeegroup = new EmployeeGroup(new EmployeeGroupId("c", "d"));
employeegroup.addEmployee(new Employee("gail"));
employeegroup.addEmployee(new Employee("steve"));
session.save(employeegroup);
session.getTransaction().commit();
session.close();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Perform the test
session = openSession();
session.beginTransaction();
List<EmployeeGroupId> parameters = new ArrayList<EmployeeGroupId>();
parameters.add(new EmployeeGroupId("a", "b"));
parameters.add(new EmployeeGroupId("c", "d"));
parameters.add(new EmployeeGroupId("e", "f"));
List result = session.createQuery("select eg from EmployeeGroup eg where eg.id in (:employeegroupIds)").setParameterList("employeegroupIds", parameters).list();
Assert.assertEquals(2, result.size());
employeegroup = (EmployeeGroup) result.get(0);
Assert.assertEquals("a", employeegroup.getId().getGroupName());
Assert.assertNotNull(employeegroup.getEmployees());
Assert.assertEquals(2, employeegroup.getEmployees().size());
session.getTransaction().commit();
session.close();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// clean up test data
session = openSession();
session.beginTransaction();
List<EmployeeGroup> egs = session.createQuery("from EmployeeGroup").list();
for (EmployeeGroup eg : egs) {
eg.getEmployees().clear();
}
session.flush();
session.createQuery("delete from EmployeeGroup").executeUpdate();
session.createQuery("delete from Employee").executeUpdate();
session.getTransaction().commit();
session.close();
}
use of org.hibernate.Session in project hibernate-orm by hibernate.
the class SetSubselectTest method testSubselect.
@Test
public void testSubselect() {
Session s = openSession();
s.getTransaction().begin();
try {
Author b = new Author();
b.setName("Camilleri");
b.setId(1);
s.save(b);
Book book = new Book();
book.setId(2);
book.setAuthorId(1);
book.setTitle("Il sognaglio");
s.save(book);
Book book2 = new Book();
book2.setId(3);
book2.setAuthorId(1);
book2.setTitle("Il casellante");
s.save(book2);
s.getTransaction().commit();
} catch (Exception e) {
if (s.getTransaction().getStatus() == TransactionStatus.ACTIVE) {
s.getTransaction().rollback();
}
fail(e.getMessage());
} finally {
s.close();
}
s = openSession();
try {
Author author = s.get(Author.class, 1);
assertThat(author.getBooks().size(), is(2));
} finally {
s.close();
}
}
use of org.hibernate.Session in project hibernate-orm by hibernate.
the class SubselectTest method testEntitySubselect.
@Test
@SuppressWarnings({ "unchecked" })
public void testEntitySubselect() {
Session s = openSession();
Transaction t = s.beginTransaction();
Human gavin = new Human();
gavin.setName("gavin");
gavin.setSex('M');
gavin.setAddress("Melbourne, Australia");
Alien x23y4 = new Alien();
x23y4.setIdentity("x23y4$$hu%3");
x23y4.setPlanet("Mars");
x23y4.setSpecies("martian");
s.save(gavin);
s.save(x23y4);
s.flush();
List<Being> beings = (List<Being>) s.createQuery("from Being").list();
for (Being being : beings) {
assertNotNull(being.getLocation());
assertNotNull(being.getIdentity());
assertNotNull(being.getSpecies());
}
s.clear();
sessionFactory().getCache().evictEntityRegion(Being.class);
Being gav = (Being) s.get(Being.class, gavin.getId());
assertEquals(gav.getLocation(), gavin.getAddress());
assertEquals(gav.getSpecies(), "human");
assertEquals(gav.getIdentity(), gavin.getName());
s.clear();
//test the <synchronized> tag:
gavin = (Human) s.get(Human.class, gavin.getId());
gavin.setAddress("Atlanta, GA");
gav = (Being) s.createQuery("from Being b where b.location like '%GA%'").uniqueResult();
assertEquals(gav.getLocation(), gavin.getAddress());
s.delete(gavin);
s.delete(x23y4);
assertTrue(s.createQuery("from Being").list().isEmpty());
t.commit();
s.close();
}
Aggregations