gpt4 book ai didi

java - hibernate jpa java.lang.ClassCastException : model. Dog 无法转换为 model.Dog

转载 作者:行者123 更新时间:2023-12-02 04:47:30 26 4
gpt4 key购买 nike

我有一个奇怪的异常,我不知道为什么。我在内存中有数据库中的列表,但是当我尝试访问 getter 或将实体保存在变量中时,它会引发异常。

这是实体:

package model;

import java.io.Serializable;
import javax.persistence.*;

@Entity
@NamedQuery(name="Dog.findAll", query="SELECT d FROM Dog d")
public class Dog implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;

private String name;

public Dog() {
}

public int getId() {
return this.id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

}

这是我的 session bean 实现:

@Stateless(name = "mytable")
public class TableBean implements TableLocal {

@PersistenceContext(unitName = "sample")
private EntityManager manager;

@Override
public Dog getAll() {
Dog h = manager.find(Dog.class, 1);
Hibernate.initialize(h);
return h;
}

@Override
@SuppressWarnings("unchecked")
public List<Dog> findAll() {
Query query = manager.createQuery("SELECT h FROM Dog h",Dog.class);
List<Dog> col = query.getResultList();
Hibernate.initialize(col);
return col;
}

@Override
public void addDog() {
Dog d = new Dog();
//d.setId(2);
d.setName("chappy");
manager.persist(d);
}
}

我也有一个服务bean,但是没有意义

@Stateless(name = "service")
public class ServiceBean implements ServiceLocal {

@EJB(beanName = "mytable")
TableLocal table;

@Override
public Dog getAll() {
return table.getAll();
}

@Override
public List<Dog> findAll() {
return table.findAll();
}

@Override
public void addDog() {
table.addDog();

}
}

和我的 servlet:

public class HelloServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

@EJB(beanName = "service")
ServiceLocal table;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Dog> tables = table.findAll();
Iterator<Dog> it = tables.iterator();
while (it.hasNext()) {
Dog t = it.next();
resp.getWriter().println(t.getName());
}
/*Dog h = table.getAll();
resp.getWriter().println(h.getId() + " " + h.getName());*/
/*table.addDog();
resp.getWriter().println("Added!");*/
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}

}

错误是:

2015-07-28T13:38:17.385+0300|Warning: StandardWrapperValve[HelloServlet]: Servlet.service() for servlet HelloServlet threw exception
java.lang.ClassCastException: model.Dog cannot be cast to model.Dog
at HelloServlet.doGet(HelloServlet.java:26)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)

和我的 persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="sample">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>MySQL</jta-data-source>
<class>model.Dog</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl" value="update" />
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>

最佳答案

在你的findAll()方法,使用 TypedQuery 而不是 Query。原因是,TypedQuery 的 getResultList()正在返回类型列表的列表。即在你的情况下,List<Dog>但 Query 的 getResultList 返回原始 List

您可以尝试使用以下代码:-

@Override
@SuppressWarnings("unchecked")
public List<Dog> findAll() {
TypedQuery<Dog> query = manager.createQuery("SELECT h FROM Dog h",Dog.class);
List<Dog> col = query.getResultList();
Hibernate.initialize(col);
return col;
}

关于java - hibernate jpa java.lang.ClassCastException : model. Dog 无法转换为 model.Dog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31674082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com