Search in sources :

Example 1 with AliasToBeanConstructorResultTransformer

use of org.hibernate.transform.AliasToBeanConstructorResultTransformer in project hibernate-orm by hibernate.

the class AbstractQueryCacheResultTransformerTest method testOneSelectNewAliasesList.

@Test
public void testOneSelectNewAliasesList() throws Exception {
    CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {

        protected Criteria getCriteria(Session s) throws Exception {
            return s.createCriteria(Student.class, "s").setProjection(Projections.property("s.name").as("name")).addOrder(Order.asc("s.studentNumber")).setResultTransformer(new AliasToBeanConstructorResultTransformer(getConstructor()));
        }

        private Constructor getConstructor() throws NoSuchMethodException {
            return StudentDTO.class.getConstructor(PersonName.class);
        }
    };
    HqlExecutor hqlExecutor = new HqlExecutor() {

        public Query getQuery(Session s) {
            return s.createQuery("select new org.hibernate.test.querycache.StudentDTO(s.name) from Student s order by s.studentNumber");
        }
    };
    ResultChecker checker = new ResultChecker() {

        public void check(Object results) {
            List resultList = (List) results;
            assertEquals(2, resultList.size());
            StudentDTO yogi = (StudentDTO) resultList.get(0);
            assertNull(yogi.getDescription());
            assertEquals(yogiExpected.getName(), yogi.getName());
            StudentDTO sherman = (StudentDTO) resultList.get(1);
            assertEquals(shermanExpected.getName(), sherman.getName());
            assertNull(sherman.getDescription());
        }
    };
    runTest(hqlExecutor, criteriaExecutor, checker, false);
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) AliasToBeanConstructorResultTransformer(org.hibernate.transform.AliasToBeanConstructorResultTransformer) Session(org.hibernate.Session) Test(org.junit.Test)

Example 2 with AliasToBeanConstructorResultTransformer

use of org.hibernate.transform.AliasToBeanConstructorResultTransformer in project hibernate-orm by hibernate.

the class AbstractQueryCacheResultTransformerTest method testMultiSelectNewList.

@Test
public void testMultiSelectNewList() throws Exception {
    CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {

        protected Criteria getCriteria(Session s) throws Exception {
            return s.createCriteria(Student.class, "s").setProjection(Projections.projectionList().add(Property.forName("s.studentNumber").as("studentNumber")).add(Property.forName("s.name").as("name"))).addOrder(Order.asc("s.studentNumber")).setResultTransformer(new AliasToBeanConstructorResultTransformer(getConstructor()));
        }

        private Constructor getConstructor() throws NoSuchMethodException {
            return Student.class.getConstructor(long.class, PersonName.class);
        }
    };
    HqlExecutor hqlExecutor = new HqlExecutor() {

        public Query getQuery(Session s) {
            return s.createQuery("select new Student(s.studentNumber, s.name) from Student s order by s.studentNumber");
        }
    };
    ResultChecker checker = new ResultChecker() {

        public void check(Object results) {
            List resultList = (List) results;
            assertEquals(2, resultList.size());
            Student yogi = (Student) resultList.get(0);
            assertEquals(yogiExpected.getStudentNumber(), yogi.getStudentNumber());
            assertEquals(yogiExpected.getName(), yogi.getName());
            Student sherman = (Student) resultList.get(1);
            assertEquals(shermanExpected.getStudentNumber(), sherman.getStudentNumber());
            assertEquals(shermanExpected.getName(), sherman.getName());
        }
    };
    runTest(hqlExecutor, criteriaExecutor, checker, false);
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) AliasToBeanConstructorResultTransformer(org.hibernate.transform.AliasToBeanConstructorResultTransformer) Session(org.hibernate.Session) Test(org.junit.Test)

Example 3 with AliasToBeanConstructorResultTransformer

use of org.hibernate.transform.AliasToBeanConstructorResultTransformer in project hibernate-orm by hibernate.

the class AbstractQueryCacheResultTransformerTest method testSelectNewEntityConstructorList.

@Test
public void testSelectNewEntityConstructorList() throws Exception {
    CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {

        protected Criteria getCriteria(Session s) {
            return s.createCriteria(Student.class, "s").setProjection(Projections.projectionList().add(Property.forName("s.studentNumber").as("studentNumber")).add(Property.forName("s.name").as("name"))).addOrder(Order.asc("s.studentNumber")).setResultTransformer(new AliasToBeanConstructorResultTransformer(getConstructor()));
        }

        private Constructor getConstructor() {
            Type studentNametype = sessionFactory().getEntityPersister(Student.class.getName()).getPropertyType("name");
            return ReflectHelper.getConstructor(Student.class, new Type[] { StandardBasicTypes.LONG, studentNametype });
        }
    };
    HqlExecutor hqlExecutor = new HqlExecutor() {

        public Query getQuery(Session s) {
            return s.createQuery("select new Student(s.studentNumber, s.name) from Student s order by s.studentNumber");
        }
    };
    ResultChecker checker = new ResultChecker() {

        public void check(Object results) {
            List resultList = (List) results;
            assertEquals(2, resultList.size());
            Student yogi = (Student) resultList.get(0);
            assertEquals(yogiExpected.getStudentNumber(), yogi.getStudentNumber());
            assertEquals(yogiExpected.getName(), yogi.getName());
            Student sherman = (Student) resultList.get(1);
            assertEquals(shermanExpected.getStudentNumber(), sherman.getStudentNumber());
            assertEquals(shermanExpected.getName(), sherman.getName());
        }
    };
    runTest(hqlExecutor, criteriaExecutor, checker, false);
}
Also used : Type(org.hibernate.type.Type) ArrayList(java.util.ArrayList) List(java.util.List) AliasToBeanConstructorResultTransformer(org.hibernate.transform.AliasToBeanConstructorResultTransformer) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with AliasToBeanConstructorResultTransformer

