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

时间:2016-03-26 15:02:37

标签: java jpa netbeans dao

我正在尝试使用此命名查询来获取基于其ID的用户,但是我不断获得非法参数异常。我一直在看这段代码。希望有人可能会抓到我可能会错过的东西。 这是我的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 + " ]";
}

}

这是我的用户外观

@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;
}
}

我的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>

错误日志如下:

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

5 个答案:

答案 0 :(得分:2)

实际错误:

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

发生于:

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

<强>更新 从你的persistence.xml:

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

您正在使用transaction-type="RESOURCE_LOCAL"。因此,如果您使用它,那么您负责EntityManager。 然后你必须遵循(JPA Concepts: JPA 101

  1. 您必须使用EntityManagerFactory获取EntityManager
  2. 生成的EntityManager实例为PersistenceContext/Cache
  3. 可以通过EntityManagerFactory注入@PersistenceUnit 仅限注释(不是@PersistenceContext
  4. 您不得使用@PersistenceContext来指代其中一个单位 输入RESOURCE_LOCAL
  5. 您必须使用EntityTransaction API开始/提交每个 致电您EntityManger
  6. 两次调用entityManagerFactory.createEntityManager()会导致 两个独立的EntityManager实例,因此有两个独立的实例 PersistenceContexts/Caches
  7. 拥有多个实例几乎绝不是一个好主意 EntityManager正在使用中(除非你有,否则不要创建第二个) 摧毁了第一个)
  8. 解决方案:

    这是example。如果您想使用RESOURCE_LOCAL,可以按照教程。

    相关链接:

    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

答案 1 :(得分:1)

我们遇到了相同的错误,这是因为persistence.xml中缺少实体

答案 2 :(得分:0)

我注意到你的persistence.xml中有类ejbentity.Myuser。该类实际上存在于一个包含一个名为“ejbentity”的目录的包结构中?堆栈跟踪表明找不到带有NamedQuery注释的Myuser类,如果没有按预期加载包结构,将会解释这一点。

答案 3 :(得分:0)

我不认为事务类型与@NamedQuery注释有关。

此错误

  

引起:java.lang.IllegalArgumentException:名称的NamedQuery:找不到Myuser.findAll。   在org.eclipse.persistence.internal.jpa.QueryImpl.getDatabaseQueryInternal(QueryImpl.java:355)

看起来像是一个EclipseLink错误。你使用的是哪个版本?

答案 4 :(得分:0)

在你的persistence.xml中是:

<persistence-unit name="EJBEntityPU"

但在您的MyuserFacade中使用:

@PersistenceContext(unitName = "EJBServer-ejbPU")

您应该将其更改为:

@PersistenceContext(unitName = "EJBEntityPU")