3 Replies Latest reply on Aug 5, 2003 3:17 PM by kutter

    EJBQL: comparing object-parameter and relation-field gives s

    kutter

      Help Needed
      (I am working with an out-of-the box configuration,
      based on the examples in the buy-documentation
      for CMP2.0, JBoss 3.2.1.)

      I am trying to make an EJBQL-finder method,
      which takes an object as argument, and then
      compares this to an object obtained by using
      a CMR-relation.

      I think someone may give me a hint, without going
      trough all the code. If not, I will build it into the
      as-is gangster-example and post the full code.

      Problem Description:
      I have the following bean, two versions of
      a finder method findAD03, findAD04, and a
      CMR-relation field userHoldingAd. For findAD03 it
      works, for findAD04 I get an error:

      import interfaces.UserEntityLocal;

      ...
      * @ejb.finder
      * signature = "java.util.Collection findAD03(interfaces.UserEntityLocal uid1, interfaces.UserEntityLocal uid2)"
      * query = "SELECT OBJECT(g) FROM affinityData g, userEntity u WHERE u = ?1 "
      *
      * @ejb.finder
      * signature = "java.util.Collection findAD04(interfaces.UserEntityLocal uid1, interfaces.UserEntityLocal uid2)"
      * query = "SELECT OBJECT(g) FROM affinityData g WHERE g.userHoldingAd = ?1 "
      *
      */
      public abstract class AffinityDataBean implements EntityBean{
      ...
      /**
      * @return
      * @ejb.relation
      * name = "Users-Holding-ADs"
      * role-name = "ad-hold-by-userEntity"
      * cascade-delete = "yes"
      * @jboss.relation
      * related-pk-field = "userEntityId"
      * fk-column = "userHoldingAd"
      *
      * @ejb.persistence
      * column-name = "userHoldingAd"
      * @ejb.interface-method
      */
      public abstract UserEntityLocal getUserHoldingAd();
      public abstract void setUserHoldingAd(UserEntityLocal org);
      ...
      }


      I wrote a test case, where I give the two user entities, and get the collections:

      UserEntityLocal yojimbo =
      userEntityHome.findByPrimaryKey(YOJIMBO);
      UserEntityLocal takeshi =
      userEntityHome.findByPrimaryKey(TAKESHI);
      Collection all =
      affinityDataHome.findAD03(yojimbo, takeshi);

      With findAD03 it works, thus I provide two good arguments, and, althoug the code does not make something useful,
      it seems I can compare u with the parameter ?1

      Now, if I replace find AD03 with findAD04, I compare g.userHoldingAd with ?1
      I have tested the CMR field userHoldinAd in other routines. What I get for findAD04 (but not for findAD03) is the following error:

      testUsersAD(test.CrimePortalTest): Find failed: java.sql.SQLException: Wrong data type: For string: "[B@987e1e" in statement [SELECT t0_g.affinityDataId FROM AFFINITYDATA t0_g, USERENTITY t1_g_userHoldingAd WHERE ((t1_g_userHoldingAd.id=0)) AND (t0_g.userHoldingAd=t1_g_userHoldingAd.id)]


      If someone could help me would be great!


        • 1. More: EJBQL: comparing object-parameter and relation-field
          kutter

          ADDENDUM:

          I am trying a third version:

          * @ejb.finder
          * signature = "java.util.Collection findAD05(java.lang.Integer uid1, java.lang.Integer uid2)"
          * query = "SELECT OBJECT(g) FROM affinityData g WHERE g.userHoldingAd.userEntityId = ?1 "
          *

          which of course I am callling differently:

          Collection all = affinityDataHome.findAD06(YOJIMBO, TAKESHI);


          what I really do not understand is that I get the same kind of error, or more exactly: I get exactly the same
          error.

          I am going to write a test routine for the standard gangster example, where somthing like this is used, but not tested.

          If someone has a hint in the meantime, please let me know,

          Philipp

          • 2. Re: EJBQL: comparing object-parameter and relation-field giv
            kutter

            On the "Advanced Documentation" list, a posted a
            completion to the official JBoss docu which covers this
            problem. I still have to figure out what is the problem
            here, but for the selectAccomplices example, I have
            added all the missing parts to the JBoss docu, and
            it runs.

            If someone can give me more insight, would still be
            appreciated.

            Best, Philipp

            • 3. SOLUTION
              kutter

              I got help on another list, here is the solution:
              (with slightly different code)

              Correct is:

              > java.util.Collection findUserByPhoneId(PhoneLocal phone)
              > SELECT OBJECT(u) FROM User AS u, IN(u.phones) AS p WHERE p = ?1
              >
              >
              another possibility is:

              java.util.Collection findUserByPhoneId(PhoneLocal phone)
              SELECT OBJECT(u) FROM Phone AS p, User AS u WHERE p.user = u AND p = ?1


              My problem was that I wanted instead of the second version the WRONG thing:

              java.util.Collection findUserByPhoneId(PhoneLocal phone)
              SELECT OBJECT(u) FROM User AS u WHERE ?1.user = u


              Which of course is excluded by the syntax.

              The trick is thus, that for every argument A a,
              you need to have an "A as a0" in the SELECT part, and an "a0 = ?1" in the WHERE part.

              Then, you can compare the relation-field with that a0.