使用JPA预处理语句时出错

时间:2016-05-03 02:40:57

标签: jpa db2

我是JPA的新手。我正在尝试执行准备好的语句,但我一直收到错误。你能说出什么问题吗?谢谢。

这是具有预备声明的实体:

@Entity
@Table(name = "TCMSPRUSR", schema="TCMS")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "User.getName", query = "SELECT u.empnam FROM User u"),
    @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
    @NamedQuery(name = "User.findByUserid", query = "SELECT u FROM User u WHERE u.userid = :userid"),
    @NamedQuery(name = "User.findByEmpno", query = "SELECT u FROM User u WHERE u.empno = :empno"),
    @NamedQuery(name = "User.findByEmpnam", query = "SELECT u FROM User u WHERE u.empnam = :empnam"),
    @NamedQuery(name = "User.findByPassword", query = "SELECT u FROM User u WHERE u.password = :password"),
    @NamedQuery(name = "User.findByPassword1", query = "SELECT u FROM User u WHERE u.password1 = :password1"),
    @NamedQuery(name = "User.findByPassword2", query = "SELECT u FROM User u WHERE u.password2 = :password2"),
    @NamedQuery(name = "User.findByPassword3", query = "SELECT u FROM User u WHERE u.password3 = :password3"),
    @NamedQuery(name = "User.findByPassword4", query = "SELECT u FROM User u WHERE u.password4 = :password4"),
    @NamedQuery(name = "User.findByEffdte", query = "SELECT u FROM User u WHERE u.effdte = :effdte"),
    @NamedQuery(name = "User.findByPcname", query = "SELECT u FROM User u WHERE u.pcname = :pcname"),
    @NamedQuery(name = "User.findByAtmptcnt", query = "SELECT u FROM User u WHERE u.atmptcnt = :atmptcnt"),
    @NamedQuery(name = "User.findByDaysexp", query = "SELECT u FROM User u WHERE u.daysexp = :daysexp"),
    @NamedQuery(name = "User.findByPwdate", query = "SELECT u FROM User u WHERE u.pwdate = :pwdate"),
    @NamedQuery(name = "User.findByStatdesc", query = "SELECT u FROM User u WHERE u.statdesc = :statdesc"),
    @NamedQuery(name = "User.findByStatcode", query = "SELECT u FROM User u WHERE u.statcode = :statcode"),
    @NamedQuery(name = "User.findByUseflg", query = "SELECT u FROM User u WHERE u.useflg = :useflg"),
    @NamedQuery(name = "User.findByCuser", query = "SELECT u FROM User u WHERE u.cuser = :cuser"),
    @NamedQuery(name = "User.findByCdate", query = "SELECT u FROM User u WHERE u.cdate = :cdate")})
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "USERID")
    private String userid="0";
    @Basic(optional = false)
    @NotNull
    @Column(name = "EMPNO")
    private int empno;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "EMPNAM")
    private String empnam;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "PASSWORD")
    private String password;
    @Size(max = 255)
    @Column(name = "PASSWORD1")
    private String password1;
    @Size(max = 255)
    @Column(name = "PASSWORD2")
    private String password2;
    @Size(max = 255)
    @Column(name = "PASSWORD3")
    private String password3;
    @Size(max = 255)
    @Column(name = "PASSWORD4")
    private String password4;
    @Basic(optional = false)
    @NotNull
    @Column(name = "EFFDTE")
    @Temporal(TemporalType.DATE)
    private Date effdte;
    @Size(max = 255)
    @Column(name = "PCNAME")
    private String pcname;
    @Basic(optional = false)
    @NotNull
    @Column(name = "ATMPTCNT")
    private short atmptcnt;
    @Basic(optional = false)
    @NotNull
    @Column(name = "DAYSEXP")
    private short daysexp;
    @Basic(optional = false)
    @NotNull
    @Column(name = "PWDATE")
    @Temporal(TemporalType.DATE)
    private Date pwdate;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "STATDESC")
    private String statdesc;
    @Basic(optional = false)
    @NotNull
    @Column(name = "STATCODE")
    private long statcode;
    @Column(name = "USEFLG")
    private Short useflg;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "CUSER")
    private String cuser;
    @Basic(optional = false)
    @NotNull
    @Column(name = "CDATE")
    @Temporal(TemporalType.DATE)
    private Date cdate;
    @JoinColumn(name = "ORGACODE", referencedColumnName = "ORGACODE")
    @ManyToOne(optional = false)
    private Organization orgacode;
    @JoinColumn(name = "ROLEID", referencedColumnName = "ROLEID")
    @ManyToOne(optional = false)
    private Role roleid;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "userid")
    private Collection<AuditTrail> auditTrailCollection;

