13 Replies Latest reply on Jun 9, 2008 5:05 PM by Auke Noppe

    Illegalargumentexception on calling namedquery

    Auke Noppe Newbie

      Hi,

      i have a entitybean user, which has a namedquery User.getAllUsers.
      when i call this query from my sessionbean:

      Query q = getEm().createNativeQuery("SELECT object(u) FROM User u WHERE "
       + col + " LIKE :value", User.class).setParameter("value", value);


      or the namedquery
      @NamedQuery(name = "User.getAllUsers", query = "SELECT u FROM User u"):
      


      then i get this stack from the client progam:
      xception in thread "main" javax.ejb.EJBException: java.lang.IllegalArgumentException: wrong number of arguments
       at org.jboss.ejb3.cache.simple.SimpleStatefulCache.create(SimpleStatefulCache.java:337)
       at org.jboss.ejb3.stateful.StatefulContainer.dynamicInvoke(StatefulContainer.java:320)
       at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
       at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:82)
       at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:769)
       at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:573)
       at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:373)
       at
      org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:166)


      the serverstack is as follows:
      13:09:37,421 ERROR [STDERR] java.lang.IllegalArgumentException: wrong number of arguments
      13:09:37,421 ERROR [STDERR] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      13:09:37,421 ERROR [STDERR] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
      13:09:37,421 ERROR [STDERR] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
      13:09:37,421 ERROR [STDERR] at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
      13:09:37,421 ERROR [STDERR] at org.jboss.ejb3.EJBContainer.construct(EJBContainer.java:514)
      13:09:37,421 ERROR [STDERR] at org.jboss.ejb3.AbstractPool.create(AbstractPool.java:66)
      13:09:37,421 ERROR [STDERR] at org.jboss.ejb3.InfinitePool.get(InfinitePool.java:49)
      13:09:37,421 ERROR [STDERR] at org.jboss.ejb3.ThreadlocalPool.create(ThreadlocalPool.java:50)
      13:09:37,421 ERROR [STDERR] at org.jboss.ejb3.ThreadlocalPool.get(ThreadlocalPool.java:90)
      13:09:37,421 ERROR [STDERR] at org.jboss.ejb3.cache.simple.SimpleStatefulCache.create(SimpleStatefulCache.java:315)
      13:09:37,421 ERROR [STDERR] at org.jboss.ejb3.stateful.StatefulContainer.dynamicInvoke(StatefulContainer.java:320)
      13:09:37,421 ERROR [STDERR] at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
      13:09:37,421 ERROR [STDERR] at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:82)
      13:09:37,421 ERROR [STDERR] at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:769)
      13:09:37,421 ERROR [STDERR] at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:573)
      13:09:37,421 ERROR [STDERR] at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:373)
      13:09:37,421 ERROR [STDERR] at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:166)


      does anyone has one idea about what i'm doing wrong?

      Regards

      Auke Noppe

        • 1. Re: Illegalargumentexception on calling namedquery
          jaikiran pai Master

          Which version of JBoss and Java do you use?

          • 2. Re: Illegalargumentexception on calling namedquery
            Auke Noppe Newbie

             

            "jaikiran" wrote:
            Which version of JBoss and Java do you use?

            jboss-4.2.2.GA & java 1.6

            • 3. Re: Illegalargumentexception on calling namedquery
              jaikiran pai Master

               

              "aukenoppe" wrote:


              Query q = getEm().createNativeQuery("SELECT object(u) FROM User u WHERE "
               + col + " LIKE :value", User.class).setParameter("value", value);




              Change this to:

              Query q = getEm().createQuery("SELECT object(u) FROM User u WHERE "
               + col + " LIKE :value").setParameter("value", value);


              • 4. Re: Illegalargumentexception on calling namedquery
                Auke Noppe Newbie

                still the same error.

                but how about the namedqueries defined in the User entitybean?
                you have to call them using the createNamedQuery method from the entitymanager.

                • 5. Re: Illegalargumentexception on calling namedquery
                  Auke Noppe Newbie

                  anyone?

                  "aukenoppe" wrote:
                  still the same error.

                  but how about the namedqueries defined in the User entitybean?
                  you have to call them using the createNamedQuery method from the entitymanager.


                  • 6. Re: Illegalargumentexception on calling namedquery
                    jaikiran pai Master

                     

                    "aukenoppe" wrote:


                    but how about the namedqueries defined in the User entitybean?
                    you have to call them using the createNamedQuery method from the entitymanager.


                    Sorry, i did not understand this question. Here's how i have a NamedQuery declared for my entity User and then using it in a bean to fetch all the users. I have this working on JBoss-4.2.2GA:

                    User.java:
                    @Entity
                    @Table (name="User")
                    @NamedQuery(name = "User.getAllUsers", query = "SELECT u FROM User u")
                    public class User implements Serializable {
                    
                     /**
                     * Id
                     */
                     @Id
                     @GeneratedValue
                     @Column (name="id")
                     private long id;
                    
                     /**
                     * User name
                     */
                     @Column (name="name")
                     private String name;
                    ...........
                    //other stuff
                    }
                    


                    UserManagerBean.java:
                    @Stateless
                    @Remote( { UserManager.class })
                    @RemoteBinding(jndiBinding = "RemoteUserManagerBean")
                    public class UserManagerBean implements UserManager {
                    
                    
                     @PersistenceContext
                     private EntityManager entityManager;
                    
                     public User getUsers() {
                    
                     System.out.println("Using named query");
                     List namedQueryResult = entityManager.createNamedQuery("User.getAllUsers").getResultList();
                     System.out.println("Number of users in system = " + namedQueryResult.size());
                     }
                     .........
                     //other stuff
                    }
                    


                    And here's the output:
                    14:13:05,605 INFO [STDOUT] Using named query
                    14:13:05,683 INFO [STDOUT] Number of users in system = 1






                    • 7. Re: Illegalargumentexception on calling namedquery
                      Auke Noppe Newbie

                      this doesn't work either. i'm getting quite frustrated now, because 1 namequery works, but the others don't.

                      the one that works is:

                      @NamedQuery(name = "User.doLogin", query = "SELECT object(u) FROM User u WHERE u.naam = :uname AND u.password = :password")
                      


                      and on the other entities the namedqueries work also.
                      are you able to get the problem from the stacktrace i posted in the first post?

                      many thanks!

                      Auke

                      • 8. Re: Illegalargumentexception on calling namedquery
                        jaikiran pai Master

                         

                        "auke@noppe.nl" wrote:
                        this doesn't work either. i'm getting quite frustrated now, because 1 namequery works, but the others don't.

                        the one that works is:
                        @NamedQuery(name = "User.doLogin", query = "SELECT object(u) FROM User u WHERE u.naam = :uname AND u.password = :password")
                        


                        and on the other entities the namedqueries work also.
                        are you able to get the problem from the stacktrace i posted in the first post?

                        many thanks!

                        Auke


                        Are you saying that most of the queries work except for the one you posted earlier, which is :

                        @NamedQuery(name = "User.getAllUsers", query = "SELECT u FROM User u"):


                        This problem in this query is that there's a : at the end. But i dont see how it could have even passed the java compilation phase.

                        Can you post the exact code which contains this NamedQuery (which does not work) and the complete exception stacktrace? Just want to make sure, we are discussing about one single query.




                        • 9. Re: Illegalargumentexception on calling namedquery
                          Auke Noppe Newbie

                          that : was a typefault.
                          but here is the code of User.java:

                          package com.spotmyfriend.backend.entity;
                          
                          import java.io.Serializable;
                          import java.util.HashSet;
                          import java.util.Set;
                          
                          import javax.persistence.Entity;
                          import javax.persistence.FetchType;
                          import javax.persistence.GeneratedValue;
                          import javax.persistence.GenerationType;
                          import javax.persistence.Id;
                          import javax.persistence.JoinColumn;
                          import javax.persistence.JoinTable;
                          import javax.persistence.ManyToMany;
                          import javax.persistence.NamedQueries;
                          import javax.persistence.NamedQuery;
                          import javax.persistence.OneToOne;
                          import javax.persistence.Table;
                          
                          @Entity(name = "User")
                          @Table(name = "User")
                          @NamedQueries( {
                           @NamedQuery(name = "User.byUserId", query = "SELECT object(u) FROM User u WHERE u.userid = :uid"),
                           @NamedQuery(name = "User.ByName", query = "SELECT object(u) FROM User u WHERE u.naam = :uname"),
                           @NamedQuery(name = "User.doLogin", query = "SELECT object(u) FROM User u WHERE u.naam = :uname AND u.password = :password"),
                           @NamedQuery(name = "User.getAllUsers", query = "SELECT u FROM User u")
                          })
                          public class User implements Serializable {
                          
                           private static final long serialVersionUID = 7221049085316069742L;
                          
                           @Id
                           @GeneratedValue(strategy = GenerationType.AUTO)
                           private int userid;
                           private String naam;
                           private String email;
                           private String password;
                           private int telefoonnummer;
                           private String provider;
                           private String adres;
                           private String postcode;
                           private String plaats;
                           private String verbindingtype;
                           private String locatiebepalingtype;
                           private boolean googleMaps;
                           private boolean active;
                           private String role;
                          
                           @OneToOne
                           private Friend friend;
                          
                           @ManyToMany(targetEntity = Module.class,fetch=FetchType.EAGER)
                           @JoinTable(name = "modules_users",
                           joinColumns = @JoinColumn(name = "userid"),
                           inverseJoinColumns = @JoinColumn(name = "modid"))
                           private Set<Module> modules;
                          
                           @OneToOne(targetEntity=FriendList.class)
                           private FriendList friendlist;
                          
                           public User() {
                          
                           }
                          
                           public User(String naam, String email, String password, int telefoonnr,
                           String provider, String adres, String postcode, String plaats,
                           String verbindingtype, String locatiebepalingtype,
                           boolean googleMaps, boolean active, String role) {
                           this.naam = naam;
                           this.email = email;
                           this.password = password;
                           this.telefoonnummer = telefoonnr;
                           this.provider = provider;
                           this.adres = adres;
                           this.postcode = postcode;
                           this.plaats = plaats;
                           this.verbindingtype = verbindingtype;
                           this.locatiebepalingtype = locatiebepalingtype;
                           this.googleMaps = googleMaps;
                           this.active = true;
                           this.role = role;
                           }
                          
                           public int getUserid() {
                           return userid;
                           }
                          
                           public void setUserid(int userid) {
                           this.userid = userid;
                           }
                          
                           public String getNaam() {
                           return naam;
                           }
                          
                           public void setNaam(String naam) {
                           this.naam = naam;
                           }
                          
                           public String getEmail() {
                           return email;
                           }
                          
                           public void setEmail(String email) {
                           this.email = email;
                           }
                          
                           public String getPassword() {
                           return password;
                           }
                          
                           public void setPassword(String password) {
                           this.password = password;
                           }
                          
                           public int getTelefoonnummer() {
                           return telefoonnummer;
                           }
                          
                           public void setTelefoonnummer(int telefoonnummer) {
                           this.telefoonnummer = telefoonnummer;
                           }
                          
                           public String getProvider() {
                           return provider;
                           }
                          
                           public void setProvider(String provider) {
                           this.provider = provider;
                           }
                          
                           public String getAdres() {
                           return adres;
                           }
                          
                           public void setAdres(String adres) {
                           this.adres = adres;
                           }
                          
                           public String getPostcode() {
                           return postcode;
                           }
                          
                           public void setPostcode(String postcode) {
                           this.postcode = postcode;
                           }
                          
                           public String getPlaats() {
                           return plaats;
                           }
                          
                           public void setPlaats(String plaats) {
                           this.plaats = plaats;
                           }
                          
                           public String getVerbindingtype() {
                           return verbindingtype;
                           }
                          
                           public void setVerbindingtype(String verbindingtype) {
                           this.verbindingtype = verbindingtype;
                           }
                          
                           public String getLocatiebepalingtype() {
                           return locatiebepalingtype;
                           }
                          
                           public void setLocatiebepalingtype(String locatiebepalingtype) {
                           this.locatiebepalingtype = locatiebepalingtype;
                           }
                          
                           public boolean isGoogleMaps() {
                           return googleMaps;
                           }
                          
                           public void setGoogleMaps(boolean googleMaps) {
                           this.googleMaps = googleMaps;
                           }
                          
                           public boolean isActive() {
                           return active;
                           }
                          
                           public void setActive(boolean active) {
                           this.active = active;
                           }
                          
                           public String getRole() {
                           return role;
                           }
                          
                           public void setRole(String role) {
                           this.role = role;
                           }
                          
                           public Set<Module> getModules() {
                           if (modules == null) {
                           modules = new HashSet<Module>();
                           }
                           return modules;
                           }
                          
                           public void setModules(Module activeModules) {
                           this.modules.add(activeModules);
                           }
                          
                           public Friend getFriend() {
                           return friend;
                           }
                          
                           public void setFriend(Friend friendType) {
                           this.friend = friendType;
                           }
                          
                           public FriendList getFriendlist() {
                           return friendlist;
                           }
                          
                           public void setFriendlist(FriendList friendlist) {
                           this.friendlist = friendlist;
                           }
                          
                           @Override
                           public String toString() {
                           String s = "----------------- user -------------\n";
                           s += "userid: " + getUserid() + "\n";
                           s += "Naam: " + getNaam() + "\n";
                           s += "Adres: " + getAdres() + "\n";
                           s += "Email: " + getEmail() + "\n";
                           s += "Password:" + getPassword() + "\n";
                           s += "Postcode: " + getPostcode() + "\n";
                           s += "Plaats: " + getPlaats() + "\n";
                           s += "Telefoonnr:" + getTelefoonnummer() + "\n";
                           s += "Provider: " + getProvider() + "\n";
                           s += "Verbindingstype: " + getVerbindingtype() + "\n";
                           s += "Locatiebepalingstype: " + getLocatiebepalingtype() + "\n";
                           s += "GoogleMaps: " + isGoogleMaps() + "\n";
                           s += "------------------------------------\n";
                          
                           s+= "heeft de volgende modules geactiveerd: ";
                           Set<Module> mods = getModules();
                           if(mods.size()>0) {
                           for(Module m: mods) {
                           s+= ""+m.getNaam();
                           }
                           }
                           s+= "\n";
                          
                           FriendList fl = getFriendlist();
                           Set<Friend> friends = fl.getFriends();
                           if(friends.size()>1) {
                           s+= "heeft vrienden: ";
                           for(Friend f : friends) {
                           s+= "Naam: "+f.getUser().getNaam();
                           s+= "Geblokkeerd: "+f.isBlocked();
                           }
                           } else {
                           s+= "geen vrienden geregistreerd";
                           }
                           s+= "\n";
                           System.out.println(s);
                           return s;
                           }
                          
                          
                          
                          
                          
                          }
                          


                          and this is the UserManagerBean.java:

                          package com.spotmyfriend.backend.sessionbeans.usermanagement;
                          
                          import java.io.Serializable;
                          import java.util.Collection;
                          import java.util.List;
                          
                          import javax.ejb.Local;
                          import javax.ejb.Remote;
                          import javax.ejb.Stateful;
                          import javax.persistence.EntityManager;
                          import javax.persistence.PersistenceContext;
                          import javax.persistence.Query;
                          
                          import com.spotmyfriend.backend.entity.Module;
                          import com.spotmyfriend.backend.entity.User;
                          
                          @Remote(UserManager.class)
                          @Local(UserManager.class)
                          @Stateful(name = "UserManager")
                          public class UserManagerBean implements Serializable, UserManager {
                          
                           private static final long serialVersionUID = 2667633265960007691L;
                          
                           private User user;
                          
                           @PersistenceContext(unitName = "spotmyfriend")
                           private EntityManager em;
                          
                           public UserManagerBean(User user) {
                           this.user = user;
                           }
                          
                           private EntityManager getEm() {
                           return em;
                           }
                          
                           @SuppressWarnings("unchecked")
                           public List<User> getAllUsers() {
                           List<User> result = getEm().createNamedQuery("User.getAllUsers")
                           .getResultList();
                           return result;
                           }
                          
                           public void addModule(Module module) {
                           user.setModules(module);
                           module.setUser(user);
                           getEm().merge(user);
                           getEm().merge(module);
                           }
                          
                           public void removeModule(Module module) {
                           Collection<Module> modules = user.getModules();
                           modules.remove(module);
                           getEm().merge(user);
                           }
                          
                           public boolean isAdmin() {
                           if (user.getRole().equals("administrator")) {
                           return true;
                           }
                           return false;
                           }
                          
                           public List<User> search(String col, String value) {
                           // q.setParameter gebruiken tegen sql-injection!!!
                           Query q = getEm().createQuery("SELECT object(u) FROM User u WHERE "
                           + col + " LIKE :value").setParameter("value", value);
                           return q.getResultList();
                           }
                          
                           public User getUserById(int uid) {
                           Query q = em.createNamedQuery("User.byUserId").setParameter("uid", uid);
                           User u = (User) q.getSingleResult();
                           return u;
                           }
                          }
                          


                          the clientcode i execute is:
                          String factory = "org.jnp.interfaces.NamingContextFactory";
                           String url = "127.0.0.1:1099";
                          
                           Properties properties = new Properties();
                           properties.put(Context.INITIAL_CONTEXT_FACTORY, factory);
                           properties.put(Context.PROVIDER_URL, url);
                          InitialContext ctx = new InitialContext(properties);
                          UserManager um = (UserManager) ctx.lookup("UserManager/remote");
                           User u = um.getUserById(1);
                           u.toString();
                          
                          


                          thanks in advance!

                          • 10. Re: Illegalargumentexception on calling namedquery
                            Auke Noppe Newbie

                            i have done some debugging, and i found out that the namedQuery Users.getAllUsers works when called from another sessionbean. so i quess that there is an fault in the UserManagerBean. do you have an idea what exactly?

                            • 11. Re: Illegalargumentexception on calling namedquery
                              jaikiran pai Master

                               

                              "auke@noppe.nl" wrote:

                              public UserManagerBean(User user) {
                               this.user = user;
                               }


                              i have done some debugging, and i found out that the namedQuery Users.getAllUsers works when called from another sessionbean. so i quess that there is an fault in the UserManagerBean. do you have an idea what exactly?


                              You don't have a default constructor in your UserManagerBean. Add a default constructor to that bean and rebuild your application. Then test the changes.



                              • 12. Re: Illegalargumentexception on calling namedquery
                                Sandor Fiedler Newbie

                                Three things to your attention:

                                1. Rename the table from User to anything else (i.e TUser) while on different databases this might be a reserved word (i.e. Oracle).

                                2. Remove the constructor or replace your constructor with a default constructor with no args.

                                3. For maintaining the state of a logged in user use a business method like

                                public User doLogin(String user, String pass)
                                . With this code you can find the user to log in by using your correct query.

                                • 13. Re: Illegalargumentexception on calling namedquery
                                  Auke Noppe Newbie

                                  Thank you all for your replies. it was the default constructor that did the trick!

                                  Tanks :)