Results 1 to 3 of 3

Thread: Find by other property than PK ?

  1. #1
    Join Date
    Aug 2005
    Posts
    14

    Default Find by other property than PK ?

    Hello

    I want to find objects releted to other object by many-to-one relationship, but not by PK, but some other prooperty, like this:

    hibernate mapping for Cities object:

    <many-to-one name="land"
    class="Lands"
    column="LAND_CODE"
    property-ref="code"
    not-null="true"/>

    here's the code:

    int count = ((Integer) getHibernateTemplate()
    .iterate("select count(*) from Cities c where c.land=?", land.getCode())
    .next()).intValue();

    and if i store city with property land, it nicely stores land.code in LAND_CODE column, but in finders i have to do "....where c.land=?', land.getCode()...", becouse "....where c.land=?', land..." doesn't work as it should. any suggestions ?
    thanks in advance
    tom

  2. #2
    Join Date
    Apr 2005
    Location
    Portland, OR
    Posts
    8

    Default

    Try something like:
    Code:
    public int getWipCountFor&#40;final Land land&#41; &#123;
      Integer count = &#40;Integer&#41; getHibernateTemplate&#40;&#41;.execute&#40;new HibernateCallback&#40;&#41; &#123;
        public Object doInHibernate&#40;Session session&#41; throws HibernateException, SQLException &#123;
          Query query = session.createQuery&#40;"select count&#40;*&#41; from Cities c where c.land = &#58;land"&#41;;
          query.setParameter&#40;"land", land&#41;;
          return query.uniqueResult&#40;&#41;;
        &#125;;
      &#125;&#41;;
      return count.intValue&#40;&#41;;
    &#125;

  3. #3
    Join Date
    Aug 2005
    Posts
    16

    Default

    becouse "....where c.land=?', land..." doesn't work as it should.
    What exactly is the problem you're running into? I do the exact same thing in my code and it works fine for me.

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Unit testing with JOTM and JtaTransactionManager
    By lalle in forum Architecture
    Replies: 1
    Last Post: Oct 15th, 2005, 09:05 AM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. Replies: 4
    Last Post: Aug 17th, 2005, 04:42 AM
  5. Replies: 2
    Last Post: May 13th, 2005, 05:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •