这对我来说很奇怪......
所以这......会抛出ClassCastException
MyStatus object = Main.getSession().get(MyStatus.class, 1);
但这不会......
Object object = Main.getSession().get(MyStatus.class, 1);
我也做了instanceof
上面返回的Object
。它显然是一个MyStatus
对象,它具有从DB获得的所有正确数据。我的Main
类由Intellij自动生成。
public class Main {
private static final SessionFactory ourSessionFactory;
static {
try {
Configuration configuration = new Configuration();
configuration.configure();
ourSessionFactory = configuration.buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession() throws HibernateException {
return ourSessionFactory.openSession();
}
public static void main(final String[] args) throws Exception {
final Session session = getSession();
try {
System.out.println("querying all the managed entities...");
final Metamodel metamodel = session.getSessionFactory().getMetamodel();
for (EntityType<?> entityType : metamodel.getEntities()) {
final String entityName = entityType.getName();
final Query query = session.createQuery("from " + entityName);
System.out.println("executing: " + query.getQueryString());
for (Object o : query.list()) {
System.out.println(" " + o);
}
}
} finally {
session.close();
}
}
}
这是我的hibernate.cfg.xml
文件
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/testdb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">test</property>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<mapping resource="/com/test/app/MyStatus.hbm.xml" class="com.test.app.models.MyStatus" />
</session-factory>
</hibernate-configuration>
这里是Exception
抛出的
javax.servlet.ServletException: java.lang.ClassCastException: com.test.MyStatus cannot be cast to com.test.MyStatus
org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:489)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
答案 0 :(得分:0)
1)如果您在同一个类(同名和包)上有ClassCastException
,则意味着您正在处理不同的ClassLoaders
。
如果您打印MyStatus.class.getClassLoader()
和object.getClass().getClassLoader()
,您会看到它们是ClassLoader的不同实例。
当您在Glassfish类路径和战争中拥有相同的类时,可能会发生这种情况。
2)另一个奇怪的事情是,MyStatus
中com.test.app.models.MyStatus
被hibernate.cfg.xml
声明为com.test.MyStatus
,但例外情况为ClassCastException
。这是两个不同的类,但很可能与python3.4 lilac.py
问题无关。
答案 1 :(得分:0)
您是否尝试过投射结果?
MyStatus object = (MyStatus) Main.getSession().get(MyStatus.class, 1);
正如@tsolakp所说,错误很奇怪,似乎加载了两个包。