Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JPA Illegal Argument Exception - NamedQuery of name: xyz not found

I am trying to get a user based on their ID using this named query, however I keep getting an illegal argument exception. I've been looking at this code for awhile. Hopefully someone may catch something I might've missed. This is my ORM

@Entity
@Table(name = "MYUSER")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Myuser.findAll", query = "SELECT m FROM Myuser m"),
@NamedQuery(name = "Myuser.findByUserid", query = "SELECT m FROM Myuser m WHERE m.userid = :userid"),
@NamedQuery(name = "Myuser.findByName", query = "SELECT m FROM Myuser m WHERE m.name = :name"),
@NamedQuery(name = "Myuser.findByPassword", query = "SELECT m FROM Myuser m WHERE m.password = :password"),
@NamedQuery(name = "Myuser.findByEmail", query = "SELECT m FROM Myuser m WHERE m.email = :email"),
@NamedQuery(name = "Myuser.findByTel", query = "SELECT m FROM Myuser m WHERE m.tel = :tel"),
@NamedQuery(name = "Myuser.findByAddress", query = "SELECT m FROM Myuser m WHERE m.address = :address"),
@NamedQuery(name = "Myuser.findBySecqn", query = "SELECT m FROM Myuser m WHERE m.secqn = :secqn"),
@NamedQuery(name = "Myuser.findBySecans", query = "SELECT m FROM Myuser m WHERE m.secans = :secans")})
public class Myuser implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "USERID")
private Integer userid;
@Column(name = "NAME")
private String name;
@Column(name = "PASSWORD")
private String password;
@Column(name = "EMAIL")
private String email;
@Column(name = "TEL")
private Integer tel;
@Column(name = "ADDRESS")
private String address;
@Column(name = "SECQN")
private String secqn;
@Column(name = "SECANS")
private String secans;

public Myuser() {
}

public Myuser(Integer userid) {
    this.userid = userid;
}

public Integer getUserid() {
    return userid;
}

public void setUserid(Integer userid) {
    this.userid = userid;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public Integer getTel() {
    return tel;
}

public void setTel(Integer tel) {
    this.tel = tel;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getSecqn() {
    return secqn;
}

public void setSecqn(String secqn) {
    this.secqn = secqn;
}

public String getSecans() {
    return secans;
}

public void setSecans(String secans) {
    this.secans = secans;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (userid != null ? userid.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Myuser)) {
        return false;
    }
    Myuser other = (Myuser) object;
    if ((this.userid == null && other.userid != null) || (this.userid != null && !this.userid.equals(other.userid))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "ejbentity.Myuser[ userid=" + userid + " ]";
}

}

This is my user facade

@Stateless
public class MyuserFacade implements MyuserFacadeRemote {

@PersistenceContext(unitName = "EJBServer-ejbPU")
private EntityManager em;


protected EntityManager getEntityManager() {
    return em;
}

public MyuserFacade() {
    //super(Myuser.class);
}

@Override
public Myuser getUser(int userID) {
    Myuser aUser = new Myuser();
    aUser =   (Myuser)em.createNamedQuery("Myuser.findByUserid").setParameter("userid", userID).getResultList();
    return aUser;
}
}

My persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence   http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
   <persistence-unit name="EJBEntityPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<!--<exclude-unlisted-classes>false</exclude-unlisted-classes>-->
<class>ejbentity.Myuser</class>
<properties>
  <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/EA"/>
  <property name="javax.persistence.jdbc.password" value="test"/>
  <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
  <property name="javax.persistence.jdbc.user" value="test"/>
    </properties>
   </persistence-unit>
</persistence>

Error Log is given below:

java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.glassfish.appclient.client.acc.AppClientContainer.launch(AppClientContainer.java:446)
at org.glassfish.appclient.client.AppClientFacade.main(AppClientFacade.java:166)
Caused by: javax.ejb.EJBException
at com.sun.ejb.containers.EJBContainerTransactionManager.processSystemException(EJBContainerTransactionManager.java:752)
at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:702)
at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:507)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4566)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2074)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2044)
at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:212)
at com.sun.ejb.containers.EJBObjectInvocationHandlerDelegate.invoke(EJBObjectInvocationHandlerDelegate.java:79)
at com.sun.proxy.$Proxy266.getUser(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie.dispatchToMethod(ReflectiveTie.java:143)
at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:173)
at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatchToServant(ServerRequestDispatcherImpl.java:528)
at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatch(ServerRequestDispatcherImpl.java:199)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequestRequest(MessageMediatorImpl.java:1549)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:1425)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleInput(MessageMediatorImpl.java:930)
at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:213)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:694)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.dispatch(MessageMediatorImpl.java:496)
at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.doWork(MessageMediatorImpl.java:2222)
at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497)
at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540)
         Caused by: java.lang.IllegalArgumentException: NamedQuery of name: Myuser.findByUserid not found.
at org.eclipse.persistence.internal.jpa.QueryImpl.getDatabaseQueryInternal(QueryImpl.java:355)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createNamedQuery(EntityManagerImpl.java:1135)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createNamedQuery(EntityManagerWrapper.java:522)
at stateless.MyuserFacade.getUser(MyuserFacade.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4786)
at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:656)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:73)
at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4758)
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4746)
at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:205)
... 19 more
Java Result: 1

File Structure

like image 539
JianYA Avatar asked Mar 26 '16 15:03

JianYA


1 Answers

Actual error:

Caused by: java.lang.IllegalArgumentException: NamedQuery of name: Myuser.findByUserid not found.

Happens from:

@NamedQuery(name = "Myuser.findByUserid", query = "SELECT m FROM Myuser m WHERE m.userid = :userid"),

UPDATE: From your persistence.xml:

<persistence-unit name="EJBEntityPU" transaction-type="RESOURCE_LOCAL">

Here you are using transaction-type="RESOURCE_LOCAL". So if you use it, then you are responsible for EntityManager. Then you must have to follow (JPA Concepts: JPA 101)

  1. You must use the EntityManagerFactory to get an EntityManager
  2. The resulting EntityManager instance is a PersistenceContext/Cache
  3. An EntityManagerFactory can be injected via the @PersistenceUnit annotation only (not @PersistenceContext)
  4. You are not allowed to use @PersistenceContext to refer to a unit of type RESOURCE_LOCAL
  5. You must use the EntityTransaction API to begin/commit around every call to your EntityManger
  6. Calling entityManagerFactory.createEntityManager() twice results in two separate EntityManager instances and therefor two separate PersistenceContexts/Caches.
  7. It is almost never a good idea to have more than one instance of an EntityManager in use (don't create a second one unless you've destroyed the first)

Solution:

This is an example. you can follow the tutorial if you want to use RESOURCE_LOCAL.

Related Link:

  1. DON'T USE JPA'S RESOURCE_LOCAL ON THE SERVER
  2. Persistence unit as RESOURCE_LOCAL or JTA?
  3. persistence.xml different transaction-type attributes
like image 116
SkyWalker Avatar answered Sep 19 '22 11:09

SkyWalker