-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate SQLQuery truncating UDT char field in SQL Server
PostPosted: Wed Jan 31, 2007 3:32 pm 
Newbie

Joined: Mon Sep 18, 2006 6:34 am
Posts: 18
Hi,

I'm creating a SQLQuery and am passing it a very simple T-SQL statement to run but it is truncating a user defined data type (with 12 chars) so that only the first character is being retrieved in the Object[] returned from the SQLQuery list() call.

For example, if 12345 should be coming back from the database, only the first 1 is coming back.

Seeing that there are no mapping files for this type of query, is there a workaround? I could rely on good old JDBC to do the job but doing it through Hibernate would be nicer.

Cheers,
J.

Hibernate version: 3.2

Name and version of the database you are using: MS SQL Server 2000 SP4


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 31, 2007 7:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
waaay to little info to help out....show some code ;) (my guess is that the auto detection of the return type is somehow wrong and result in CharType to be used instead of a StringType)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 4:44 am 
Newbie

Joined: Mon Sep 18, 2006 6:34 am
Posts: 18
Code that runs T-SQL:
Code:
public ReportVO[] getReportWhereClientNameLike(
            final String cliNm, final Date startDate, final Date endDate) {


        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        String queryString =
            "SELECT client_t.gcli_nm AS gcliNm "
            + ", pol_t.pol_num AS polNum "
            + ", cc_file_t.ccfile_rcvd_dtm AS ccfileRcvdDtm "
            + ", cc_file_event_t.ccfile_evnt_dsc AS ccfileEvntDsc "
            + ", cc_file_event_status_type_t.ccfile_evnt_stat_dsc AS ccfileEvntStatDsc "
            + ", cc_file_t.ccfile_mbr_cnt AS ccfileMbrCnt "
            + ", cc_file_t.ccfile_10pct_chg_ind AS ccfile10pctChgInd "
            + ", cc_profile_t.ccpfl_is_active_ind AS ccpflIsActiveInd "
            + "FROM client_t "
            + "INNER JOIN pol_t ON "
            + "client_t.gcli_id = pol_t.gcli_id "
            + "INNER JOIN cc_profile_t ON "
            + "pol_t.gcli_id = cc_profile_t.gcli_id "
            + "AND pol_t.pol_id = cc_profile_t.pol_id "
            + "INNER JOIN cc_file_t ON "
            + "cc_profile_t.gcli_id = cc_file_t.gcli_id "
            + "AND cc_profile_t.pol_id = cc_file_t.pol_id "
            + "INNER JOIN cc_file_event_history_t ON "
            + "cc_file_t.gcli_id = cc_file_event_history_t.gcli_id "
            + "AND cc_file_t.pol_id = cc_file_event_history_t.pol_id "
            + "AND cc_file_t.ccfile_seq_num = cc_file_event_history_t.ccfile_seq_num "
            + "INNER JOIN cc_file_event_t ON "
            + "cc_file_event_history_t.ccfile_evnt_cd = cc_file_event_t.ccfile_evnt_cd "
            + "INNER JOIN cc_file_event_status_type_t ON "
            + "cc_file_event_history_t.ccfile_evnt_stat_cd = cc_file_event_status_type_t.ccfile_evnt_stat_cd "
            + "WHERE "
            + "((cc_file_event_history_t.ccfile_evnt_cd = 'CCLOAD' AND (cc_file_event_history_t.ccfile_evnt_stat_cd = 'SUCCESS' OR cc_file_event_history_t.ccfile_evnt_stat_cd = 'WTHERROR')) "
            + "OR (cc_file_event_history_t.ccfile_evnt_cd = 'OASISLD' AND (cc_file_event_history_t.ccfile_evnt_stat_cd = 'SUCCESS' OR cc_file_event_history_t.ccfile_evnt_stat_cd = 'WTHERROR')) "
            + "OR (cc_file_event_history_t.ccfile_evnt_stat_cd = 'FAILURE') "
            + "OR (cc_file_event_history_t.ccfile_evnt_stat_cd = 'SYSPROC')) "
            + "AND cc_file_event_history_t.ccfile_evnt_strt_dtm = ( "
            + "SELECT MAX(ccfile_evnt_strt_dtm) "
            + "FROM cc_file_event_history_t "
            + "WHERE ((cc_file_event_history_t.ccfile_evnt_cd = 'CCLOAD' AND (ccfile_evnt_stat_cd = 'SUCCESS' OR ccfile_evnt_stat_cd = 'WTHERROR')) "
            + "OR (cc_file_event_history_t.ccfile_evnt_cd = 'OASISLD' AND (ccfile_evnt_stat_cd = 'SUCCESS' OR ccfile_evnt_stat_cd = 'WTHERROR')) "
            + "OR (ccfile_evnt_stat_cd = 'FAILURE') "
            + "OR (ccfile_evnt_stat_cd = 'SYSPROC')) "
            + "AND gcli_id = cc_file_t.gcli_id "
            + "AND pol_id = cc_file_t.pol_id "
            + "AND ccfile_seq_num = cc_file_t.ccfile_seq_num) "
            + "AND cc_file_t.ccfile_rcvd_dtm BETWEEN '" + formatter.format(startDate) + "' AND + '" + formatter.format(endDate) + "' "
            + "AND client_t.gcli_nm LIKE '" + cliNm + "'";

        SQLQuery sqlQuery =
            HibernateExtranetSession.getSessionFactory().getCurrentSession().
            createSQLQuery(queryString);

        return convertToVOArray(sqlQuery.list());
    }


