Example usage for org.hibernate.transform Transformers aliasToBean

List of usage examples for org.hibernate.transform Transformers aliasToBean

Introduction

In this page you can find the example usage for org.hibernate.transform Transformers aliasToBean.

Prototype

public static ResultTransformer aliasToBean(Class target) 

Source Link

Document

Creates a resulttransformer that will inject aliased values into instances of Class via property methods or fields.

Usage

From source file:com.ett.drv.biz.DrvQueryHelperImp.java

public List<ComboxObject> getDict(String xtlb, String type, String glbm) {
    List<ComboxObject> listTmp = new ArrayList<ComboxObject>();
    SessionFactory sessionFactory = this.getBaseDaoDrv().getHibernateSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();

    String queryStr = MessageFormat.format(DictSqlWithGlbm, xtlb, type, glbm);
    logger.debug("DictSqlWithGlbm?" + queryStr.toString());
    SQLQuery query = session.createSQLQuery(queryStr.toString());
    query.addScalar("value", new org.hibernate.type.StringType());
    query.addScalar("text", new org.hibernate.type.StringType());

    query.setResultTransformer(Transformers.aliasToBean(ComboxObject.class));
    try {// www .j  a v a  2s.  co m
        tx.begin();
        //query.uniqueResult()
        listTmp = query.list();

        if (logger.isInfoEnabled()) {
            logger.info("==" + listTmp.size());
        }
        if (listTmp.size() == 0) {
            return null;
            //throw new ObjectDontExistException("?");
        }
        session.flush();
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        logger.info(e);
        if (tx != null) {
            tx.rollback();
        }
        throw new ObjectDontExistException(e);
    } finally {
        session.close();
    }
    return listTmp;
}

From source file:com.ett.self.print.biz.PrintBizImpl.java

protected void searchFlowFromSql(String sql, String idcard) {

    SessionFactory sessionFactory = this.getBaseDaoDrv().getHibernateSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();

    String queryStr = MessageFormat.format(sql, idcard);
    log.debug("?" + queryStr.toString());
    SQLQuery query = session.createSQLQuery(queryStr.toString());
    query.addScalar("ywlx", new org.hibernate.type.StringType());
    query.addScalar("fee", new org.hibernate.type.StringType());
    query.addScalar("ywyy", new org.hibernate.type.StringType());
    query.addScalar("yyrq", new org.hibernate.type.TimestampType());
    query.addScalar("ksrq", new org.hibernate.type.TimestampType());
    query.addScalar("slrq", new org.hibernate.type.TimestampType());
    query.addScalar("lsh", new org.hibernate.type.StringType());
    query.addScalar("xm", new org.hibernate.type.StringType());
    query.addScalar("hphm", new org.hibernate.type.StringType());

    query.addScalar("jdsbh", new org.hibernate.type.StringType());
    query.addScalar("sfzmhm", new org.hibernate.type.StringType());
    query.addScalar("zjcx", new org.hibernate.type.StringType());
    query.addScalar("dabh", new org.hibernate.type.StringType());

    query.setResultTransformer(Transformers.aliasToBean(DrvPersonFlowObject.class));
    try {//  w  ww .jav a 2  s.c o m
        tx.begin();
        //query.uniqueResult()
        this.lists = query.list();

        if (log.isInfoEnabled()) {
            log.info("?==" + lists.size());
        }
        if (this.lists == null || this.lists.size() == 0) {
            this.lists = new ArrayList();

            //this.lists.add(co);
        }
        session.flush();
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        log.info(e);
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        session.close();
    }
    //return null;
}

From source file:com.exilant.GLEngine.CoaCache.java

