将JPA sqlresultset映射到自定义POJO

时间:2016-02-24 13:20:58

标签: java hibernate jpa

我想将JPA结果集映射到自定义POJO。 以下是代码

@SqlResultSetMapping(
    name = "SearchVO", 
    entities = {
            @EntityResult(
                    entityClass = UserInfo.class,
                    fields = {
                        @FieldResult(name = "profile_id", column = "id"),
                        @FieldResult(name = "first_name", column = "firstName"),
                        @FieldResult(name = "last_name", column = "lastName"), 
                        @FieldResult(name = "email_id", column = "email")
                    }),
            @EntityResult(
                    entityClass = ProjectVO.class,
                    fields = {
                        @FieldResult(name = "project_id", column = "id"),
                        @FieldResult(name = "project_title", column = "title")
                    }),
            @EntityResult(
                    entityClass = DemoRealVO.class,
                    fields = {
                        @FieldResult(name = "demo_reel_id", column = "reelId"), 
                        @FieldResult(name = "demo_reel_title", column = "title")
                    })
    })
public class SearchVO {
   private int profile_id;
   private int project_id;
   private int demo_reel_id;
   private String first_name;
   private String last_name;
   private String email_id;
   private String project_title;
   private String demo_reel_title;
// getter and setter
}

Query query = entityManager.createNativeQuery(
"select u.id,u.first_name,u.last_name,p.id,p.title,d.reel_id,d.title    from user_info u,projectvo p,demo_realvo d where (concat(u.first_name,' ',u.last_name,' ',p.title,' ',d.title) like lower(:queryString))", "SearchVO");

但它给了我以下错误

javax.persistence.PersistenceException: org.hibernate.MappingException: Unknown SqlResultSetMapping [SearchVO]
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1215)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1148)
    at org.hibernate.ejb.AbstractEntityManagerImpl.createNativeQuery(AbstractEntityManagerImpl.java:564)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:257)
    at com.sun.proxy.$Proxy39.createNativeQuery(Unknown Source)
    at com.cinpost.user.dao.UserDAO.search(UserDAO.java:618)
    at com.cinpost.user.dao.UserDAO$$FastClassByCGLIB$$4d9b8b3b.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:713)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:646)
    at com.cinpost.user.dao.UserDAO$$EnhancerByCGLIB$$a23dcf11.search(<generated>)
    at com.cinpost.user.bo.UserBO.search(UserBO.java:1585)
    at com.cinpost.user.controller.UserController.getSearchList(UserController.java:814)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
    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:748)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    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:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.MappingException: Unknown SqlResultSetMapping [SearchVO]
    at org.hibernate.impl.SQLQueryImpl.setResultSetMapping(SQLQueryImpl.java:359)
    at org.hibernate.ejb.AbstractEntityManagerImpl.createNativeQuery(AbstractEntityManagerImpl.java:560)
    ... 57 more

请告诉我哪里出错了。

1 个答案:

答案 0 :(得分:0)

使用JPA 2.1时,@SqlResultSetMapping注释应放在实体类上。