use of org.hibernate.orm.test.sql.hand.Person in project hibernate-orm by hibernate.
the class NativeSQLQueriesTest method testSQLQueryInterface.
@Test
public void testSQLQueryInterface(SessionFactoryScope scope) {
Organization ifa = new Organization("IFA");
Organization jboss = new Organization("JBoss");
Person gavin = new Person("Gavin");
Employment emp = new Employment(gavin, jboss, "AU");
scope.inTransaction(session -> {
session.persist(ifa);
session.persist(jboss);
session.persist(gavin);
session.persist(emp);
session.flush();
List l = session.createNativeQuery(getOrgEmpRegionSQL()).addEntity("org", Organization.class).addJoin("emp", "org.employments").addScalar("regionCode", StandardBasicTypes.STRING).list();
assertEquals(2, l.size());
l = session.createNativeQuery(getOrgEmpPersonSQL()).addEntity("org", Organization.class).addJoin("emp", "org.employments").addJoin("pers", "emp.employee").list();
assertEquals(1, l.size());
});
scope.inTransaction(session -> {
List l = session.createNativeQuery("select {org.*}, {emp.*} " + "from ORGANIZATION org " + " left outer join EMPLOYMENT emp on org.ORGID = emp.EMPLOYER, ORGANIZATION org2").addEntity("org", Organization.class).addJoin("emp", "org.employments").setResultListTransformer(new ResultListTransformer() {
@Override
public List transformList(List list) {
List<Object> result = new ArrayList<>(list.size());
Map<Object, Object> distinct = new IdentityHashMap<>();
for (Object entity : list) {
if (distinct.put(entity, entity) == null) {
result.add(entity);
}
}
return result;
}
}).list();
assertEquals(2, l.size());
});
scope.inTransaction(session -> {
session.delete(emp);
session.delete(gavin);
session.delete(ifa);
session.delete(jboss);
});
}
use of org.hibernate.orm.test.sql.hand.Person in project hibernate-orm by hibernate.
the class NativeSQLQueriesTest method testCompositeIdJoins.
@Test
@SuppressWarnings({ "unchecked" })
@FailureExpected(jiraKey = "unknown")
public void testCompositeIdJoins(SessionFactoryScope scope) {
scope.inTransaction(session -> {
Person person = new Person();
person.setName("Noob");
Product product = new Product();
product.setProductId(new Product.ProductId());
product.getProductId().setOrgid("x");
product.getProductId().setProductnumber("1234");
product.setName("Hibernate 3");
Order order = new Order();
order.setOrderId(new Order.OrderId());
order.getOrderId().setOrdernumber("1");
order.getOrderId().setOrgid("y");
product.getOrders().add(order);
order.setProduct(product);
order.setPerson(person);
session.save(product);
session.save(order);
session.save(person);
});
scope.inTransaction(session -> {
Product p = (Product) session.createQuery("from Product p join fetch p.orders").list().get(0);
assertTrue(Hibernate.isInitialized(p.getOrders()));
});
scope.inTransaction(session -> {
Object[] o = (Object[]) session.createNativeQuery("select\r\n" + " product.orgid as {product.id.orgid}," + " product.productnumber as {product.id.productnumber}," + " {prod_orders}.orgid as orgid3_1_,\r\n" + " {prod_orders}.ordernumber as ordernum2_3_1_,\r\n" + " product.name as {product.name}," + " {prod_orders.element.*}" + /*" orders.PROD_NO as PROD4_3_1_,\r\n" +
" orders.person as person3_1_,\r\n" +
" orders.PROD_ORGID as PROD3_0__,\r\n" +
" orders.PROD_NO as PROD4_0__,\r\n" +
" orders.orgid as orgid0__,\r\n" +
" orders.ordernumber as ordernum2_0__ \r\n" +*/
"from\r\n" + " Product product \r\n" + " inner join\r\n" + " TBL_ORDER {prod_orders} \r\n" + " on product.orgid={prod_orders}.PROD_ORGID \r\n" + " and product.productnumber={prod_orders}.PROD_NO").addEntity("product", Product.class).addJoin("prod_orders", "product.orders").list().get(0);
Product p = (Product) o[0];
assertTrue(Hibernate.isInitialized(p.getOrders()));
assertNotNull(p.getOrders().iterator().next());
});
}
use of org.hibernate.orm.test.sql.hand.Person in project hibernate-orm by hibernate.
the class NativeSQLQueriesTest method testResultSetMappingDefinition.
@Test
public void testResultSetMappingDefinition(SessionFactoryScope scope) {
Organization ifa = new Organization("IFA");
Organization jboss = new Organization("JBoss");
Person gavin = new Person("Gavin");
Employment emp = new Employment(gavin, jboss, "AU");
scope.inTransaction(session -> {
session.persist(ifa);
session.persist(jboss);
session.persist(gavin);
session.persist(emp);
session.flush();
List l = session.createNativeQuery(getOrgEmpRegionSQL(), "org-emp-regionCode").list();
assertEquals(2, l.size());
l = session.createNativeQuery(getOrgEmpPersonSQL(), "org-emp-person").list();
assertEquals(1, l.size());
session.delete(emp);
session.delete(gavin);
session.delete(ifa);
session.delete(jboss);
});
}
use of org.hibernate.orm.test.sql.hand.Person in project hibernate-orm by hibernate.
the class NativeSQLQueriesTest method testAutoDetectAliasing.
@Test
@SuppressWarnings({ "UnusedDeclaration", "deprecation", "UnusedAssignment" })
public void testAutoDetectAliasing(SessionFactoryScope scope) {
Organization ifa = new Organization("IFA");
Organization jboss = new Organization("JBoss");
Person gavin = new Person("Gavin");
Employment emp = new Employment(gavin, jboss, "AU");
scope.inTransaction(session -> {
Object orgId = session.save(jboss);
Object orgId2 = session.save(ifa);
session.save(gavin);
session.save(emp);
});
Employment emp2 = scope.fromTransaction(session -> {
List list = session.createNativeQuery(getEmploymentSQL()).addEntity(Employment.class.getName()).list();
assertEquals(1, list.size());
Employment _emp2 = (Employment) list.get(0);
assertEquals(_emp2.getEmploymentId(), emp.getEmploymentId());
assertEquals(_emp2.getStartDate().getDate(), emp.getStartDate().getDate());
assertEquals(_emp2.getEndDate(), emp.getEndDate());
return _emp2;
});
scope.inTransaction(session -> {
List list = session.createNativeQuery(getEmploymentSQL()).addEntity(Employment.class.getName()).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
assertEquals(1, list.size());
Map m = (Map) list.get(0);
assertTrue(m.containsKey("Employment"));
assertEquals(1, m.size());
list = session.createNativeQuery(getEmploymentSQL()).list();
assertEquals(1, list.size());
Object[] o = (Object[]) list.get(0);
assertEquals(8, o.length);
list = session.createNativeQuery(getEmploymentSQL()).setResultTransformer(new UpperCasedAliasToEntityMapResultTransformer()).list();
assertEquals(1, list.size());
m = (Map) list.get(0);
assertTrue(m.containsKey("EMPID"));
assertTrue(m.containsKey("AMOUNT"));
assertTrue(m.containsKey("ENDDATE"));
assertEquals(8, m.size());
list = session.createNativeQuery(getEmploymentSQLMixedScalarEntity()).addScalar("employerid").addEntity(Employment.class).list();
assertEquals(1, list.size());
o = (Object[]) list.get(0);
assertEquals(2, o.length);
assertClassAssignability(Number.class, o[0].getClass());
assertClassAssignability(Employment.class, o[1].getClass());
Query queryWithCollection = session.getNamedQuery("organizationEmploymentsExplicitAliases");
queryWithCollection.setParameter("id", jboss.getId());
list = queryWithCollection.list();
assertEquals(list.size(), 1);
session.clear();
list = session.createNativeQuery(getOrganizationJoinEmploymentSQL()).addEntity("org", Organization.class).addJoin("emp", "org.employments").list();
assertEquals(2, list.size());
session.clear();
list = session.createNativeQuery(getOrganizationFetchJoinEmploymentSQL()).addEntity("org", Organization.class).addJoin("emp", "org.employments").list();
assertEquals(2, list.size());
session.clear();
// TODO : why twice?
session.getNamedQuery("organizationreturnproperty").list();
list = session.getNamedQuery("organizationreturnproperty").list();
assertEquals(2, list.size());
session.clear();
list = session.getNamedQuery("organizationautodetect").list();
assertEquals(2, list.size());
});
scope.inTransaction(session -> {
session.delete(emp2);
session.delete(jboss);
session.delete(gavin);
session.delete(ifa);
});
scope.inTransaction(session -> {
Dimension dim = new Dimension(3, 30);
session.save(dim);
List list = session.createNativeQuery("select d_len * d_width as surface, d_len * d_width * 10 as volume from Dimension").list();
session.delete(dim);
});
scope.inTransaction(session -> {
SpaceShip enterprise = new SpaceShip();
enterprise.setModel("USS");
enterprise.setName("Entreprise");
enterprise.setSpeed(50d);
Dimension d = new Dimension(45, 10);
enterprise.setDimensions(d);
session.save(enterprise);
session.flush();
Object[] result = (Object[]) session.getNamedQuery("spaceship").uniqueResult();
assertEquals(3, result.length, "expecting 3 result values");
enterprise = (SpaceShip) result[0];
assertTrue(50d == enterprise.getSpeed());
assertTrue(450d == extractDoubleValue(result[1]));
assertTrue(4500d == extractDoubleValue(result[2]));
session.delete(enterprise);
});
}
use of org.hibernate.orm.test.sql.hand.Person in project hibernate-orm by hibernate.
the class NativeSQLQueriesTest method testMappedAliasStrategy.
@Test
@SuppressWarnings({ "deprecation", "UnusedDeclaration" })
public void testMappedAliasStrategy(SessionFactoryScope scope) {
Organization ifa = new Organization("IFA");
Organization jboss = new Organization("JBoss");
Person gavin = new Person("Gavin");
Employment emp = new Employment(gavin, jboss, "AU");
scope.inTransaction(session -> {
Object orgId = session.save(jboss);
Object orgId2 = session.save(ifa);
session.save(gavin);
session.save(emp);
});
scope.inTransaction(session -> {
Query namedQuery = session.getNamedQuery("AllEmploymentAsMapped");
List list = namedQuery.list();
assertEquals(1, list.size());
Employment emp2 = (Employment) list.get(0);
assertEquals(emp2.getEmploymentId(), emp.getEmploymentId());
assertEquals(emp2.getStartDate().getDate(), emp.getStartDate().getDate());
assertEquals(emp2.getEndDate(), emp.getEndDate());
});
scope.inTransaction(session -> {
Query sqlQuery = session.getNamedQuery("EmploymentAndPerson");
sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
List list = sqlQuery.list();
assertEquals(1, list.size());
Object res = list.get(0);
assertClassAssignability(Map.class, res.getClass());
Map m = (Map) res;
assertEquals(2, m.size());
});
scope.inTransaction(session -> {
Query sqlQuery = session.getNamedQuery("organizationreturnproperty");
sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
List list = sqlQuery.list();
assertEquals(2, list.size());
Map m = (Map) list.get(0);
assertEquals(1, m.size());
assertTrue(m.containsKey("org"));
assertClassAssignability(m.get("org").getClass(), Organization.class);
if (jboss.getId() == ((Organization) m.get("org")).getId()) {
assertTrue(Hibernate.isInitialized(((Organization) m.get("org")).getEmployments()));
}
Map m2 = (Map) list.get(1);
assertEquals(1, m.size());
assertTrue(m2.containsKey("org"));
assertClassAssignability(m2.get("org").getClass(), Organization.class);
if (ifa.getId() == ((Organization) m2.get("org")).getId()) {
assertTrue(Hibernate.isInitialized(((Organization) m2.get("org")).getEmployments()));
}
});
scope.inTransaction(session -> {
Query namedQuery = session.getNamedQuery("EmploymentAndPerson");
List list = namedQuery.list();
assertEquals(1, list.size());
Object[] objs = (Object[]) list.get(0);
assertEquals(2, objs.length);
Employment emp2 = (Employment) objs[0];
Person _gavin = (Person) objs[1];
session.delete(emp2);
session.delete(jboss);
session.delete(_gavin);
session.delete(ifa);
});
}
Aggregations