NamedNativeQuery使用FK

时间:2018-05-24 21:45:27

标签: java oracle hibernate jpa exception

我正在研究一个简单的代码,用一个外键将一个实体映射到一个列表来显示结果,使用@NamedNativeQuery但是在调用时仍然得到“java.sql.SQLException:Nombre de columnanoválido”(无效的列名)那个query.list

TABLE "schema"."BEDE_ESTADOS" 

"CODIGO_ESTADO" NUMBER(2,0) NOT NULL ENABLE, 
"DESCRIPCION" VARCHAR2(50 BYTE) NOT NULL ENABLE, 
"HABILITADO" NUMBER(2,0) NOT NULL ENABLE, 
"USUARIO_ULTIMA_MODIFICACION" VARCHAR2(100 BYTE) NOT NULL ENABLE, 
"FECHA_ULTIMA_MODIFICACION" DATE NOT NULL ENABLE, 
 CONSTRAINT "PK_BEDE_ESTADOS" PRIMARY KEY ("CODIGO_ESTADO")

TABLE "schema"."BEDE_CATEGORIA_HORA" 
"CODIGO_CATEGORIA" NUMBER(2,0) NOT NULL ENABLE, 
"DESCRIPCION" VARCHAR2(50 BYTE) NOT NULL ENABLE, 
"CODIGO_ESTADO" NUMBER(2,0) NOT NULL ENABLE, 
"TOPE_HORAS" NUMBER(2,0) NOT NULL ENABLE, 
"OBJETO_GASTO" VARCHAR2(10 BYTE) NOT NULL ENABLE, 
"USUARIO_ULTIMA_MODIFICACION" VARCHAR2(100 BYTE) NOT NULL ENABLE, 
"FECHA_ULTIMA_MODIFICACION" DATE NOT NULL ENABLE,
CONSTRAINT "PK_BEDE_CATEGORIA_HORA" PRIMARY KEY ("CODIGO_CATEGORIA"),
CONSTRAINT "FK_BEDE_CATEG_HORA_BEDE_ESTADO" FOREIGN KEY ("CODIGO_ESTADO")
  REFERENCES "schema"."BEDE_ESTADOS" ("CODIGO_ESTADO")

实体

    public class BedeEstados implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "codigo_Sequence")
@SequenceGenerator(
        name = "codigo_Sequence", 
        sequenceName = "schema.SEQ_BEDE_ESTADOS_CODIGO_ESTADO",
        allocationSize = 1
)
@Basic(optional = false)
@Column(name = "CODIGO_ESTADO")
private Short codigoEstado;
@Basic(optional = false)
@Column(name = "DESCRIPCION")
private String descripcion;
@Basic(optional = false)
@Column(name = "HABILITADO")
private short habilitado;
@Basic(optional = false)
@Column(name = "USUARIO_ULTIMA_MODIFICACION")
private String usuarioUltimaModificacion;
@Basic(optional = false)
@Column(name = "FECHA_ULTIMA_MODIFICACION")
@Temporal(TemporalType.TIMESTAMP)
private Date fechaUltimaModificacion;

public BedeEstados() {
}

获取/设置.................... }

public class BedeCategoriaHora implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "codigo_SequenceCategoriaHora")
@SequenceGenerator(
        name = "codigo_SequenceCategoriaHora", 
        sequenceName = "SAE_DESIGNACIONES_WEB.SEQ_BEDE_CATEGORIA_HORA_COD_CT",
        allocationSize = 1
)
@Basic(optional = false)
@Column(name = "CODIGO_CATEGORIA")
private Short codigoCategoria;
@Basic(optional = false)
@Column(name = "DESCRIPCION")
private String descripcion;
@Basic(optional = false)
@Column(name = "TOPE_HORAS")
private short topeHoras;
@Basic(optional = false)
@Column(name = "OBJETO_GASTO")
private String objetoGasto;
@Basic(optional = false)
@Column(name = "USUARIO_ULTIMA_MODIFICACION")
private String usuarioUltimaModificacion;
@Basic(optional = false)
@Column(name = "FECHA_ULTIMA_MODIFICACION")
@Temporal(TemporalType.TIMESTAMP)
private Date fechaUltimaModificacion;
@JoinColumn(name = "CODIGO_ESTADO", referencedColumnName = "CODIGO_ESTADO")
@ManyToOne(optional = false)
private BedeEstados codigoEstado;

获取/设置............. }

@NamedNativeQuery