License:Open Source License

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void loadAccountData() {

    /*/*from  www  .  j  av  a  2s .co  m*/
     * 1.Loads all the account codes and details of that as GLAccount objects in theGLAccountCode,theGLAccountId HashMap's
     */

    // Temporary place holders
    final HashMap glAccountCodes = new HashMap();
    final HashMap glAccountIds = new HashMap();
    final HashMap accountDetailType = new HashMap();

    String sql = "select id as \"id\",name as \"name\",tableName as \"tableName\","
            + "description as \"description\",columnName as \"columnName\",attributeName as \"attributeName\""
            + ",nbrOfLevels as  \"nbrOfLevels\" from AccountDetailType";

    final Session currentSession = persistenceService.getSession();
    SQLQuery createSQLQuery = currentSession.createSQLQuery(sql);
    createSQLQuery.addScalar("id", IntegerType.INSTANCE).addScalar("name").addScalar("tableName")
            .addScalar("description").addScalar("columnName").addScalar("attributeName")
            .setResultTransformer(Transformers.aliasToBean(AccountDetailType.class));
    List<AccountDetailType> accountDetailTypeList = new ArrayList<AccountDetailType>();
    List<GLAccount> glAccountCodesList = new ArrayList<GLAccount>();
    new ArrayList<GLAccount>();

    accountDetailTypeList = createSQLQuery.list();
    for (final AccountDetailType type : accountDetailTypeList)
        accountDetailType.put(type.getAttributeName(), type);
    sql = "select ID as \"ID\", glCode as \"glCode\" ,name as \"name\" ,"
            + "isActiveForPosting as \"isActiveForPosting\" ,classification as \"classification\", functionReqd as \"functionRequired\" from chartofaccounts ";
    createSQLQuery = currentSession.createSQLQuery(sql);
    createSQLQuery.addScalar("ID", IntegerType.INSTANCE).addScalar("glCode").addScalar("name")
            .addScalar("isActiveForPosting", BooleanType.INSTANCE)
            .addScalar("classification", LongType.INSTANCE).addScalar("functionRequired", BooleanType.INSTANCE)
            .setResultTransformer(Transformers.aliasToBean(GLAccount.class));

    glAccountCodesList = createSQLQuery.list();
    for (final GLAccount type : glAccountCodesList)
        glAccountCodes.put(type.getCode(), type);
    for (final GLAccount type : glAccountCodesList)
        glAccountIds.put(type.getId(), type);
    loadParameters(glAccountCodes, glAccountIds);
    try {
        final HashMap<String, HashMap> hm = new HashMap<String, HashMap>();
        hm.put(ACCOUNTDETAILTYPENODE, accountDetailType);
        hm.put(GLACCCODENODE, glAccountCodes);
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Loading size:" + glAccountCodes.size());
        hm.put(GLACCIDNODE, glAccountIds);
        applicationCacheManager.put(ROOTNODE, hm);
    } catch (final Exception e) {
        throw e;
    }

}

From source file:com.gisgraphy.hibernate.criterion.DistanceRestrictionTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test// w  ww .j  a v a  2s  .c  o m
public void testDistanceRestrictionPointDouble() {
    final City p1 = GisgraphyTestHelper.createCity("paris", 48.86667F, 2.3333F, 1L);
    City p2 = GisgraphyTestHelper.createCity("bordeaux", 44.83333F, -0.56667F, 3L);

    this.cityDao.save(p1);
    this.cityDao.save(p2);

    HibernateCallback hibernateCallback = new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {

            Criteria testCriteria = session.createCriteria(City.class);
            List<String> fieldList = new ArrayList<String>();
            fieldList.add("name");
            Projection projection = Projections.property("name").as("name");
            testCriteria.setProjection(projection).add(Restrictions.ne("id", p1.getId()))
                    .add(new DistanceRestriction(p1.getLocation(), 500000D))
                    .setResultTransformer(Transformers.aliasToBean(_CityDTO.class));

            List<_CityDTO> results = testCriteria.list();
            return results;
        }
    };

    List<_CityDTO> cities = (List<_CityDTO>) testDao.testCallback(hibernateCallback);
    assertEquals("According to the distance restriction, it should have zero result", 0, cities.size());

    hibernateCallback = new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {

            Criteria testCriteria = session.createCriteria(City.class);
            Projection projection = Projections.property("name").as("name");
            testCriteria.setProjection(projection).add(new DistanceRestriction(p1.getLocation(), 600000D))
                    .add(Restrictions.ne("id", p1.getId()))
                    .setResultTransformer(Transformers.aliasToBean(_CityDTO.class));

            List<_CityDTO> results = testCriteria.list();
            return results;
        }
    };

    cities = (List<_CityDTO>) testDao.testCallback(hibernateCallback);
    assertEquals("According to the distance restriction, it should have one results", 1, cities.size());
    assertEquals("bordeaux", cities.get(0).getName());

}

