简体   繁体   中英

How do i create hibernate mappings for this situation?

I have the following SQL query:

    SELECT CONNECTIONDATE AS UNLOAD, 
    SUBSTR(ROUTECODE,1,(LENGTH(ROUTECODE)-2)) AS ROUTE, 
    COUNT(SUBSTR(ROUTECODE,1,(LENGTH(ROUTECODE)-2))) AS FREQUENCY 
    FROM RouteTableSynonym RC1 
    WHERE RC1.ROUTECONNECTIONTYPE = 'INBOUND' 
    and RC1.CONNECTIONTIME >= TO_TIMESTAMP('<sdate>', 'DD-MM-YYYY HH24:MI:SS') 
    and RC1.CONNECTIONTIME <= TO_TIMESTAMP('<edate>', 'DD-MM-YYYY HH24:MI:SS') 
    and RC1.LOGISTICSPOINTID in (SELECT DISTINCT RCO1.LOGISTICSPOINTID  
    FROM RouteOrderTableSynonym RCO1 
    WHERE RCO1.NAMC = '<namc>') 
    GROUP BY CONNECTIONDATE, SUBSTR(ROUTECODE,1,(LENGTH(ROUTECODE)-2)) 
    ORDER BY CONNECTIONDATE, ROUTE;

All values designated by '< var_name >' are replace with the values that being queried. I also have this entity class that will store the result of this query:

   import java.util.Date;

   public class DD_BlackoutRouteFrequencies {

        private Date rte_day;
        private String route;
        private int freq;
        private int delayedFreq;

        public Date getRte_day() {
            return rte_day;
        }
        public void setRte_day(Date rte_day) {
            this.rte_day = rte_day;
        }
        public String getRoute() {
            return route;
        }
        public void setRoute(String route) {
            this.route = route;
        }
        public int getFreq() {
            return freq;
        }
        public void setFreq(int freq) {
            this.freq = freq;
        }
        public int getDelayedFreq() {
            return delayedFreq;
        }
        public void setDelayedFreq(int delayedFreq) {
            this.delayedFreq = delayedFreq;
        }   
    }

I execute the query like this :

    try{
        SQLQuery sqlquery = session.createSQLQuery(query);
        sqlquery.addEntity(DD_BlackoutRouteFrequencies.class);
        results = sqlquery.list();
        logger.debug(results.size());
        System.out.println("Frequnecy Results size: "+results.size());
    }catch(Exception e){
        logger.error("Exception ", e);
        throw new RuntimeException("SQL Exception getting Blackout Route Frequencies: "+ e.getMessage());
    }

The problem I am having is that I cannot figure out how to do the hibernate mapping for this entity to receive the results of this query which draws its results from two different tables.

Do I have to do the hibernate mapping for both table used in the query and then map the entity?

You can also get an instance of a map. eg:

SQLQuery sqlQuery = session.createSQLQuery(sql);

// positional parameters (using ?)
sqlQuery.setString(0, sdate);
sqlQuery.setString(1, edate);
sqlQuery.setString(2, namc);

// scalar values for each column
sqlQuery.addScalar("UNLOAD", Hibernate.STRING);
sqlQuery.addScalar("ROUTE", Hibernate.STRING);
sqlQuery.addScalar("FREQUENCY ", Hibernate.INTEGER);

sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
List<Map<String, Object>> list = sqlQuery.list();

for (Map<String, Object> map : list) {
    System.out.println(map.get("UNLOAD"));
    System.out.println(map.get("ROUTE"));
    System.out.println(map.get("FREQUENCY"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM