我正在尝试将原生查询结果映射到POJO类并面临以下错误:
org.springframework.orm.jpa.JpaSystemException: Unknown entity: com.cvrr.btq.model.LossDetails; nested exception is org.hibernate.MappingException: Unknown entity: com.cvrr.btq.model.LossDetails
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:303) ~[spring-orm-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:214) ~[spring-orm-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417) ~[spring-orm-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[spring-tx-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) ~[spring-aop-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at com.sun.proxy.$Proxy270.listLossDetails(Unknown Source) ~[na:na]
at com.cvrr.btq.service.impl.BotiqueServiceImpl.listLossDetails(BotiqueServiceImpl.java:45) ~[BotiqueServiceImpl.class:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_40]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_40]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_40]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) ~[spring-aop-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201) ~[spring-aop-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at com.sun.proxy.$Proxy271.listLossDetails(Unknown Source) ~[na:na]
at com.cvrr.btq.controller.BotiqueController.listLossDetails(BotiqueController.java:44) ~[BotiqueController.class:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_40]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_40]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_40]
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215) ~[spring-web-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) ~[spring-web-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) ~[spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781) ~[spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721) ~[spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) ~[spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943) ~[spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) ~[spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) [spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) [spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618) [servlet-api.jar:na]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) [spring-webmvc-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) [servlet-api.jar:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) [catalina.jar:8.0.20]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [catalina.jar:8.0.20]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-websocket.jar:8.0.20]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) [catalina.jar:8.0.20]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [catalina.jar:8.0.20]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) [catalina.jar:8.0.20]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [catalina.jar:8.0.20]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) [catalina.jar:8.0.20]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) [catalina.jar:8.0.20]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [catalina.jar:8.0.20]
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) [catalina.jar:8.0.20]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [catalina.jar:8.0.20]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516) [catalina.jar:8.0.20]
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086) [tomcat-coyote.jar:8.0.20]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659) [tomcat-coyote.jar:8.0.20]
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223) [tomcat-coyote.jar:8.0.20]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558) [tomcat-coyote.jar:8.0.20]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515) [tomcat-coyote.jar:8.0.20]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_40]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_40]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:8.0.20]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_40]
Caused by: org.hibernate.MappingException: Unknown entity: com.cvrr.btq.model.LossDetails
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1146) ~[hibernate-core-4.2.11.Final.jar:4.2.11.Final]
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.getSQLLoadable(SQLQueryReturnProcessor.java:336) ~[hibernate-core-4.2.11.Final.jar:4.2.11.Final]
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processRootReturn(SQLQueryReturnProcessor.java:377) ~[hibernate-core-4.2.11.Final.jar:4.2.11.Final]
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processReturn(SQLQueryReturnProcessor.java:356) ~[hibernate-core-4.2.11.Final.jar:4.2.11.Final]
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.process(SQLQueryReturnProcessor.java:172) ~[hibernate-core-4.2.11.Final.jar:4.2.11.Final]
at org.hibernate.loader.custom.sql.SQLCustomQuery.<init>(SQLCustomQuery.java:87) ~[hibernate-core-4.2.11.Final.jar:4.2.11.Final]
at org.hibernate.engine.query.spi.NativeSQLQueryPlan.<init>(NativeSQLQueryPlan.java:67) ~[hibernate-core-4.2.11.Final.jar:4.2.11.Final]
at org.hibernate.engine.query.spi.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:197) ~[hibernate-core-4.2.11.Final.jar:4.2.11.Final]
at org.hibernate.internal.AbstractSessionImpl.getNativeSQLQueryPlan(AbstractSessionImpl.java:226) ~[hibernate-core-4.2.11.Final.jar:4.2.11.Final]
at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:232) ~[hibernate-core-4.2.11.Final.jar:4.2.11.Final]
at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:157) ~[hibernate-core-4.2.11.Final.jar:4.2.11.Final]
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:268) ~[hibernate-entitymanager-4.2.11.Final.jar:4.2.11.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_40]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_40]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_40]
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:360) ~[spring-orm-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at com.sun.proxy.$Proxy277.getResultList(Unknown Source) ~[na:na]
at com.cvrr.btq.dao.impl.BotiqueDAOImpl.listLossDetails(BotiqueDAOImpl.java:107) ~[BotiqueDAOImpl.class:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_40]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_40]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_40]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) ~[spring-aop-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.1.1.RELEASE.jar:4.1.1.RELEASE]
... 51 common frames omitted
Oct 18, 2015 4:09:06 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [SpringDispatcher] in context with path [/Botique] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Unknown entity: com.cvrr.btq.model.LossDetails; nested exception is org.hibernate.MappingException: Unknown entity: com.cvrr.btq.model.LossDetails] with root cause
org.hibernate.MappingException: Unknown entity: com.cvrr.btq.model.LossDetails
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1146)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.getSQLLoadable(SQLQueryReturnProcessor.java:336)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processRootReturn(SQLQueryReturnProcessor.java:377)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processReturn(SQLQueryReturnProcessor.java:356)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.process(SQLQueryReturnProcessor.java:172)
at org.hibernate.loader.custom.sql.SQLCustomQuery.<init>(SQLCustomQuery.java:87)
at org.hibernate.engine.query.spi.NativeSQLQueryPlan.<init>(NativeSQLQueryPlan.java:67)
at org.hibernate.engine.query.spi.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:197)
at org.hibernate.internal.AbstractSessionImpl.getNativeSQLQueryPlan(AbstractSessionImpl.java:226)
at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:232)
at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:157)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:268)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:360)
at com.sun.proxy.$Proxy277.getResultList(Unknown Source)
at com.cvrr.btq.dao.impl.BotiqueDAOImpl.listLossDetails(BotiqueDAOImpl.java:107)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy270.listLossDetails(Unknown Source)
at com.cvrr.btq.service.impl.BotiqueServiceImpl.listLossDetails(BotiqueServiceImpl.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201)
at com.sun.proxy.$Proxy271.listLossDetails(Unknown Source)
at com.cvrr.btq.controller.BotiqueController.listLossDetails(BotiqueController.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
我的实体类的注释为:
@SqlResultSetMapping(
name = "LossDetailsMapping",
entities = @EntityResult(
entityClass = LossDetails.class,
fields={
@FieldResult(name = "billId", column = "billId"),
@FieldResult(name = "billSrlNum", column = "billSrlNum"),
@FieldResult(name = "totalInItems", column = "totalInItems"),
@FieldResult(name = "totalInAmount", column = "totalInAmount"),
@FieldResult(name = "totalSoldItems", column = "totalSoldItems"),
@FieldResult(name = "totalSoldAmount", column = "totalSoldAmount"),
@FieldResult(name = "remItems", column = "remItems"),
@FieldResult(name = "loss", column = "loss")
}
)
)
public class LossDetails {
//setter, getter are here
}
和我的JPA查询是:
LIST_LOSS_PROFIT_DETAILS="SELECT sd_data.bill_id as billId ,"
+ " sd_data.bill_srl_no as billSrlNum ," + " sd_data.total_act_qnt as totalInItems ,"
+ " sd_data.tot_act_rt as totalInAmount ," + " NVL(ssd_data.sold_qnt,0) as totalSoldItems ,"
+ " NVL(ssd_data.sold_act_rt,0) as totalSoldAmount,"
+ " sd_data.total_act_qnt-NVL(ssd_data.sold_qnt,0) as remItems,"
+ " sd_data.tot_act_rt-NVL(ssd_data.sold_act_rt,0) as loss" + " FROM" + " (SELECT sd.BILL_ID,"
+ " sd.BILL_SRL_NO," + " (sd.QUANTITY_PCS -sd.REVERSED_QUANTITY) AS total_act_qnt,"
+ " (sd.QUANTITY_PCS -sd.REVERSED_QUANTITY)*sd.ACTUAL_RATE AS tot_act_rt" + " FROM stock_details sd"
+ " ) sd_data" + " LEFT OUTER JOIN" + " (SELECT ssd.bill_id," + " ssd.BILL_SRL_NO,"
+ " SUM(ssd.QUANTITY_PCS -ssd.REVERSED_PCS) AS sold_qnt,"
+ " SUM((ssd.QUANTITY_PCS-ssd.REVERSED_PCS)*ssd.SOLD_RATE) AS sold_act_rt"
+ " FROM stock_sold_details ssd" + " WHERE ssd.REVERSED_PCS NOT IN ('C')"
// + " --AND ssd.SOLD_DATE > '01-JUL-2015'"
+ " GROUP BY ssd.BILL_ID," + " ssd.BILL_SRL_NO" + " ORDER BY ssd.BILL_ID," + " ssd.BILL_SRL_NO"
+ " ) ssd_data" + " ON sd_data.bill_id =ssd_data.bill_id"
+ " AND sd_data.BILL_SRL_NO=ssd_data.BILL_SRL_NO" + " where sd_data.bill_id= ? "
+ " order by sd_data.bill_id," + " sd_data.bill_srl_no";
Query query = entityManager.createNativeQuery(LIST_LOSS_PROFIT_DETAILS,LossDetails.class);
List<LossDetails> lossDtls = query.getResultList();
你能不能帮助我在这段代码中犯了什么错误?
答案 0 :(得分:2)
有两种方法可以将本机SQL查询的结果集映射到类 - 通过@EntityResult
和@ConstructorResult
<强> @EntityResult 强>
可以从本机SQL查询中检索托管实体。您可以将通过SQL查询返回的值映射到实体属性。例如,您有一个实体类LossDetails.class
,并映射到LOSS_DETAILS
表。
@Entity
@Table(name="LOSS_DETAILS)
public class LossDetails {
@Id
private Long billId;
private String billSrlNum;
private Integer totalInItems;
private Double totalInAmount;
private Integer totalSoldItems;
private Double totalSoldAmount;
private Integer remItems;
private Double loss;
}
然后,例如,您的本机SQL查询将返回ff。列:
BILL_ID, BILL_SRL_NO, TOTAL_ACT_QNT, TOTAL_ACT_RT, SOLD_QNT, SOLD_ACT_RT, REM_ITEMS, LOSS
请注意,列名称与实体类LossDetails
的属性名称不完全匹配。为此,您需要像这样指定@SqlResultSetMapping
:
@SqlResultSetMapping(name="LossDetailsMapping",
entities=@EntityResult(
entityClass=LossDetails.class,
fields={
@FieldResult(name="billId", column="BILL_ID"),
@FieldResult(name="billSrlNum", column="BILL_SRL_NO"),
@FieldResult(name="totalInItems", column="TOTAL_ACT_QNT"),
@FieldResult(name="totalInAmount", column="TOTAL_ACT_RT"),
@FieldResult(name="totalSoldItems", column="SOLD_QNT"),
@FieldResult(name="totalSoldAmount", column="SOLD_ACT_RT"),
@FieldResult(name="remItems", column="REM_ITEMS"),
@FieldResult(name="loss", column="LOSS")
}
)
)
如果您的SQL列和实体属性与名称完全匹配,则您不需要枚举这些字段。就像这样:
@SqlResultSetMapping(name="LossDetailsMapping",
entities=@EntityResult(
entityClass=LossDetails.class)
)
请注意,本机SQL查询可能从另一个表中选择(与映射到实体的表不同),只要生成的列将映射到每个实体属性并且它们的类型是兼容的。
要在创建本机查询时使用声明的结果集映射,只需按名称引用它。示例如下:
private String LIST_LOSS_PROFIT_DETAILS = "...YOUR NATIVE SQL QUERY HERE...";
Query query = entityManager.createNativeQuery(LIST_LOSS_PROFIT_DETAILS, "LossDetailsMapping");
List lossDtls = query.getResultList();
<强> @ConstructorResult 强>
如果您的目的只是将原生SQL的结果映射到POJO或bean类,并且该bean不一定必须通过对其执行保存/更新/删除操作来管理,那么您不要#39;需要一个实体类。实际上,您可以将SQL结果集列映射到纯Java对象。我相信这是你想要的。使用JPA 2.1版本,您可以通过@ConstructorResult
从本机查询构建非实体类型。
以下是一个例子:
public class LossDetails {
public LostDetails(Long billId, String billSrlNum,
Integer totalInItems, Double totalInAmount,
Integer totalSoldItems, Double totalSoldAmount,
Integer remItems, Double loss) {
this.billId = billId;
this.billSrlNum = billSrlNum;
this.totalInItems = totalInItems;
this.totalInAmount = totalInAmount;
this.totalSoldItems = totalSoldItems;
this.totalSoldAmount = totalSoldAmount;
this.remItems = remItems;
this.loss = loss;
}
private Long billId;
private String billSrlNum;
private Integer totalInItems;
private Double totalInAmount;
private Integer totalSoldItems;
private Double totalSoldAmount;
private Integer remItems;
private Double loss;
}
请注意,LossDetails
只是一个POJO,未使用@Entity
注释。还要注意构造函数的存在。使用@ConstructorResult
的sql结果集映射将使用此构造函数从本机SQL查询结果中创建POJO。
根据JPA规范:
所有列对应于预期构造函数的参数 必须使用ConstructorResult的columns元素指定 注释的顺序与参数列表的顺序相同 构造
所以这里使用了@ConstructorResult
:
@SqlResultSetMapping(name="LossDetailsMapping",
classes={
@ConstructorResult(targetClass=LossDetails.class,
columns={
@ColumnResult(name="billId", type=Long.class),
@ColumnResult(name="billSrlNum", type=String.class),
@ColumnResult(name="totalInItems", type=Integer.class),
@ColumnResult(name="totalInAmount", type=Double.class),
@ColumnResult(name="totalSoldItems", type=Integer.class),
@ColumnResult(name="totalSoldAmount", type=Double.class),
@ColumnResult(name="remItems", type=Integer.class),
@ColumnResult(name="loss", type=Double.class)
})
}
)
我不确定您使用的是哪个JPA版本2.0或2.1。 @ConstructorResult
仅在JPA 2.1版本中受支持。检查是否可以导入java.persistence.ConstructorResult
而不会出现任何编译错误。