From source file:com.gisgraphy.hibernate.criterion.FulltextRestrictionTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test/*  ww w.jav a  2s  . c  o  m*/
public void testFulltextRestriction() {
    OpenStreetMap streetOSM = createOpenStreetMap();
    openStreetMapDao.save(streetOSM);
    assertNotNull(openStreetMapDao.get(streetOSM.getId()));

    int numberOfLineUpdated = openStreetMapDao.updateTS_vectorColumnForStreetNameSearch();

    assertEquals("It should have 1 lines updated : one for partial and one for fulltext", 1,
            numberOfLineUpdated);

    HibernateCallback hibernateCallback = new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {

            Criteria testCriteria = session.createCriteria(OpenStreetMap.class);
            List<String> fieldList = new ArrayList<String>();
            fieldList.add("name");
            fieldList.add("gid");

            Projection projection = ProjectionBean.fieldList(fieldList, true);
            testCriteria.setProjection(projection)
                    .add(new FulltextRestriction(OpenStreetMap.FULLTEXTSEARCH_VECTOR_PROPERTY_NAME,
                            "Champs elysees"))//case sensitive accent
                    .setResultTransformer(Transformers.aliasToBean(_OpenstreetmapDTO.class));

            List<_OpenstreetmapDTO> results = testCriteria.list();
            return results;
        }
    };

    List<_OpenstreetmapDTO> streets = (List<_OpenstreetmapDTO>) testDao.testCallback(hibernateCallback);
    assertEquals(
            "According to the fulltext restriction, it should have a result (it should be case insensitive,accent insensitive and '-' insensitive",
            1, streets.size());

    assertEquals("According to the fulltext restriction, the result is incorrect", streetOSM.getGid(),
            streets.get(0).getGid());

    HibernateCallback hibernateCallbackminusSign = new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {

            Criteria testCriteria = session.createCriteria(OpenStreetMap.class);
            List<String> fieldList = new ArrayList<String>();
            fieldList.add("name");
            fieldList.add("gid");

            Projection projection = ProjectionBean.fieldList(fieldList, true);
            testCriteria.setProjection(projection)
                    .add(new FulltextRestriction(OpenStreetMap.FULLTEXTSEARCH_VECTOR_PROPERTY_NAME,
                            "Champs-elysees"))//'-'
                    .setResultTransformer(Transformers.aliasToBean(_OpenstreetmapDTO.class));

            List<_OpenstreetmapDTO> results = testCriteria.list();
            return results;
        }
    };

    streets = (List<_OpenstreetmapDTO>) testDao.testCallback(hibernateCallbackminusSign);
    assertEquals("According to the fulltext restriction, it should have a result (it should be '-' insensitive",
            1, streets.size());

    assertEquals("According to the fulltext restriction, the result is incorrect", streetOSM.getGid(),
            streets.get(0).getGid());

    HibernateCallback hibernateCallbackZeroResult = new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {

            Criteria testCriteria = session.createCriteria(OpenStreetMap.class);
            List<String> fieldList = new ArrayList<String>();
            fieldList.add("name");
            fieldList.add("gid");

            Projection projection = ProjectionBean.fieldList(fieldList, true);
            testCriteria.setProjection(projection)
                    .add(new FulltextRestriction(OpenStreetMap.FULLTEXTSEARCH_VECTOR_PROPERTY_NAME,
                            "Champ elysees"))//wrong word
                    .setResultTransformer(Transformers.aliasToBean(_OpenstreetmapDTO.class));

            List<_OpenstreetmapDTO> results = testCriteria.list();
            return results;
        }
    };

    streets = (List<_OpenstreetmapDTO>) testDao.testCallback(hibernateCallbackZeroResult);
    assertEquals("According to the fulltext restriction, it should not have result ", 0, streets.size());

}

From source file:com.gisgraphy.hibernate.criterion.IntersectsRestrictionTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test/*  ww w .  j ava 2  s.c  o m*/
public void testPartialWordRestriction() {
    OpenStreetMap streetOSM = createAndSaveStreet();

    HibernateCallback hibernateCallbackSuccess = new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {

            Criteria testCriteria = session.createCriteria(OpenStreetMap.class);
            List<String> fieldList = new ArrayList<String>();
            fieldList.add("name");
            fieldList.add("gid");

            Projection projection = ProjectionBean.fieldList(fieldList, true);
            testCriteria.setProjection(projection)
                    .add(new IntersectsRestriction(OpenStreetMap.SHAPE_COLUMN_NAME,
                            GeolocHelper.createPolygonBox(6.94130445F, 50.91544865F, 10000)))
                    .setResultTransformer(Transformers.aliasToBean(_OpenstreetmapDTO.class));

            List<_OpenstreetmapDTO> results = testCriteria.list();
            return results;
        }
    };

    List<_OpenstreetmapDTO> streets = (List<_OpenstreetmapDTO>) testDao.testCallback(hibernateCallbackSuccess);
    assertEquals("According to the intersects restriction, it should have a result ", 1, streets.size());

    assertEquals("According to the intersects restriction, the result is incorrect", streetOSM.getGid(),
            streets.get(0).getGid());

    HibernateCallback hibernateCallbackFail = new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {

            Criteria testCriteria = session.createCriteria(OpenStreetMap.class);
            List<String> fieldList = new ArrayList<String>();
            fieldList.add("name");
            fieldList.add("gid");

            Projection projection = ProjectionBean.fieldList(fieldList, true);
            testCriteria.setProjection(projection)
                    .add(new IntersectsRestriction(OpenStreetMap.SHAPE_COLUMN_NAME,
                            GeolocHelper.createPolygonBox(7.94130445F, 51.91544865F, 10000)))
                    .setResultTransformer(Transformers.aliasToBean(_OpenstreetmapDTO.class));

            List<_OpenstreetmapDTO> results = testCriteria.list();
            return results;
        }
    };

    streets = (List<_OpenstreetmapDTO>) testDao.testCallback(hibernateCallbackFail);
    assertEquals("According to the intersects restriction, it should have no result ", 0, streets.size());

}