use of org.hibernate.transform.AliasToBeanConstructorResultTransformer in project hibernate-orm by hibernate.

the class AbstractQueryCacheResultTransformerTest method testMultiSelectNewWithLiteralList.

@Test
public void testMultiSelectNewWithLiteralList() throws Exception {
    CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {

        protected Criteria getCriteria(Session s) throws Exception {
            return s.createCriteria(Student.class, "s").setProjection(Projections.projectionList().add(Projections.sqlProjection("555 as studentNumber", new String[] { "studentNumber" }, new Type[] { StandardBasicTypes.LONG })).add(Property.forName("s.name").as("name"))).addOrder(Order.asc("s.studentNumber")).setResultTransformer(new AliasToBeanConstructorResultTransformer(getConstructor()));
        }

        private Constructor getConstructor() throws NoSuchMethodException {
            return Student.class.getConstructor(long.class, PersonName.class);
        }
    };
    HqlExecutor hqlExecutor = new HqlExecutor() {

        public Query getQuery(Session s) {
            return s.createQuery("select new Student(555L, s.name) from Student s order by s.studentNumber");
        }
    };
    ResultChecker checker = new ResultChecker() {

        public void check(Object results) {
            List resultList = (List) results;
            assertEquals(2, resultList.size());
            Student yogi = (Student) resultList.get(0);
            assertEquals(555L, yogi.getStudentNumber());
            assertEquals(yogiExpected.getName(), yogi.getName());
            Student sherman = (Student) resultList.get(1);
            assertEquals(555L, sherman.getStudentNumber());
            assertEquals(shermanExpected.getName(), sherman.getName());
        }
    };
    runTest(hqlExecutor, criteriaExecutor, checker, false);
}
Also used : Type(org.hibernate.type.Type) ArrayList(java.util.ArrayList) List(java.util.List) AliasToBeanConstructorResultTransformer(org.hibernate.transform.AliasToBeanConstructorResultTransformer) Session(org.hibernate.Session) Test(org.junit.Test)

Example 5 with AliasToBeanConstructorResultTransformer

use of org.hibernate.transform.AliasToBeanConstructorResultTransformer in project hibernate-orm by hibernate.

the class AbstractQueryCacheResultTransformerTest method testOneSelectNewNoAliasesList.

@Test
public void testOneSelectNewNoAliasesList() throws Exception {
    CriteriaExecutor criteriaExecutor = new CriteriaExecutor() {

        protected Criteria getCriteria(Session s) throws Exception {
            return s.createCriteria(Student.class, "s").setProjection(Projections.property("s.name")).addOrder(Order.asc("s.studentNumber")).setResultTransformer(new AliasToBeanConstructorResultTransformer(getConstructor()));
        }

        private Constructor getConstructor() throws NoSuchMethodException {
            return StudentDTO.class.getConstructor(PersonName.class);
        }
    };
    HqlExecutor hqlExecutor = new HqlExecutor() {

        public Query getQuery(Session s) {
            return s.createQuery("select new org.hibernate.test.querycache.StudentDTO(s.name) from Student s order by s.studentNumber");
        }
    };
    ResultChecker checker = new ResultChecker() {

        public void check(Object results) {
            List resultList = (List) results;
            assertEquals(2, resultList.size());
            StudentDTO yogi = (StudentDTO) resultList.get(0);
            assertNull(yogi.getDescription());
            assertEquals(yogiExpected.getName(), yogi.getName());
            StudentDTO sherman = (StudentDTO) resultList.get(1);
            assertEquals(shermanExpected.getName(), sherman.getName());
            assertNull(sherman.getDescription());
        }
    };
    runTest(hqlExecutor, criteriaExecutor, checker, false);
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) AliasToBeanConstructorResultTransformer(org.hibernate.transform.AliasToBeanConstructorResultTransformer) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)5 List (java.util.List)5 Session (org.hibernate.Session)5 AliasToBeanConstructorResultTransformer (org.hibernate.transform.AliasToBeanConstructorResultTransformer)5 Test (org.junit.Test)5 Type (org.hibernate.type.Type)2