这是使用预准备语句的类:

@Stateless
public class UserFacade {

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

    @SuppressWarnings("unchecked")
    public List<User> findAll() {
        List<User> uList = em.createQuery("SELECT u FROM User u").getResultList();
        return uList;
    }

    @SuppressWarnings("unchecked")
    public List<User> getName() {
        List<User> uList = em.createQuery("SELECT u.empnam FROM User u").getResultList();
        return uList;
    }
}

执行getName()方法没有问题,但findAll()方法有错误。

这是堆栈痕迹

[5/3/16 9:32:19:456 CST] 00000244 BusinessExcep E   CNTR0020E: EJB threw an unexpected (non-declared) exception during invocation of method "findAll" on bean "BeanId(TCMS#TCMSEJB.jar#UserFacade, null)". Exception data: <openjpa-2.2.3-SNAPSHOT-r422266:1686911 fatal general error> org.apache.openjpa.persistence.PersistenceException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=ASG2.TCMSPRUSR, DRIVER=4.13.127 {prepstmnt 1878102677 SELECT t0.USERID, t0.ATMPTCNT, t0.CDATE, t0.CUSER, t0.DAYSEXP, t0.EFFDTE, t0.EMPNAM, t0.EMPNO, t1.ORGACODE, t1.CDATE, t1.CUSER, t1.ORGAABBR, t1.ORGADESC, t1.STATUS, t0.PASSWORD, t0.PASSWORD1, t0.PASSWORD2, t0.PASSWORD3, t0.PASSWORD4, t0.PCNAME, t0.PWDATE, t2.ROLEID, t2.ACCTIND, t2.CDATE, t2.CUSER, t2.ROLEDESC, t2.UNITCAT, t0.STATCODE, t0.STATDESC, t0.USEFLG FROM TCMSPRUSR t0 INNER JOIN CFASFA0740 t1 ON t0.ORGACODE = t1.ORGACODE INNER JOIN TCMSPRROL t2 ON t0.ROLEID = t2.ROLEID  optimize for 1 row} [code=-204, state=42704]SQLCA OUTPUT[Errp=SQLNQ1F7, Errd=-2145779603, 0, 0, 0, -10, 0]
DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=ASG2.TCMSPRUSR, DRIVER=4.13.127
DB2 SQL Error: SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2;-204;42704;ASG2.TCMSPRUSR, DRIVER=4.13.127
DB2 SQL Error: SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2;-204;42704;ASG2.TCMSPRUSR, DRIVER=4.13.127
FailedObject: SELECT u FROM User u [java.lang.String]
    at org.apache.openjpa.jdbc.sql.DBDictionary.narrow(DBDictionary.java:4996)
    at org.apache.openjpa.jdbc.sql.DBDictionary.newStoreException(DBDictionary.java:4956)
    at org.apache.openjpa.jdbc.sql.DB2Dictionary.newStoreException(DB2Dictionary.java:571)
    at org.apache.openjpa.jdbc.sql.SQLExceptions.getStore(SQLExceptions.java:136)
    at org.apache.openjpa.jdbc.sql.SQLExceptions.getStore(SQLExceptions.java:118)
    at org.apache.openjpa.jdbc.sql.SQLExceptions.getStore(SQLExceptions.java:70)
    at org.apache.openjpa.jdbc.kernel.SelectResultObjectProvider.handleCheckedException(SelectResultObjectProvider.java:155)
    at org.apache.openjpa.lib.rop.EagerResultList.<init>(EagerResultList.java:40)
    at org.apache.openjpa.kernel.QueryImpl.toResult(QueryImpl.java:1258)
    at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:1014)
    at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:870)
    at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:801)
    at org.apache.openjpa.kernel.DelegatingQuery.execute(DelegatingQuery.java:542)
    at org.apache.openjpa.persistence.QueryImpl.execute(QueryImpl.java:286)
    at org.apache.openjpa.persistence.QueryImpl.getResultList(QueryImpl.java:302)
    at com.ibm.ws.persistence.QueryImpl.getResultList(QueryImpl.java:118)
    at org.apache.openjpa.persistence.QueryImpl.getSingleResult(QueryImpl.java:330)
    at ph.gov.bsp.tcms.core.UserFacade.findAll(UserFacade.java:29)
    at ph.gov.bsp.tcms.core.EJSLocalNSLUserFacade_55a49658.findAll(EJSLocalNSLUserFacade_55a49658.java)
    at ph.gov.bsp.tcms.test.Test.print(Test.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:88)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
    at java.lang.reflect.Method.invoke(Method.java:613)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:268)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
    at org.apache.myfaces.view.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:83)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:83)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:51)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:404)
    at javax.faces.component.UICommand.broadcast(UICommand.java:103)
    at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:995)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:278)
    at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1307)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:733)
    at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:34)
    at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:172)
    at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:119)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1232)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:781)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:480)
    at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1114)
    at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:87)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:940)
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1817)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:200)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:463)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:530)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:316)
    at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:88)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1881)
Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=ASG2.TCMSPRUSR, DRIVER=4.13.127 {prepstmnt 1878102677 SELECT t0.USERID, t0.ATMPTCNT, t0.CDATE, t0.CUSER, t0.DAYSEXP, t0.EFFDTE, t0.EMPNAM, t0.EMPNO, t1.ORGACODE, t1.CDATE, t1.CUSER, t1.ORGAABBR, t1.ORGADESC, t1.STATUS, t0.PASSWORD, t0.PASSWORD1, t0.PASSWORD2, t0.PASSWORD3, t0.PASSWORD4, t0.PCNAME, t0.PWDATE, t2.ROLEID, t2.ACCTIND, t2.CDATE, t2.CUSER, t2.ROLEDESC, t2.UNITCAT, t0.STATCODE, t0.STATDESC, t0.USEFLG FROM TCMSPRUSR t0 INNER JOIN CFASFA0740 t1 ON t0.ORGACODE = t1.ORGACODE INNER JOIN TCMSPRROL t2 ON t0.ROLEID = t2.ROLEID  optimize for 1 row} [code=-204, state=42704]
    at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:219)
    at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:203)
    at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.access$700(LoggingConnectionDecorator.java:59)
    at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator$LoggingConnection$LoggingPreparedStatement.executeQuery(LoggingConnectionDecorator.java:1118)
    at org.apache.openjpa.lib.jdbc.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:268)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreManager$CancelPreparedStatement.executeQuery(JDBCStoreManager.java:1801)
    at org.apache.openjpa.lib.jdbc.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:258)
    at org.apache.openjpa.jdbc.sql.SelectImpl.executeQuery(SelectImpl.java:499)
    at org.apache.openjpa.jdbc.sql.SelectImpl.execute(SelectImpl.java:424)
    at com.ibm.ws.persistence.jdbc.sql.SelectImpl.execute(SelectImpl.java:89)
    at org.apache.openjpa.jdbc.sql.SelectImpl.execute(SelectImpl.java:391)
    at org.apache.openjpa.jdbc.sql.LogicalUnion$UnionSelect.execute(LogicalUnion.java:427)
    at org.apache.openjpa.jdbc.sql.LogicalUnion.execute(LogicalUnion.java:230)
    at org.apache.openjpa.jdbc.sql.LogicalUnion.execute(LogicalUnion.java:220)
    at org.apache.openjpa.jdbc.kernel.SelectResultObjectProvider.open(SelectResultObjectProvider.java:94)
    at org.apache.openjpa.lib.rop.EagerResultList.<init>(EagerResultList.java:34)
    ... 52 more

0 个答案:

没有答案