From source file:com.gisgraphy.hibernate.criterion.NativeSQLOrderTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test/*from   w w w.  j  a v  a2s  .  com*/
public void testNativeSQLOrderShouldSortAscByDefault() {
    final City p1 = GisgraphyTestHelper.createCity("paris", 48.86667F, 2.3333F, 1L);
    City p2 = GisgraphyTestHelper.createCity("bordeaux", 44.83333F, -0.56667F, 3L);

    City p3 = GisgraphyTestHelper.createCity("goussainville", 49.01667F, 2.46667F, 2L);
    this.cityDao.save(p1);
    this.cityDao.save(p2);
    this.cityDao.save(p3);
    HibernateCallback hibernateCallback = new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {

            Criteria testCriteria = session.createCriteria(City.class);
            List<String> fieldList = new ArrayList<String>();
            fieldList.add("name");
            Projection projection = Projections.property("featureId").as("featureId");
            testCriteria.setProjection(projection).addOrder(new NativeSQLOrder("featureId"))
                    .setResultTransformer(Transformers.aliasToBean(_CityDTO.class));

            List<_CityDTO> results = testCriteria.list();
            return results;
        }
    };

    List<_CityDTO> cities = (List<_CityDTO>) testDao.testCallback(hibernateCallback);
    assertEquals(3, cities.size());
    assertEquals("1", cities.get(0).getFeatureId().toString());
    assertEquals("2", cities.get(1).getFeatureId().toString());
    assertEquals("3", cities.get(2).getFeatureId().toString());
}

From source file:com.gisgraphy.hibernate.criterion.NativeSQLOrderTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test/* w w w .  j  a  v a2 s .c  o  m*/
public void testNativeSQLOrderShouldSortDesc() {
    final City p1 = GisgraphyTestHelper.createCity("paris", 48.86667F, 2.3333F, 1L);
    City p2 = GisgraphyTestHelper.createCity("bordeaux", 44.83333F, -0.56667F, 3L);

    City p3 = GisgraphyTestHelper.createCity("goussainville", 49.01667F, 2.46667F, 2L);
    this.cityDao.save(p1);
    this.cityDao.save(p2);
    this.cityDao.save(p3);
    HibernateCallback hibernateCallback = new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {

            Criteria testCriteria = session.createCriteria(City.class);
            List<String> fieldList = new ArrayList<String>();
            fieldList.add("name");
            Projection projection = Projections.property("featureId").as("featureId");
            testCriteria.setProjection(projection).addOrder(new NativeSQLOrder("featureId", false))
                    .setResultTransformer(Transformers.aliasToBean(_CityDTO.class));

            List<_CityDTO> results = testCriteria.list();
            return results;
        }
    };

    List<_CityDTO> cities = (List<_CityDTO>) testDao.testCallback(hibernateCallback);
    assertEquals(3, cities.size());
    assertEquals("3", cities.get(0).getFeatureId().toString());
    assertEquals("2", cities.get(1).getFeatureId().toString());
    assertEquals("1", cities.get(2).getFeatureId().toString());
}

