Announcement Announcement Module
Collapse
No announcement yet.
Find by other property than PK ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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;

    Comment


    • #3
      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.

      Comment

      Working...
      X