0

这是我使用 JPA 与数据库对话的方法:

public Collection<ModuleConnection> getConnections(int parentModuleId) {
  Query query = em.createQuery("SELECT moduleConnection FROM ModuleConnection As moduleConnection WHERE moduleConnection.parentModule = :parentModuleId");
  query.setParameter("parentModuleId", parentModuleId);
  List<ModuleConnection> cons = query.getResultList();
  return cons;
}

我这样调用这个方法:

Collection<ModuleConnection> connections = bh.getConnections(module.getModuleId());

每次我这样做,我都会得到这个异常:

java.lang.IllegalArgumentException: Can not set int field xyz.BomModule.moduleId to java.lang.Integer
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:55)
    at sun.reflect.UnsafeIntegerFieldAccessorImpl.getInt(UnsafeIntegerFieldAccessorImpl.java:56)
    at sun.reflect.UnsafeIntegerFieldAccessorImpl.get(UnsafeIntegerFieldAccessorImpl.java:36)
    at java.lang.reflect.Field.get(Field.java:372)
    at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:55)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:206)
    at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3619)
    at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3335)
    at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:204)
    at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:241)
    at org.hibernate.type.EntityType.getIdentifier(EntityType.java:430)
    at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:110)
    at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:61)
    at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:514)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1589)
    at org.hibernate.loader.Loader.doQuery(Loader.java:696)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
    at org.hibernate.loader.Loader.doList(Loader.java:2228)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
    at org.hibernate.loader.Loader.list(Loader.java:2120)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:67)
    at xyz.getConnections(BomHandling.java:194)
    at org.apache.jsp.js.jquery_002dtreeview.demo.bom_005fexample_jsp._jspService(bom_005fexample_jsp.java:222)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:722)
11:38:03,373 ERROR [CachedConnectionValve] Application error: jsp did not complete its transaction

这是表格的样子:

CREATE TABLE "MODULECONNECTION"
(
    "ID"                    NUMBER(10,0) NOT NULL ENABLE,
    "QUANTITY"              NUMBER(10,0) NOT NULL ENABLE,
    "PARENTMODULE_MODULEID" NUMBER(10,0),
    "SUBMODULE_MODULEID"    NUMBER(10,0)
)

知道如何解决这个问题吗?

4

1 回答 1

0

我认为您的查询应该是

SELECT moduleConnection FROM ModuleConnection As moduleConnection 
WHERE moduleConnection.parentModule.id = :parentModuleId

如果查询的参数是 Module 类型(关联实体的类型,而不是其 ID 的类型),您可以使用原始查询

于 2012-05-01T09:57:37.133 回答