Announcement Announcement Module
Collapse
No announcement yet.
Finder on a field set attribute Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Finder on a field set attribute

    Hi there,

    iīm just started using springroo to develop a web application tool and stuck on following problem:

    Within the RooShell i added an attribute with the command field set, which referencing on itself. For that i also added an finder for the attribut and get some error i donīt know how to handle with?

    RooEntity Class
    Code:
    @RooJavaBean
    @RooToString
    @RooPlural("Zertifikatsarten")
    @RooEntity(finders = { "findZertifikatsartenByTitelLike",
        "findZertifikatsartenByZertifikatskategorie",
        "findZertifikatsartenByTitel", "findZertifikatsartenByDepend" })
    public class Zertifikatsart {
       .
       . 
       .
      @ManyToMany(cascade = CascadeType.ALL)
      private Set<com.app.sample.domain.Zertifikatsart> depend = new    HashSet<com.app.sample.domain.Zertifikatsart>();
    
      
    // imported generated method for the finder functionality
    public static TypedQuery<Zertifikatsart> findZertifikatsartenByDepend(
          Set<Zertifikatsart> depend) {
        if (depend == null)
          throw new IllegalArgumentException("The depend argument is required");
        EntityManager em = Zertifikatsart.entityManager();
        StringBuilder queryBuilder = new StringBuilder(
            "SELECT Zertifikatsart FROM Zertifikatsart AS zertifikatsart WHERE");
        for (int i = 0; i < depend.size(); i++) {
          if (i > 0)
            queryBuilder.append(" AND");
          queryBuilder.append(" :depend_item").append(i)
              .append(" MEMBER OF zertifikatsart.depend");
        }
        TypedQuery<Zertifikatsart> q = em.createQuery(queryBuilder.toString(),
            Zertifikatsart.class);
        int dependIndex = 0;
        for (Zertifikatsart _zertifikatsart : depend) {
          q.setParameter("depend_item" + dependIndex++, _zertifikatsart);
        }
    
        return q;
      }
    }
    ERROR Code:
    Code:
    java.lang.String cannot be cast to com.app.sample.domain.Zertifikatsart
    Can anybody point me to the right way to think about this problem?

    thx

  • #2
    Again

    Hello again,

    Ok i will try it again:

    i want to realize a ManyToMany relationsship. entities are student and lecture. i add a finder to find students by lecture. then i get this error:

    Code:
    java.lang.String cannot be cast to com.study.domain.Lecture
    My RooLog looks like that:
    Code:
    // Spring Roo 1.1.2.RELEASE [rev fbc33bb] log opened at 2011-03-28 23:43:06
    project --topLevelPackage com.study --projectName School --java 6
    persistence setup --database HYPERSONIC_IN_MEMORY --provider HIBERNATE 
    entity --class ~.domain.Student --testAutomatically 
    field string --fieldName name
    entity --class ~.domain.Lecture --testAutomatically 
    field string --fieldName title
    field set --fieldName students --type ~.domain.Student --cardinality MANY_TO_MANY 
    field set --fieldName lectures--type ~.domain.Lecture --cardinality MANY_TO_MANY --class ~.domain.Student --mappedBy students
    controller all --package ~.web

    Does anybody know this issue or furthermore has an idea to solve it?

    Comment


    • #3
      You get the exception when Roo tries to do the foreach on the depend collection
      Code:
      for (Zertifikatsart _zertifikatsart : depend) {
            q.setParameter("depend_item" + dependIndex++, _zertifikatsart);
          }
      because depend contains the id's of your type, instead of the objects.
      You can get through with it by doing something like this:

      Code:
      Set<Zertifikatsart> zertifikatsartSet= new HashSet<Zertifikatsart>();
       String string = depend.toString().substring(1, depend.toString().length()-1);
       String[] res = string.split(", ");
       for(int i = 0; i < res.length; i++)
       {
              zertifikatsartSet.add(Zertifikatsart.findZertifikatsart(Long.parseLong(res[i])));
       }
      And where the foreach tries to iterate over depend, you replace it to iterate over your new Set containing the actual objects that you need.
      Code:
         for (Zertifikatsart _zertifikatsart : zertifikatsartSet) {
            q.setParameter("depend_item" + dependIndex++, _zertifikatsart);
          }

      I hope that will help you and other, because I was myself in this situation.
      P.S: maybe it's not 100% the methods you actually have that I wrote, so please review them again too work with your application

      Originally posted by nimda View Post
      Hi there,

      iīm just started using springroo to develop a web application tool and stuck on following problem:

      Within the RooShell i added an attribute with the command field set, which referencing on itself. For that i also added an finder for the attribut and get some error i donīt know how to handle with?

      RooEntity Class
      Code:
      @RooJavaBean
      @RooToString
      @RooPlural("Zertifikatsarten")
      @RooEntity(finders = { "findZertifikatsartenByTitelLike",
          "findZertifikatsartenByZertifikatskategorie",
          "findZertifikatsartenByTitel", "findZertifikatsartenByDepend" })
      public class Zertifikatsart {
         .
         . 
         .
        @ManyToMany(cascade = CascadeType.ALL)
        private Set<com.app.sample.domain.Zertifikatsart> depend = new    HashSet<com.app.sample.domain.Zertifikatsart>();
      
        
      // imported generated method for the finder functionality
      public static TypedQuery<Zertifikatsart> findZertifikatsartenByDepend(
            Set<Zertifikatsart> depend) {
          if (depend == null)
            throw new IllegalArgumentException("The depend argument is required");
          EntityManager em = Zertifikatsart.entityManager();
          StringBuilder queryBuilder = new StringBuilder(
              "SELECT Zertifikatsart FROM Zertifikatsart AS zertifikatsart WHERE");
          for (int i = 0; i < depend.size(); i++) {
            if (i > 0)
              queryBuilder.append(" AND");
            queryBuilder.append(" :depend_item").append(i)
                .append(" MEMBER OF zertifikatsart.depend");
          }
          TypedQuery<Zertifikatsart> q = em.createQuery(queryBuilder.toString(),
              Zertifikatsart.class);
          int dependIndex = 0;
          for (Zertifikatsart _zertifikatsart : depend) {
            q.setParameter("depend_item" + dependIndex++, _zertifikatsart);
          }
      
          return q;
        }
      }
      ERROR Code:
      Code:
      java.lang.String cannot be cast to com.app.sample.domain.Zertifikatsart
      Can anybody point me to the right way to think about this problem?

      thx

      Comment

      Working...
      X