So sqlQuery.list() is a list of Object[] as there is no mapping to a VO involved. In order to map the data to a VO, I have to traverse each object array in the list and map the contents of the array to a field in the VO.

Code that maps object[] list to VO array:
Code:
public final ReportVO[] convertToVOArray(
            final List hibernateList) {
        try {
        ReportVO[] ret = new ReportVO[hibernateList.size()];
        for (int i = 0; i < hibernateList.size(); i++) {
            Object[] objs = (Object[]) hibernateList.get(i);

//            String[] arr = ((String) hibernateList.get(i)).split(",");
            ReportVO vo = new ReportVO();
            vo.setCliNm((String) objs[0]);
            vo.setPolNum((String) objs[1].toString());
            vo.setCcfileRcvdDtm((Date) objs[2]);
            vo.setCcfileEvntDsc((String) objs[3]);
            vo.setCcfileEvntStatDsc((String) objs[4]);
            vo.setCcfileMbrCnt(((Short) objs[5]).intValue());
            vo.setCcfile10pctChgInd((String) objs[6].toString());
            vo.setCcpflIsActiveInd((String) objs[7].toString());
            ret[i] = vo;
        }
        //hibernateList.toArray(ret);
        return ret;
        } catch (ArrayStoreException ase) {
            return null;
        }
    }


Here is what I am mapping the contents of ecah object[] to:

VO code:
Code:
public class ReportVO implements Serializable {
    /** serialVersionUID. */
    private static final long serialVersionUID = 1;

    /** cli_nm. */
    private String cliNm;
    /** pol_num. */
    private String polNum;
    /** ccfile_rcvd_dtm. */
    private Date ccfileRcvdDtm;
    /** ccfile_evnt_dsc. */
    private String ccfileEvntDsc;
    /** ccfile_evnt_stat_dsc. */
    private String ccfileEvntStatDsc;
    /** ccfile_mbr_cnt. */
    private int ccfileMbrCnt;
    /** ccfile_10pct_chg_ind. */
    private String ccfile10pctChgInd;
    /** ccpflIsActiveInd. */
    private String ccpflIsActiveInd;

    /**
     * cliNm getter.
     *
     * @return cliNm
     */
    public String getCliNm() {
        return cliNm;
    }
    /**
     * cliNm setter.
     *
     * @param cliNmIn cliNmIn
     */
    public void setCliNm(final String cliNmIn) {
        this.cliNm = cliNmIn;
    }

    /**
     * polNum getter.
     *
     * @return polNum
     */
    public String getPolNum() {
        return polNum;
    }
    /**
     * polNum setter.
     *
     * @param polNumIn polNumIn
     */
    public void setPolNum(final String polNumIn) {
        this.polNum = polNumIn;
    }

    /**
     * ccfileRcvdDtm getter.
     *
     * @return ccfileRcvdDtm
     */
    public Date getCcfileRcvdDtm() {
        return ccfileRcvdDtm;
    }

