-->
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.  [ 2 posts ] 
Author Message
 Post subject: Composite key and loading example needed
PostPosted: Mon Jul 11, 2005 11:47 am 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
HI,

I have check around in the forum and in the hibernate doc, and I didn't found a simple example of retrieving a record with a composite key.

I have this mapping:

Code:
<class name="ExchangeRate"
         table="ExchangeRate"
           mutable="false">
           
    <composite-id name="id" class="ExchangeRateId">
      <key-property name="Currency" type="TrimmedString">
        <column name="Currency" scale="3" precision="0" not-null="true" sql-type="char" />
      </key-property>
      <key-property name="RateDate" type="Numeric8DigitToDateFormat">
        <column name="RateDate" scale="8" precision="0" not-null="true" sql-type="numeric" />
      </key-property>
    </composite-id>
   
    <property name="Rate" type="java.math.BigDecimal">
      <column name="Rate" scale="13" precision="7" not-null="true" sql-type="decimal" />
    </property>
(-- more properties here... --)
</class>



I would like to know what is the "one liner code" (simpliest code) for my DAO method "getExchangeRateByIDs(currency, date)".

I need to use it for retrieving a record by the composite primary key. Can I use the session.load()? but I dont have the "Primary key" of the composite-key class (ExchangeRateId).

Should I use a Criteria? Could I have an exemple please? I would prefer not to use HQL here...

Thanks all

Etienne
Montreal


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 1:43 pm 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
Hi,

I think I have manage to make it work with the simpliest code:

In my DAO:

Code:
   public ExchangeRate findExchangeRateByIds(String exchangeRateCurrencyCode, Date exchangeRateDate) {
      Session session = SessionFactoryUtils.getSession(getSessionFactory(), true);
      Criteria criteria = session.createCriteria(ExchangeRate.class);
      criteria.add(Expression.eq("id.currency", exchangeRateCurrencyCode));
      criteria.add(Expression.eq("id.rateDate", exchangeRateDate));
      return (ExchangeRate)criteria.uniqueResult();
   }


Can it be more simplier?

Thanks.

Etienne.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.