-->
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.  [ 1 post ] 
Author Message
 Post subject: Question querying with Example in Many to One
PostPosted: Thu Oct 27, 2011 6:11 pm 
Newbie

Joined: Thu Oct 27, 2011 4:53 pm
Posts: 1
Greetings!

I'm wondering if anyone has any experience (or helpful tips) when attempting to use the Example implementation of Criterion to query a many-to-one by the "one" side.

Logically, I have a Record (table/class) and a Transaction (table/class) where Transaction has a FK to Record's PK. In java, my Transaction class has a field that is of type Record. I can insert/update/select just fine if I'm doing it by primary class, but if I'm trying to do it by association I run into problems. I'm attempting to build both a Transaction object and a Record object, then setting the Record's name field. I then set Transaction's record to be my new object, and make that my Example. When I try to retrieve the list, the show-sql shows only the values associated with the Transaction object.

I think I'm missing a property or attribute in the hibernate-mapping files that would allow me to do this, but I'm not sure. If anyone could take a look at my code/mappings and give some insight, that'd be great. Alternatively, if someone could tell me this just isn't possible, that'd be fine too as I could work on developing a different way to get the data I need.

Record.hbm.xml
Code:
<hibernate-mapping>
    <class name="com.xxx.yyy.domain.RecordDTO"
        table="RECORDS">

        <id name="recordID"
            column="RECORD_ID"
            type="long">
            <generator class="identity" />   
        </id>
       <property
          name="statusCd"
          column="STATUS_CD"
            type="string"/>
           
       <property
          name="policyNo"
          column="PLCY_NO"
            type="string"/>
           
       <property
          name="voidInd"
          column="VOID_IND"
          type="string"/>
           
    </class>
</hibernate-mapping>


Transaction.hbm.xml
Code:
<hibernate-mapping>
    <class name="com.xxx.yyy.domain.TransactionDTO"
        table="TRANSACTIONS" lazy="false">

        <id name="recordID"
            column="TRANSACTION_ID"
            type="long">
            <generator class="identity" />   
        </id>
       <property
          name="pftId"
          column="PFT_ID"
            type="long"/>
           
       <many-to-one
          name="recordParent"
          column="RECORD_ID"
            class="com.xxx.yyy.domain.RecordDTO"
            cascade="all"
            lazy="false"
            fetch="join"/>
                     
       <property
          name="sentDt"
          column="SENT_DT"
            type="java.util.Date"/>
           
       <property
          name="approvalInd"
          column="APPROVAL_IND"
          type="string"/>
           
       <property
          name="sentInd"
          column="SENT_IND"
          type="string"/>
           
       <property
          name="voidInd"
          column="VOID_IND"
          type="string"/>
           
    </class>
</hibernate-mapping>


Snippet of TransactionDTO.java
Code:
public class TransactionDTO implements Serializable {

   private long recordID;

   private recordDTO recordParent;
...   

        public long getRecordID() {
      return recordID;
   }

   public void setRecordID(long recordID) {
      this.recordID = recordID;
   }

        public RecordDTO getRecordParent() {
                 return recordParent;
        }

        public void setRecordParent(RecordDTO parent) {
                 this.recordParent = parent;
        }
}


TransactionDaoHibernateImpl.java
Code:
public class TransactionDaoHibernateImpl extends HibernateDaoSupport{
   public Collection fetchByExample() {

      Collection myCol = new ArrayList();

                TransactionDTO t = new TransactionDTO();
                t.setVoided(false);
                RecordDTO r = new RecordDTO();
                r.setVoided(false);
                r.setPolicyNo("zz");
                t.setParentRecord(r);

      Example ex = Example.create(t).excludeZeroes();
            
      Session session = getHibernateTemplate().getSessionFactory().openSession();
      
      Transaction tx = session.beginTransaction();
      
      Criteria crit = session
            .createCriteria(TransactionDTO.class)
            .add(ex);
      
      
      myCol = crit.list();
      tx.rollback();
      return myCol;
   }
}



The resulting collection has Transactions that are not voided (with their associated Records), but does not limit based on the Record's policyNo being "zz"


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

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.