    /**
     * ccfileRcvdDtm setter.
     *
     * @param ccfileRcvdDtmIn ccfileRcvdDtmIn
     */
    public void setCcfileRcvdDtm(final Date ccfileRcvdDtmIn) {
        this.ccfileRcvdDtm = ccfileRcvdDtmIn;
    }

    /**
     * ccfileEvntDsc getter.
     *
     * @return ccfileEvntDsc
     */
    public String getCcfileEvntDsc() {
        return ccfileEvntDsc;
    }
    /**
     * ccfileEvntDsc setter.
     *
     * @param ccfileEvntDscIn ccfileEvntDscIn
     */
    public void setCcfileEvntDsc(final String ccfileEvntDscIn) {
        this.ccfileEvntDsc = ccfileEvntDscIn;
    }

    /**
     * ccfileEvntStatDsc getter.
     *
     * @return ccfileEvntStatDsc
     */
    public String getCcfileEvntStatDsc() {
        return ccfileEvntStatDsc;
    }
    /**
     * ccfileEvntStatDsc setter.
     *
     * @param ccfileEvntStatDscIn ccfileEvntStatDscIn
     */
    public void setCcfileEvntStatDsc(final String ccfileEvntStatDscIn) {
        this.ccfileEvntStatDsc = ccfileEvntStatDscIn;
    }

    /**
     * ccfileMbrCnt getter.
     *
     * @return ccfileMbrCnt
     */
    public int getCcfileMbrCnt() {
        return ccfileMbrCnt;
    }
    /**
     * ccfileMbrCnt setter.
     *
     * @param ccfileMbrCntIn ccfileMbrCntIn
     */
    public void setCcfileMbrCnt(final int ccfileMbrCntIn) {
        this.ccfileMbrCnt = ccfileMbrCntIn;
    }

    /**
     * ccfile10pctChgInd getter.
     *
     * @return ccfile10pctChgInd
     */
    public String getCcfile10pctChgInd() {
        return ccfile10pctChgInd;
    }
    /**
     * ccfile10pctChgInd setter.
     *
     * @param ccfile10pctChgIndIn ccfile10pctChgIndIn
     */
    public void setCcfile10pctChgInd(final String ccfile10pctChgIndIn) {
        this.ccfile10pctChgInd = ccfile10pctChgIndIn;
    }

    /**
     * ccpflIsActiveInd getter.
     *
     * @return ccpflIsActiveInd
     */
    public String getCcpflIsActiveInd() {
        return ccpflIsActiveInd;
    }
    /**
     * ccpflIsActiveInd setter.
     *
     * @param ccpflIsActiveIndIn ccpflIsActiveIndIn
     */
    public void setCcpflIsActiveInd(final String ccpflIsActiveIndIn) {
        this.ccpflIsActiveInd = ccpflIsActiveIndIn;
    }
}


objs[1] is the problematic field, it should be returning a 5 char String but Hibernate is only bringing back the first char of the string and it must be down to the fact that the db field is a char type instead of varchar.

Therefore I need to instruct Hibernate in some way to bring back the full string instead of the first char only.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 4:50 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
take a look at

http://blog.hibernate.org/cgi-bin/blosx ... ql_and_sql

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 6:53 am 
Newbie

Joined: Mon Sep 18, 2006 6:34 am
Posts: 18
Yep, that did the job alright. Had to still set the datatype to Hibernate.STRING in the addScalar calls but that was all.