@NamedNativeQuery(name = "BedeCategoriaHora.obtenerCategorias",
        query ="SELECT  * FROM "
                + "(SELECT  q.*, rownum rn FROM "
                    + "(SELECT  ct.*, e.CODIGO_ESTADO AS COD_ESTADO, E.DESCRIPCION AS DESC_ESTADO "
                    + " FROM schema.BEDE_CATEGORIA_HORA ct, schema.BEDE_ESTADOS e"
                    + " WHERE ct.CODIGO_ESTADO = e.CODIGO_ESTADO"
                    + " AND ct.CODIGO_CATEGORIA LIKE :pFiltroIdRegistro"
                    + " AND UPPER(ct.DESCRIPCION) LIKE :pFiltroDescripcion "
                    + "ORDER BY ct.CODIGO_CATEGORIA)"
                + " q)  "
               + "WHERE rn BETWEEN :pPrimerRegistro AND :pUltimoRegistro",
        resultSetMapping = "obtenerCategoriasEntidad")

@SqlResultSetMapping

@SqlResultSetMapping(name = "obtenerCategoriasEntidad",
        entities = {
            @EntityResult(entityClass = BedeCategoriaHora.class,
                    fields = {
                    @FieldResult(name = "CODIGO_CATEGORIA", column = "CODIGO_CATEGORIA"),
                    @FieldResult(name = "DESCRIPCION", column = "DESC_ESTADO"),
                    @FieldResult(name = "CODIGO_ESTADO", column = "CODIGO_ESTADO"),
                    @FieldResult(name = "TOPE_HORAS", column = "TOPE_HORAS"),
                    @FieldResult(name = "OBJETO_GASTO", column = "OBJETO_GASTO"),
                    @FieldResult(name = "USUARIO_ULTIMA_MODIFICACION", column = "USUARIO_ULTIMA_MODIFICACION"),
                    @FieldResult(name = "FECHA_ULTIMA_MODIFICACION", column = "FECHA_ULTIMA_MODIFICACION"),
                    }
            ),
            @EntityResult(entityClass = BedeEstados.class,
                    fields = {
                    @FieldResult(name = "CODIGO_ESTADO", column = "COD_ESTADO"),
                    @FieldResult(name = "DESCRIPCION", column = "DESC_ESTADO"),
                    @FieldResult(name = "HABILITADO", column = "HABILITADO"),
                    @FieldResult(name = "USUARIO_ULTIMA_MODIFICACION", column = "USUARIO_ULTIMA_MODIFICACION"),
                    @FieldResult(name = "FECHA_ULTIMA_MODIFICACION", column = "FECHA_ULTIMA_MODIFICACION"),
                    }
            )
        },
        columns = {
            @ColumnResult(name = "rn")
        })

呼叫

List <Object[]> results;

     results = this.dao.getSessionFactory().getCurrentSession().getNamedQuery("BedeCategoriaHora.obtenerCategorias")
            .setParameter(lNombresParametros[0], lValoresParametros[0])
            .setParameter(lNombresParametros[1], lValoresParametros[1])
            .setParameter(lNombresParametros[2], lValoresParametros[2])
            .setParameter(lNombresParametros[3], lValoresParametros[3])
            .list();

错误

