我正在尝试将本机SQL结果映射到我的POJO,但是返回错误。这是完整的堆栈跟踪:
Hibernate: SELECT * FROM members_tb where memberName like ? 2019-12-19 07:40:20.688 TRACE 32472 --- [nio-8084-exec-2] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [%Frank%] java.lang.IllegalArgumentException: Could not locate appropriate constructor on class : com.app.ecclesiamainframe.entity.Members at org.hibernate.loader.custom.ConstructorResultColumnProcessor.resolveConstructor(ConstructorResultColumnProcessor.java:92) at org.hibernate.loader.custom.ConstructorResultColumnProcessor.performDiscovery(ConstructorResultColumnProcessor.java:45) at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:494) at org.hibernate.loader.Loader.processResultSet(Loader.java:2338) at org.hibernate.loader.Loader.getResultSet(Loader.java:2294) at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2050) at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2012) at org.hibernate.loader.Loader.doQuery(Loader.java:953) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354) at org.hibernate.loader.Loader.doList(Loader.java:2815) at org.hibernate.loader.Loader.doList(Loader.java:2797) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2629) at org.hibernate.loader.Loader.list(Loader.java:2624) at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:338) at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2123) at org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1134) at org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:173) at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1526) at org.hibernate.query.Query.getResultList(Query.java:165) at com.app.ecclesiamainframe.service.impl.MembersServiceImpl.findByMembername(MembersServiceImpl.java:63) at com.app.ecclesiamainframe.service.impl.MembersServiceImpl$$FastClassBySpringCGLIB$$b6791a90.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:769) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:366) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:99) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689) at com.app.ecclesiamainframe.service.impl.MembersServiceImpl$$EnhancerBySpringCGLIB$$81c0914a.findByMembername(<generated>) at com.app.ecclesiamainframe.controller.MembersController.getMember(MembersController.java:64) 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:498) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1591) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) 2019-12-19 07:40:21.744 INFO 32472 --- [nio-8084-exec-2] i.StatisticalLoggingSessionEventListener : Session Metrics { 1250700 nanoseconds spent acquiring 1 JDBC connections; 0 nanoseconds spent releasing 0 JDBC connections; 0 nanoseconds spent preparing 0 JDBC statements; 0 nanoseconds spent executing 0 JDBC statements; 0 nanoseconds spent executing 0 JDBC batches; 0 nanoseconds spent performing 0 L2C puts; 0 nanoseconds spent performing 0 L2C hits; 0 nanoseconds spent performing 0 L2C misses; 0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections); 0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections) } 2019-12-19 08:39:48.231 WARN 32472 --- [l-1 housekeeper] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Thread starvation or clock leap detected (housekeeper delta=45s935ms542µs800ns). 2019-12-19 08:46:56.149 WARN 32472 --- [l-1 housekeeper] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Thread starvation or clock leap detected (housekeeper delta=3m17s562ms942µs). 2019-12-19 08:46:57.264 WARN 32472 --- [l-2 housekeeper] com.zaxxer.hikari.pool.HikariPool : HikariPool-2 - Thread starvation or clock leap detected (housekeeper delta=3m17s563ms485µs800ns).
这是我的Membermapping.xml:
<sql-result-set-mapping name="MemberMappingXml"> <constructor-result target-class="com.app.ecclesiamainframe.entity.Members"> <column name="memberId" /> <column name="dcaRegno" /> <column name="address" /> <column name="age" /> <column name="areaId" /> <column name="birthday" /> <column name="cellId" /> <column name="dca" /> <column name="department" /> <column name="editor" /> <column name="firstTime" /> <column name="gender" /> <column name="maritalStatus" /> <column name="memberEmail" /> <column name="memberHomePhoneNum" /> <column name="memberMobileNum" /> <column name="memberName" /> <column name="note" /> <column name="secondTime" /> </constructor-result> </sql-result-set-mapping>
这是我的实体类:
/** * */ package com.app.ecclesiamainframe.entity; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.ColumnResult; import javax.persistence.ConstructorResult; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.SqlResultSetMapping; import javax.persistence.Table; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; import com.vladmihalcea.hibernate.type.json.JsonStringType; import lombok.Data; /** * @author Harry * */ @Entity @Table(name="members_tb") @Data @TypeDef( name = "json", typeClass = JsonStringType.class ) @SqlResultSetMapping( name = "MemberMapping", classes = @ConstructorResult( targetClass = Members.class, columns = { @ColumnResult(name = "memberId", type = Long.class), @ColumnResult(name = "dcaRegno", type = Long.class), @ColumnResult(name = "address"), @ColumnResult(name = "age"), @ColumnResult(name = "areaId", type = Long.class), @ColumnResult(name = "birthday"), @ColumnResult(name = "cellId", type = Long.class), @ColumnResult(name = "dca"), @ColumnResult(name = "department"), @ColumnResult(name = "editor"), @ColumnResult(name = "firstTime"), @ColumnResult(name = "gender"), @ColumnResult(name = "maritalStatus"), @ColumnResult(name = "memberEmail"), @ColumnResult(name = "memberHomePhoneNum", type = Long.class), @ColumnResult(name = "memberMobileNum", type = Long.class), @ColumnResult(name = "memberName"), @ColumnResult(name = "note"), @ColumnResult(name = "secondTime") })) public class Members implements Serializable { /** * */ public Members() {} private static final long serialVersionUID = 1L; @Id @Column(name="memberId") @GeneratedValue(strategy = GenerationType.AUTO) private Long memberId; @Column(name="dcaRegno") private Long dcaRegno; @Column(name="memberName") private String memberName; @Column(name="memberMobileNum") private Long memberMobileNum; @Column(name="memberHomePhoneNum") private Long memberHomePhoneNum; @Column(name="memberEmail") private String memberEmail; @Column(name="gender") private String gender; @Column(name="department") private String department; @Type(type = "json") @Column(columnDefinition = "json",name="address") private String address; @Column(name="cellId") private Long cellId; @Column(name="areaId") private Long areaId; @Column(name="maritalStatus") private String maritalStatus; @Column(name="age") private String age; @Column(name="birthday") private String birthday; @Column(name="firstTime") private String firstTime; @Column(name="secondTime") private String secondTime; @Type(type = "json") @Column(columnDefinition = "json",name="dca") private String dca; @Column(name="note") private String note; @Column(name="editor") private String editor; public Members(Long memberId,String memberName) { this.memberId = memberId; this.memberName = memberName; } public Members(Long memberId,Long dcaRegno,String memberName,Long memberMobileNum,Long memberHomePhoneNum,String memberEmail,String gender,String department, String address,Long cellId,Long areaId,String maritalStatus,String age,String birthday,String firstTime,String secondTime, String dca,String note,String editor) { this.memberId = memberId; this.dcaRegno = dcaRegno; this.address = address; this.age = age; this.areaId = areaId; this.birthday = birthday; this.cellId = cellId; this.dca = dca; this.department = department; this.editor = editor; this.firstTime = firstTime; this.gender = gender; this.maritalStatus = maritalStatus; this.memberEmail = memberEmail; this.memberHomePhoneNum = memberHomePhoneNum; this.memberMobileNum = memberMobileNum; this.memberName = memberName; this.note = note; this.secondTime = secondTime; } }
这是我的会员服务实施:
@Transactional @SuppressWarnings("unchecked") public List<Members> findByMembername(String memberName) { //return membersDao.findByMemberName(memberName); List<Members> members = null; try { session = HibernateUtil.getSessionFactory().openSession(); transaction = session.beginTransaction(); // Native query selecting all columns Query query = session.createNativeQuery("SELECT * FROM members_tb where memberName like :memberName","MemberMapping") .setParameter("memberName","%"+memberName+"%"); //named parameter binding members = query.getResultList(); transaction.commit(); } catch (Exception e) { e.printStackTrace(); } finally { if (session != null) { session.close(); } } // HibernateUtil.shutdown(); return members; }
请如何消除错误并设计适当的构造函数? 尽管我希望将所有列都映射到结果中,但可能不必将每一列都加载到构造函数中。
您将为此创建一个构造函数。
在您使用过的类中创建一个构造函数,并按照SqlResultsetMapping与在变量中保留变量的顺序相同的顺序保留它们。SqlResultsetMapping
SqlResultsetMapping