Hibernate输出值列表列表而不是对象列表
我提到了一些样本,这些样本引导我的方式与我已经实现的相同。 Hibernate sample
这是我的问题,
public List<Loan> getLoans(Date fromDate, Date toDate) {
factory = DbSessionManager.getSessionFactory("core");
Session session = factory.openSession();
Transaction tx = null;
List<Loan> loans = null;
try{
session.createCriteria(Loan.class).list();
tx = session.beginTransaction();
Query query = session.getNamedQuery("getLoansBetweenDates").setDate("fromDate", fromDate).setDate("toDate", toDate);
loans = query.list();
for (Iterator iterator = loans.iterator(); iterator.hasNext();) {
Loan loan = (Loan) iterator.next();
System.out.println(loan.getLoanId());
}
tx.commit();
}catch (HibernateException he) {
if(tx!=null) tx.rollback();
he.printStackTrace();
}
return loans;
}
尝试投放到贷款时出错:
SEVERE: Servlet.service() for servlet [TM_Service] in context with path [/TM_Service] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to model.core.Loan] with root cause java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to model.core.Loan
at utility.LoanManager.getLoans(LoanManager.java:75)
at controller.LoanController.getLoan(LoanController.java:46)
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.doInvoke(InvocableHandlerMethod.java:220)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
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)
换句话说,上面的代码作为Web服务公开时,没有任何强制转换,我得到以下输出。看起来像列表 - 值列表
[
[
"137",
15,
"open",
null
],
[
"147",
17,
"open",
null
],
[
"157",
16,
"open",
null
]
]
我期待以下输出,
[
{
"loanId":"137",
"customerId":15,
"status":"open",
"comments":null
},
{
"loanId":"147",
"customerId":17,
"status":"open",
"comments":null
},
{
"loanId":"157",
"customerId":16,
"status":"open",
"comments":null
}
]
POJO的
@NamedNativeQueries({
@NamedNativeQuery(
name = "getLoansBetweenDates",
query = "select * from tm_core.loan where tm_core.loan.loan_id in "
+ "(select tm_core.loan.loan_id from tm_core.loan inner join tm_core.transaction " +
"on tm_core.loan.loan_id = tm_core.transaction.loan_id " +
"where " +
"loan_status = 'open' and " +
"category = 'principal' and " +
"date >= :fromDate and " +
"date <= :toDate " +
"order by date asc)"
)
})
@Entity
@Table(name="loan")
public class Loan implements Serializable{
@Id
@Column(name="loan_id", nullable=false, unique=true)
String loanId;
@ManyToOne(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
@JoinColumn(name="customer_id", nullable = false)
@JsonBackReference
Customer customer;
@Column(name="loan_status")
String loanStatus;
@Column(name="comments")
String comments;
}
我应该实现对象映射器吗?有没有办法调整Hibernate输出,以便响应看起来像对象列表,而不是&#39;列表中的值 - &#39;