问题描述
所以这个...会抛出无法转换为com.test.MyStatus $ b。 $ b 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。服务(ServletContainer.java:228)
您在同一个类(相同的名称和包)上有 ClassCastException ,这意味着您正在处理不同的 ClassLoaders 。
如果打印 MyStatus.class.getClassLoader()和 object.getClass()。getClassLoader()你会看到它们是ClassLoader的不同实例。
当您的Glassfish类路径和您的战争中有相同的类时,可能会发生这种情况。
2)另一个奇怪的事情是,您已将 MyStatus 声明为 com.test。 hibernate.cfg.xml 中的app.models.MyStatus ,但例外说 com.test.MyStatus 。这是两个不同的类,但很可能与 ClassCastException 问题无关。
This is very weird to me...
so this...would throw ClassCastException
MyStatus object = Main.getSession().get(MyStatus.class, 1);
but this will not...
Object object = Main.getSession().get(MyStatus.class, 1);
I also did instanceof the Object returned above. It is clearly an MyStatus object and it has all correct data got from DB. My Main class is auto-generated by 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(); } } }
here is my hibernate.cfg.xml file
<?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>
and here is the Exception thrown
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)
1) When you have ClassCastException on the same class (same name and package) it means that you are dealing with different ClassLoaders. If you print MyStatus.class.getClassLoader() and object.getClass().getClassLoader() you will see that they are different instances of ClassLoader.This situation can happen when you have the same class in your Glassfish classpath and in your war.
2) Another weird thing is that you have MyStatus declared as com.test.app.models.MyStatus in your hibernate.cfg.xml but the exception says com.test.MyStatus. These are two different classes but most likely are not related to the ClassCastException issue.
这篇关于在Hibernate中调用getSession()。get()时得到ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!