Hibernate 错误是“不”;java.lang.ClassCastException:java.util.ArrayList不能强制转换为";当我';我甚至没有向全班播放ArrayList
这里可能有我遗漏的东西,但我一直没能弄清楚 因此,我编写了一个Servlet,它本质上是为了帮助我从数据库或书籍下载一个文件。现在我知道这个错误: 22:05:18349错误[io.undertow.request](默认任务-6)UT005023:对/PubHub/DownloadBook:java.lang.ClassCastException:java.util.ArrayList的异常处理请求无法强制转换为examples.PubHub.model.Book 位于examples.pubhub.servlets.DownloadBookServlet.doGet(DownloadBookServlet.java:37) 位于javax.servlet.http.HttpServlet.service(HttpServlet.java:687) 位于javax.servlet.http.HttpServlet.service(HttpServlet.java:790) 位于io.undertow.servlet.handlers.ServletHandler.HandlerRequest(ServletHandler.java:85) 位于io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129) 位于org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71) 位于io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61) 位于io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131) 位于io.undertow.servlet.handlers.FilterHandler.HandlerRequest(FilterHandler.java:84) 位于io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.HandlerRequest(ServletSecurityRoleHandler.java:62) 位于io.undertow.servlet.handlers.ServletDispatchingHandler.HandlerRequest(ServletDispatchingHandler.java:36) 位于org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.HandlerRequest(SecurityContextAssociationHandler.java:78) 位于io.undertow.server.handlers.PredicateHandler.HandlerRequest(PredicateHandler.java:43) 位于io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131) 位于io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.HandlerRequest(ServletAuthenticationCallHandler.java:57) 位于io.undertow.server.handlers.PredicateHandler.HandlerRequest(PredicateHandler.java:43) 位于io.undertow.security.handlers.AuthenticationConstraintHandler.HandlerRequest(AuthenticationConstraintHandler.java:53) 位于io.undertow.security.handler.AbstractSecretentityHandler.HandlerRequest(AbstractSecretentityHandler.java:46) 位于io.undertow.servlet.handlers.security.ServletSecretentityConstraintHandler.HandlerRequest(ServletSecretentityConstraintHandler.java:64) 位于io.undertow.servlet.handlers.security.ServletSecurityConstraintHandler.HandlerRequest(ServletSecurityConstraintHandler.java:59) 位于io.undertow.security.handler.AuthenticationMechanismsHandler.HandlerRequest(AuthenticationMechanismsHandler.java:60) 位于io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.HandlerRequest(CachedAuthenticatedSessionHandler.java:77) 位于io.undertow.security.handlers.NotificationReceiverHandler.HandlerRequest(NotificationReceiverHandler.java:50) 位于io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.HandlerRequest(AbstractSecurityContextAssociationHandler.java:43) 位于io.undertow.server.handlers.PredicateHandler.HandlerRequest(PredicateHandler.java:43) 位于org.wildfly.extension.undertow.security.jacc.jaccontextidhandler.handleRequest(jaccontextidhandler.java:61) 位于io.undertow.server.handlers.PredicateHandler.HandlerRequest(PredicateHandler.java:43) 位于io.undertow.server.handlers.PredicateHandler.HandlerRequest(PredicateHandler.java:43) 位于io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292) 位于io.undertow.servlet.handlers.ServletiInitialHandler.access$100(ServletiInitialHandler.java:81) 在io.undertow.servlet.handlers.ServletiInitialHandler$2.call(ServletinInitialHandler.java:138) 在io.undertow.servlet.handlers.ServletiInitialHandler$2.call(ServletinInitialHandler.java:135) 位于io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48) 位于io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43) 位于io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44) 位于io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44) 位于io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44) 位于io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44) 位于io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44) 位于io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44) 位于io.undertow.servlet.handlers.ServletiInitialHandler.dispatchRequest(ServletiInitialHandler.java:272) 在io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81) 在io.undertow.servlet.handlers.ServletInitialHandler$1.HandlerRequest(ServletInitialHandler.java:104) 位于io.undertow.server.Connectors.executeRootHandler(Connectors.java:202) 在io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:805) 位于java.util.concurrent.ThreadPoolExecutor.runWorker(未知源) 位于java.util.concurrent.ThreadPoolExecutor$Worker.run(未知源) 位于java.lang.Thread.run(未知源) 意味着我不安全地将ArrayList强制转换为ArrayList不是的子类的对象。问题是,我甚至没有将ArrayList转换为该对象,在本例中,是Book 我不确定是什么导致了问题,我已经检查了实体,甚至搜索了其他地方Hibernate 错误是“不”;java.lang.ClassCastException:java.util.ArrayList不能强制转换为";当我';我甚至没有向全班播放ArrayList,hibernate,servlets,arraylist,download,classcastexception,Hibernate,Servlets,Arraylist,Download,Classcastexception,这里可能有我遗漏的东西,但我一直没能弄清楚 因此,我编写了一个Servlet,它本质上是为了帮助我从数据库或书籍下载一个文件。现在我知道这个错误: 22:05:18349错误[io.undertow.request](默认任务-6)UT005023:对/PubHub/DownloadBook:java.lang.ClassCastException:java.util.ArrayList的异常处理请求无法强制转换为examples.PubHub.model.Book 位于examples.pub
package examples.pubhub.servlets;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import examples.pubhub.dao.BookDAO;
import examples.pubhub.model.Book;
import examples.pubhub.utilities.DAOUtilities;
/**
* Servlet implementation class DownloadBookservlet
*/
@WebServlet("/DownloadBook")
public class DownloadBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
/* String isbn = request.getParameter("isbn_13"); */
String isbn = request.getParameter("isbn_13");
BookDAO dao = DAOUtilities.getBookDAO();
Book book = dao.getBookByISBN(isbn);
// In order to download the PDF to the client, we need to add a data stream to the response.
// First we set the content type to PDF, so the browser knows how to interpret the data it's receiving
response.setContentType("application/pdf");
// Then we set the filename to the book's title, so it's not a random string of characters
response.setHeader("Content-Disposition", "attachment; filename=" + book.getTitle() + ".pdf");
// Create the input stream (IN to the app FROM the book)
InputStream is = new ByteArrayInputStream(book.getContent());
// Create the output stream (OUT of the app TO the client)
OutputStream os = response.getOutputStream();
// We're going to read and write 1KB at a time
byte[] buffer = new byte[1024];
// Reading returns -1 when there's no more data left to read.
while (is.read(buffer) != -1) {
os.write(buffer);
}
// Always close your streams!
os.flush();
os.close();
is.close();
}
}
@Override
public Book getBookByISBN(String isbn) {
Session session = DAOUtilities.getSessionFactory().openSession();
session.beginTransaction();
Query query = session.createQuery("from Book where isbn_13 =:isbn_13");
query.setParameter("isbn_13", isbn);
Book book = (Book) query.uniqueResult();
session.getTransaction().commit();
return book;
}
package examples.pubhub.model;
import java.time.LocalDate;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "books")
public class Book {
@Id
@Column(name="isbn_13")
public String isbn_13; // International Standard Book Number, unique
@Column(name="title")
private String title;
@Column(name="author")
private String author;
@Column(name="publish_date")
private LocalDate publish_date; // Date of publish to the website
@Column(name="price")
private double price;
@Column(name="content")
private byte[] content;
// Constructor used when no date is specified
public Book(String isbn, String title, String author, byte[] content, double price) {
super();
this.isbn_13 = isbn;
this.title = title;
this.author = author;
this.publish_date = LocalDate.now();
this.content = content;
this.price = price;
}
// Constructor used when a date is specified
public Book(String isbn, String title, String author, LocalDate publishDate, double price, byte[] content) {
super();
this.isbn_13 = isbn;
this.title = title;
this.author = author;
this.publish_date = publishDate;
this.content = content;
this.price = price;
}
// Default constructor
public Book() {
}
public String getIsbn_13() {
return isbn_13;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public LocalDate getPublish_date() {
return publish_date;
}
public void setPublish_date(LocalDate publishDate) {
this.publish_date = publishDate;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public byte[] getContent() {
return content;
}
public void setContent(byte[] content) {
this.content = content;
}
public void setIsbn_13(String isbn) {
this.isbn_13 = isbn;
}