java.sql.SQLException: Nombre de columna no válido
at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3677)
at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:2749)
at oracle.jdbc.driver.OracleResultSet.getShort(OracleResultSet.java:482)
at weblogic.jdbc.wrapper.ResultSet_oracle_jdbc_driver_OracleResultSetImpl.getShort(Unknown Source)
at org.hibernate.type.descriptor.sql.SmallIntTypeDescriptor$2.doExtract(SmallIntTypeDescriptor.java:61)
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:254)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:250)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:230)
at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:331)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2283)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1527)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1455)
at org.hibernate.loader.Loader.getRow(Loader.java:1355)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:611)
at org.hibernate.loader.Loader.doQuery(Loader.java:829)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.doList(Loader.java:2542)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
at org.hibernate.loader.Loader.list(Loader.java:2271)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:316)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1842)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:157)
at cr.ac.ucr.desgweb.dao.BedeCategoriaHora_Dao.listarRegistros(BedeCategoriaHora_Dao.java:99)
at cr.ac.ucr.desgweb.dao.BedeCategoriaHora_Dao$$FastClassByCGLIB$$850413d6.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
at cr.ac.ucr.desgweb.dao.BedeCategoriaHora_Dao$$EnhancerByCGLIB$$e7819990.listarRegistros(<generated>)
at cr.ac.ucr.desgweb.modelo.BedeCategoriaHoraMgr.listarRegistros(BedeCategoriaHoraMgr.java:37)
at cr.ac.ucr.desgweb.vista.VistaBedeCategoriaHora.listarRegistros(VistaBedeCategoriaHora.java:288)
at cr.ac.ucr.desgweb.vista.VistaBedeCategoriaHora.inicializar(VistaBedeCategoriaHora.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:346)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:299)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:132)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:397)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1438)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:459)
at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:332)
at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:43)
at org.springframework.web.context.request.SessionScope.get(SessionScope.java:92)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:328)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093)
at org.springframework.web.jsf.DelegatingVariableResolver.resolveSpringBean(DelegatingVariableResolver.java:139)
at org.springframework.web.jsf.DelegatingVariableResolver.resolveVariable(DelegatingVariableResolver.java:112)
at com.sun.faces.el.VariableResolverChainWrapper.getValue(VariableResolverChainWrapper.java:107)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:143)
at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
at com.sun.el.parser.AstIdentifier.getValue(Unknown Source)
at com.sun.el.parser.AstValue.getValue(Unknown Source)
at com.sun.el.ValueExpressionImpl.getValue(Unknown Source)
at com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:71)
at javax.faces.component.UIOutput.getValue(UIOutput.java:184)
at com.icesoft.faces.renderkit.dom_html_basic.BaseInputRenderer.getValue(BaseInputRenderer.java:36)
at com.icesoft.faces.renderkit.dom_html_basic.InputTextRenderer.encodeEnd(InputTextRenderer.java:43)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:861)
at com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer.encodeParentAndChildren(DomBasicRenderer.java:370)
at com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer.encodeParentAndChildren(DomBasicRenderer.java:365)
at com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer.encodeParentAndChildren(DomBasicRenderer.java:365)
at com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer.encodeParentAndChildren(DomBasicRenderer.java:365)
at com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer.encodeParentAndChildren(DomBasicRenderer.java:365)
at com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer.encodeParentAndChildren(DomBasicRenderer.java:365)
at com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer.encodeParentAndChildren(DomBasicRenderer.java:365)
at com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer.encodeParentAndChildren(DomBasicRenderer.java:365)
at com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer.encodeParentAndChildren(DomBasicRenderer.java:365)
at com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer.encodeParentAndChildren(DomBasicRenderer.java:365)
at com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer.encodeParentAndChildren(DomBasicRenderer.java:365)
at com.icesoft.faces.renderkit.dom_html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:96)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
at com.icesoft.faces.application.D2DViewHandler.renderResponse(D2DViewHandler.java:492)
at com.icesoft.faces.application.D2DViewHandler.renderResponse(D2DViewHandler.java:497)
at com.icesoft.faces.application.D2DViewHandler.renderResponse(D2DViewHandler.java:497)
at com.icesoft.faces.application.D2DViewHandler.renderResponse(D2DViewHandler.java:497)
at com.icesoft.faces.facelets.D2DFaceletViewHandler.renderResponse(D2DFaceletViewHandler.java:282)
at com.icesoft.faces.application.D2DViewHandler.renderView(D2DViewHandler.java:159)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:110)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at com.icesoft.faces.webapp.http.core.JsfLifecycleExecutor.apply(JsfLifecycleExecutor.java:19)
at com.icesoft.faces.webapp.http.core.ReceiveSendUpdates.renderCycle(ReceiveSendUpdates.java:132)
at com.icesoft.faces.webapp.http.core.ReceiveSendUpdates.service(ReceiveSendUpdates.java:74)
at com.icesoft.faces.webapp.http.core.RequestVerifier.service(RequestVerifier.java:31)
at com.icesoft.faces.webapp.http.common.standard.PathDispatcherServer.service(PathDispatcherServer.java:24)
at com.icesoft.faces.webapp.http.servlet.BasicAdaptingServlet.service(BasicAdaptingServlet.java:16)
at com.icesoft.faces.webapp.http.servlet.PathDispatcher.service(PathDispatcher.java:23)
at com.icesoft.faces.webapp.http.servlet.SessionDispatcher.service(SessionDispatcher.java:53)
at com.icesoft.faces.webapp.http.servlet.SessionVerifier.service(SessionVerifier.java:26)
at com.icesoft.faces.webapp.http.servlet.PathDispatcher.service(PathDispatcher.java:23)
at com.icesoft.faces.webapp.http.servlet.MainServlet.service(MainServlet.java:131)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at com.icesoft.faces.webapp.xmlhttp.BlockingServlet.service(BlockingServlet.java:56)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:83)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3715)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:178)

调试

15:39:43,356  WARN JDBCExceptionReporter:233 - SQL Error: 17006, SQLState: 99999
15:39:43,356 ERROR JDBCExceptionReporter:234 - Nombre de columna no válido
15:39:43,363 DEBUG JDBCTransaction:182 - rollback
15:39:43,364 DEBUG JDBCTransaction:223 - re-enabling autocommit
15:39:43,364 DEBUG JDBCTransaction:193 - rolled back JDBC Connection

0 个答案:

没有答案