facebook

Hibernate: Problem with composite key search

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #265109 Reply

    sandipk
    Member

    Hi,

    I have a table PEN_DCRG_AMOUNT_DTL.

    The hbm file is like:
    <hibernate-mapping>
    <class name=”com.ctmis.pension.hibernate.PenDcrgAmountDtl” table=”PEN_DCRG_AMOUNT_DTL” schema=”EMPLOYEE”>
    <composite-id name=”id” class=”com.ctmis.pension.hibernate.PenDcrgAmountDtlId”>
    <key-property name=”ppoCode” type=”java.lang.String”>
    <column name=”PPO_CODE” length=”20″ />
    </key-property>
    <key-property name=”slNo” type=”java.lang.Long”>
    <column name=”SL_NO” precision=”2″ scale=”0″ />
    </key-property>
    <key-property name=”treasuryCode” type=”java.lang.String”>
    <column name=”TREASURY_CODE” length=”3″ />
    </key-property>
    </composite-id>
    <property name=”dcrgAmount” type=”java.lang.Double”>
    <column name=”DCRG_AMOUNT” precision=”8″ />
    </property>
    <property name=”gpoNo” type=”java.lang.String”>
    <column name=”GPO_NO” length=”20″ />
    </property>
    <property name=”dated” type=”java.util.Date”>
    <column name=”DATED” length=”7″ />
    </property>
    <property name=”status” type=”java.lang.String”>
    <column name=”STATUS” length=”1″ />
    </property>
    <property name=”createUid” type=”java.lang.String”>
    <column name=”CREATE_UID” length=”20″ />
    </property>
    <property name=”createDate” type=”java.util.Date”>
    <column name=”CREATE_DATE” length=”7″ />
    </property>
    <property name=”modifiedUid” type=”java.lang.String”>
    <column name=”MODIFIED_UID” length=”20″ />
    </property>
    <property name=”modifiedDate” type=”java.util.Date”>
    <column name=”MODIFIED_DATE” length=”7″ />
    </property>
    <property name=”tokenNo” type=”java.lang.String”>
    <column name=”TOKEN_NO” length=”10″ />
    </property>
    <property name=”tokenDate” type=”java.util.Date”>
    <column name=”TOKEN_DATE” length=”7″ />
    </property>
    <property name=”tokenAmount” type=”java.lang.Double”>
    <column name=”TOKEN_AMOUNT” precision=”8″ />
    </property>
    </class>
    </hibernate-mapping>

    I want to search by ppoCode and treasuryCode.
    That is the where clause should look like: “where PPO_CODE = ? and TREASURY_CODE = ?”.
    But what actually comes is: “where (1=1)”.

    I used the following code:

    PenDcrgAmountDtlDAO penDcrgAmountDtlDAO = new PenDcrgAmountDtlDAO();
    PenDcrgAmountDtl penDcrgAmountDtl = new PenDcrgAmountDtl();
    PenDcrgAmountDtlId penDcrgAmountDtlId = new PenDcrgAmountDtlId();
    penDcrgAmountDtlId.setPpoCode(ppoCode);
    penDcrgAmountDtlId.setTreasuryCode(treasuryCode);
    penDcrgAmountDtl.setId(penDcrgAmountDtlId);

    Collection col1 = penDcrgAmountDtlDAO.findByExample(penDcrgAmountDtl);

    Here is findByExample code:

    public List findByExample(PenDcrgAmountDtl instance) {
    log.debug(“finding PenDcrgAmountDtl instance by example”);
    try {

    List results = getSession()
    .createCriteria(“com.ctmis.pension.hibernate.PenDcrgAmountDtl”)
    .add(Example.create(instance))
    .list();
    log.debug(“find by example successful, result size: ” + results.size());
    return results;
    } catch (RuntimeException re) {
    log.error(“find by example failed”, re);
    throw re;
    }
    }

    After debugging I found that “key-properties” are not checked for the where
    clause append.
    The problem can be solved by createQuery() call with model.id.ppoCode = ? etc.
    But the thing is that we have to manually build the query.
    Suppose next time we want to search by giving treasuryCode and slNo…

    The dynamism of using Example.create() is needed.

    Can anybody give a good solution?

    I am using Hibernate 3.1.

    #265166 Reply

    Haris Peco
    Member

    sandipk,

    Version properties, identifiers and associations are ignored in Example query.
    However, you can use Criteria Restrictions like this :

    List results = getSession()
    .createCriteria(“com.ctmis.pension.hibernate.PenDcrgAmountDtl”)
    .add( Restrictions.eq(“ppoCode”, ppoCode) )
    .add( Restrictions.eq(“treasuryCode”, treasuryCode) )
    .list();

    Regards,

Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: Hibernate: Problem with composite key search

You must be logged in to post in the forum log in