Resultant code:
Code:
public ReportVO[] getReportWhereClientNameLike(
            final String cliNm, final Date startDate, final Date endDate) {


        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        String queryString =
            "SELECT client_t.gcli_nm AS gcliNm "
            + ", pol_t.pol_num AS polNum "
            + ", cc_file_t.ccfile_rcvd_dtm AS ccfileRcvdDtm "
            + ", cc_file_event_t.ccfile_evnt_dsc AS ccfileEvntDsc "
            + ", cc_file_event_status_type_t.ccfile_evnt_stat_dsc AS ccfileEvntStatDsc "
            + ", cc_file_t.ccfile_mbr_cnt AS ccfileMbrCnt "
            + ", cc_file_t.ccfile_10pct_chg_ind AS ccfile10pctChgInd "
            + ", cc_profile_t.ccpfl_is_active_ind AS ccpflIsActiveInd "
            + "FROM client_t "
            + "INNER JOIN pol_t ON "
            + "client_t.gcli_id = pol_t.gcli_id "
            + "INNER JOIN cc_profile_t ON "
            + "pol_t.gcli_id = cc_profile_t.gcli_id "
            + "AND pol_t.pol_id = cc_profile_t.pol_id "
            + "INNER JOIN cc_file_t ON "
            + "cc_profile_t.gcli_id = cc_file_t.gcli_id "
            + "AND cc_profile_t.pol_id = cc_file_t.pol_id "
            + "INNER JOIN cc_file_event_history_t ON "
            + "cc_file_t.gcli_id = cc_file_event_history_t.gcli_id "
            + "AND cc_file_t.pol_id = cc_file_event_history_t.pol_id "
            + "AND cc_file_t.ccfile_seq_num = cc_file_event_history_t.ccfile_seq_num "
            + "INNER JOIN cc_file_event_t ON "
            + "cc_file_event_history_t.ccfile_evnt_cd = cc_file_event_t.ccfile_evnt_cd "
            + "INNER JOIN cc_file_event_status_type_t ON "
            + "cc_file_event_history_t.ccfile_evnt_stat_cd = cc_file_event_status_type_t.ccfile_evnt_stat_cd "
            + "WHERE "
            + "((cc_file_event_history_t.ccfile_evnt_cd = 'CCLOAD' AND (cc_file_event_history_t.ccfile_evnt_stat_cd = 'SUCCESS' OR cc_file_event_history_t.ccfile_evnt_stat_cd = 'WTHERROR')) "
            + "OR (cc_file_event_history_t.ccfile_evnt_cd = 'OASISLD' AND (cc_file_event_history_t.ccfile_evnt_stat_cd = 'SUCCESS' OR cc_file_event_history_t.ccfile_evnt_stat_cd = 'WTHERROR')) "
            + "OR (cc_file_event_history_t.ccfile_evnt_stat_cd = 'FAILURE') "
            + "OR (cc_file_event_history_t.ccfile_evnt_stat_cd = 'SYSPROC')) "
            + "AND cc_file_event_history_t.ccfile_evnt_strt_dtm = ( "
            + "SELECT MAX(ccfile_evnt_strt_dtm) "
            + "FROM cc_file_event_history_t "
            + "WHERE ((cc_file_event_history_t.ccfile_evnt_cd = 'CCLOAD' AND (ccfile_evnt_stat_cd = 'SUCCESS' OR ccfile_evnt_stat_cd = 'WTHERROR')) "
            + "OR (cc_file_event_history_t.ccfile_evnt_cd = 'OASISLD' AND (ccfile_evnt_stat_cd = 'SUCCESS' OR ccfile_evnt_stat_cd = 'WTHERROR')) "
            + "OR (ccfile_evnt_stat_cd = 'FAILURE') "
            + "OR (ccfile_evnt_stat_cd = 'SYSPROC')) "
            + "AND gcli_id = cc_file_t.gcli_id "
            + "AND pol_id = cc_file_t.pol_id "
            + "AND ccfile_seq_num = cc_file_t.ccfile_seq_num) "
            + "AND cc_file_t.ccfile_rcvd_dtm BETWEEN '" + formatter.format(startDate) + "' AND + '" + formatter.format(endDate) + "' "
            + "AND client_t.gcli_nm LIKE '" + cliNm + "'";

        List results =
            HibernateExtranetSession.getSessionFactory().getCurrentSession().
            createSQLQuery(queryString)
            .addScalar("gcliNm")
            .addScalar("polNum", Hibernate.STRING)
            .addScalar("ccfileRcvdDtm")
            .addScalar("ccfileEvntDsc")
            .addScalar("ccfileMbrCnt")
            .addScalar("ccfile10pctChgInd", Hibernate.STRING)
            .addScalar("ccpflIsActiveInd", Hibernate.STRING)
            .setResultTransformer(Transformers.aliasToBean(
                    ReportVO.class))
            .list();

        return convertToVOArray(results);
    }


convertToVOArray():
Code:
public final ReportVO[] convertToVOArray(
            final List hibernateList) {
        ReportVO[] ret = new ReportVO[hibernateList.size()];
        hibernateList.toArray(ret);
        return ret;
    }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.