0 Replies Latest reply on Sep 4, 2008 3:31 PM by microcz

    How to pass parameters into NamedNativeQuery

    microcz

      Hi everybody, i have a problem with passing parameters into named native query ... i have bean asociated with query, which looks like this:


      @Entity
      @Table(name="computer")
      @SecondaryTables({
        @SecondaryTable(name="license_assignment", pkJoinColumns=@PrimaryKeyJoinColumn(name="computer_fk_id")),
        @SecondaryTable(name="installed_medium_deploy", pkJoinColumns=@PrimaryKeyJoinColumn(name="computer_fk_id"))
      })
      @NamedNativeQuery(name="implicitSample", query="SELECT c.computer_pk_id, la.assign_date, imd.deploy_date, c.machine, c.customer, imd.comment FROM license_assignment AS la JOIN installed_medium_deploy AS imd ON la.computer_fk_id = imd.computer_fk_id JOIN license AS l ON la.license_fk_id = l.license_pk_id JOIN license_type AS lt ON l.type_fk_id = lt.type_pk_id JOIN computer c ON la.computer_fk_id = c.computer_pk_id WHERE l.license_pk_id NOT IN (SELECT rl.license_fk_id FROM returned_license AS rl) AND lt.name = 'rtx' AND imd.comment LIKE :c1 AND c.customer LIKE :c2 AND c.machine LIKE :c3", resultClass=UsedLicenseRtx.class)
      public class UsedLicenseRtx implements java.io.Serializable {
      ...



      and i use this query this way:


      javax.persistence.Query query = getEntityManager().createNamedQuery("implicitSample");
      query.setParameter("c1", usedLicenseRtx.getComment());
      query.setParameter("c2", usedLicenseRtx.getCustomer());
      query.setParameter("c3", usedLicenseRtx.getMachine());
      



      the problem is, that no parameters are set and hibernate calss exactly this:


      15:11:14,877 INFO  [STDOUT] Hibernate: 
          SELECT
              c.computer_pk_id,
              la.assign_date,
              imd.deploy_date,
              c.machine,
              c.customer,
              imd.comment 
          FROM
              license_assignment AS la 
          JOIN
              installed_medium_deploy AS imd 
                  ON la.computer_fk_id = imd.computer_fk_id 
          JOIN
              license AS l 
                  ON la.license_fk_id = l.license_pk_id 
          JOIN
              license_type AS lt 
                  ON l.type_fk_id = lt.type_pk_id 
          JOIN
              computer c 
                  ON la.computer_fk_id = c.computer_pk_id 
          WHERE
              l.license_pk_id NOT IN (
                  SELECT
                      rl.license_fk_id 
                  FROM
                      returned_license AS rl
              ) 
              AND lt.name = 'rtx' 
              AND imd.comment LIKE ? 
              AND c.customer LIKE ? 
              AND c.machine LIKE ? limit ?



      I also tryed ?1 ?2 ?3 instead of those :c1 :c2 :c3 and than use SetParameter(1, usedLicenseRtx.getComment()) etc.. but with same effect
      I have also found on some forum, that for native query only ? should be used instead of ?1 ?2 etc...so i tryed but again with same effect :(
      Please don't you know why is happening this to me?