1 Reply Latest reply on Mar 16, 2008 10:31 PM by Fernando Montaño

    entity query question

    Mike Kabir Newbie

      I have two tables cats and kittens.


      A cat can have any kittens.


      How can I use entity query to restrict by both cat's name and kitten's name.


      I need to get a Cat object as resultList.


      Can I iterate over a set as part of an entity query restriction ?


      Tables


      Cat


      catId
      catName


      Kitten


      kittenId
      catId
      KittenName


        • 1. Re: entity query question
          Fernando Montaño Expert

          @Entity
          public class Cat {
              @Id
              @Column(name = "catid")
              private Long id;
              @Column(name = "catname")
               private String name;
              @OneToMany
              @JoinColumn (name="catid") 
              private List<Kitten> kittens = new ArrayList<Kitten>();
              //getters/setters
          }



          @Entity
          public class Kitten {
              @Id
              @Column(name = "kittenid")
              private Long id;
              @Column(name="kittenname")
               private String name;
              //getters/setters
          }




          Query query = entityManager.createQuery("select c from Cat c INNER JOIN c.kittens k where c.name=:catName and k.name=:kittenName");
          query.setParameter("catName", "Tom");
          query.setParameter("kittenName", "Tommy");
          List cats = query.getResultList();




          HTH.