From source file:com.gisgraphy.hibernate.criterion.PartialWordRestrictionTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test//from  www  .ja  va  2  s. c  o m
public void testPartialWordRestriction() {
    GisgraphyConfig.PARTIAL_SEARH_EXPERIMENTAL = true;
    OpenStreetMap streetOSM = createOpenStreetMap();
    openStreetMapDao.save(streetOSM);
    assertNotNull(openStreetMapDao.get(streetOSM.getId()));

    int numberOfLineUpdated = openStreetMapDao.updateTS_vectorColumnForStreetNameSearch();
    assertEquals("It should have 2 lines updated : one for partial and one for fulltext", 2,
            numberOfLineUpdated);

    HibernateCallback hibernateCallback = new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {

            Criteria testCriteria = session.createCriteria(OpenStreetMap.class);
            List<String> fieldList = new ArrayList<String>();
            fieldList.add("name");
            fieldList.add("gid");

            Projection projection = ProjectionBean.fieldList(fieldList, true);
            testCriteria.setProjection(projection)
                    .add(new PartialWordSearchRestriction(OpenStreetMap.PARTIALSEARCH_VECTOR_PROPERTY_NAME,
                            "elys"))//case sensitive accent
                    .setResultTransformer(Transformers.aliasToBean(_OpenstreetmapDTO.class));

            List<_OpenstreetmapDTO> results = testCriteria.list();
            return results;
        }
    };

    List<_OpenstreetmapDTO> streets = (List<_OpenstreetmapDTO>) testDao.testCallback(hibernateCallback);
    assertEquals(
            "According to the fulltext restriction, it should have a result (it should be case insensitive,accent insensitive and '-' insensitive",
            1, streets.size());

    assertEquals("According to the fulltext restriction, the result is incorrect", streetOSM.getGid(),
            streets.get(0).getGid());

    HibernateCallback hibernateCallbackminusSign = new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {

            Criteria testCriteria = session.createCriteria(OpenStreetMap.class);
            List<String> fieldList = new ArrayList<String>();
            fieldList.add("name");
            fieldList.add("gid");

            Projection projection = ProjectionBean.fieldList(fieldList, true);
            testCriteria.setProjection(projection)
                    .add(new PartialWordSearchRestriction(OpenStreetMap.PARTIALSEARCH_VECTOR_PROPERTY_NAME,
                            "mps ely"))
                    .setResultTransformer(Transformers.aliasToBean(_OpenstreetmapDTO.class));

            List<_OpenstreetmapDTO> results = testCriteria.list();
            return results;
        }
    };

    streets = (List<_OpenstreetmapDTO>) testDao.testCallback(hibernateCallbackminusSign);
    assertEquals("According to the fulltext restriction, it should have a result (it should be '-' insensitive",
            1, streets.size());

    assertEquals("According to the fulltext restriction, the result is incorrect", streetOSM.getGid(),
            streets.get(0).getGid());

    HibernateCallback hibernateCallbackZeroResult = new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {

            Criteria testCriteria = session.createCriteria(OpenStreetMap.class);
            List<String> fieldList = new ArrayList<String>();
            fieldList.add("name");
            fieldList.add("gid");

            Projection projection = ProjectionBean.fieldList(fieldList, true);
            testCriteria.setProjection(projection)
                    .add(new PartialWordSearchRestriction(OpenStreetMap.PARTIALSEARCH_VECTOR_PROPERTY_NAME,
                            "elysees champs"))//wrong word
                    .setResultTransformer(Transformers.aliasToBean(_OpenstreetmapDTO.class));

            List<_OpenstreetmapDTO> results = testCriteria.list();
            return results;
        }
    };

    streets = (List<_OpenstreetmapDTO>) testDao.testCallback(hibernateCallbackZeroResult);
    assertEquals("According to the fulltext restriction, it should not have result ", 0, streets.size());
    GisgraphyConfig.PARTIAL_SEARH_EXPERIMENTAL = false;

}

From source file:com.gisgraphy.hibernate.criterion.ProjectionOrderTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test/*from  w w w .  ja va  2  s  . c  o  m*/
public void testProjectionOrderShouldSortAscByDefault() {
    final City p1 = GisgraphyTestHelper.createCity("paris", 48.86667F, 2.3333F, 1L);
    City p2 = GisgraphyTestHelper.createCity("bordeaux", 44.83333F, -0.56667F, 3L);

    City p3 = GisgraphyTestHelper.createCity("goussainville", 49.01667F, 2.46667F, 2L);
    this.cityDao.save(p1);
    this.cityDao.save(p2);
    this.cityDao.save(p3);
    HibernateCallback hibernateCallback = new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {

            Criteria testCriteria = session.createCriteria(City.class);
            List<String> fieldList = new ArrayList<String>();
            fieldList.add("name");
            Projection projection = Projections.property("featureId").as("featureId");
            testCriteria.setProjection(projection).addOrder(new ProjectionOrder("featureId"))
                    .setResultTransformer(Transformers.aliasToBean(_CityDTO.class));

            List<_CityDTO> results = testCriteria.list();
            return results;
        }
    };

    List<_CityDTO> cities = (List<_CityDTO>) testDao.testCallback(hibernateCallback);
    assertEquals(3, cities.size());
    assertEquals("1", cities.get(0).getFeatureId().toString());
    assertEquals("2", cities.get(1).getFeatureId().toString());
    assertEquals("3", cities.get(2).getFeatureId().toString());

}