-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Failed cast of ValidateEventListener
PostPosted: Thu Mar 29, 2007 9:49 pm 
Newbie

Joined: Thu Mar 29, 2007 3:58 am
Posts: 5
I've solved this, but I find it so freaky that I thought I'd post it so others don't fall into the same trap, or even just in case it could be fixed.

Quick solution: ensure that you follow the 'normal' pattern of HibernateUtil with a static method that returns the session and no instances of the object itself.


I got a org.hibernate.validator.event.ValidateEventListener cannot be cast to org.hibernate.event.PreInsertEventListener on startup of hibernate. Other than trying to use annotations, I don't think I'm trying anything unusual.

What it turned out to be was that I'd written my own version of HibernateUtil that retrieved the session in an instance method rather than a static method.

This somehow caused the above exception to be thrown. When substituting a more traditional HibernateUtil, the exception wasn't thrown. It wasn't enough to use a static method to access it - if I created an instance of my object first it still failed - it seems that the first call to the object must be a call to a static method that gets the session.

So I suppose it's some sort of bug that depends on exact class loading order or something??

Anyway, in case it helps, details below.


Hibernate version: 3.2.2 GA
Annotations version: 3.3.0 GA
Java version: 1.6.0
JBoss version: 4.0.5 GA
Name and version of the database: Oracle 9i (9.2.0.7.0)

Copy of "Hibernate.cfg.xml" body:


Copy of "HibernateUtil" body:
(my version of Hibernate util is called SessionFactory)

Code:
    private static final List<Class> persistedClasses
        = Collections.unmodifiableList(Arrays.asList(new Class[]
        {
            Email.class,
            EmailId.class
        }));
   
    private static final org.hibernate.SessionFactory hibernateSessions;
   
    static
    {
        AnnotationConfiguration config = new AnnotationConfiguration();
        for (Class c: persistedClasses) config.addAnnotatedClass(c);
        config.configure();
        hibernateSessions = config.buildSessionFactory();
    }
   
    /** Returns a new session as part of the global hibernate configuration for
     *  this application.
     */
    public Session getSession()
    {
        return hibernateSessions.openSession();
    }


(note that the errors still occurs without adding any annotated classes, and without the config.configure() step.)

Code:
<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration
          PUBLIC
          "-//Hibernate/Hibernate Configuration DTD//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <property name="connection.datasource">java:myappDS</property>
        <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
        <property name="show_sql">false</property>
        <property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
        <property name="jta.UserTransaction">java:comp/UserTransaction</property>

    </session-factory>

</hibernate-configuration>




Full stack trace of any exception that occurs:
(DbIdFactory is simply my first class that does anything with the database.)
Code:
java.lang.ExceptionInInitializerError
        at org.myapp.DbIdFactory.<init>(DbIdFactory.java:19)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at java.lang.Class.newInstance0(Class.java:355)
        at java.lang.Class.newInstance(Class.java:308)
        at org.myapp.emailgateway.PollerServlet.init(PollerServlet.java:59)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1105)
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:932)
        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3951)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4225)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:759)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:739)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:524)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.commons.modeler.BaseModelMBean.invoke(BaseModelMBean.java:503)
        at org.jboss.mx.server.RawDynamicInvoker.invoke(RawDynamicInvoker.java:164)
        at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
        at org.apache.catalina.core.StandardContext.init(StandardContext.java:5052)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.commons.modeler.BaseModelMBean.invoke(BaseModelMBean.java:503)
        at org.jboss.mx.server.RawDynamicInvoker.invoke(RawDynamicInvoker.java:164)
        at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
        at org.jboss.web.tomcat.tc5.TomcatDeployer.performDeployInternal(TomcatDeployer.java:297)
        at org.jboss.web.tomcat.tc5.TomcatDeployer.performDeploy(TomcatDeployer.java:103)
        at org.jboss.web.AbstractWebDeployer.start(AbstractWebDeployer.java:371)
        at org.jboss.web.WebModule.startModule(WebModule.java:83)
        at org.jboss.web.WebModule.startService(WebModule.java:61)
        at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanSupport.java:289)
        at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMBeanSupport.java:245)
        at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
        at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
        at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
        at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
        at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
        at org.jboss.system.ServiceController$ServiceProxy.invoke(ServiceController.java:978)
        at $Proxy0.start(Unknown Source)
        at org.jboss.system.ServiceController.start(ServiceController.java:417)
        at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
        at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
        at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
        at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
        at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
        at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
        at $Proxy37.start(Unknown Source)
        at org.jboss.web.AbstractWebContainer.start(AbstractWebContainer.java:466)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
        at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
        at org.jboss.mx.interceptor.AbstractInterceptor.invoke(AbstractInterceptor.java:133)
        at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
        at org.jboss.mx.interceptor.ModelMBeanOperationInterceptor.invoke(ModelMBeanOperationInterceptor.java:142)
        at org.jboss.mx.interceptor.DynamicInterceptor.invoke(DynamicInterceptor.java:97)
        at org.jboss.system.InterceptorServiceMBeanSupport.invokeNext(InterceptorServiceMBeanSupport.java:238)
        at org.jboss.ws.integration.jboss.DeployerInterceptor.start(DeployerInterceptor.java:92)
        at org.jboss.deployment.SubDeployerInterceptorSupport$XMBeanInterceptor.start(SubDeployerInterceptorSupport.java:188)
        at org.jboss.deployment.SubDeployerInterceptor.invoke(SubDeployerInterceptor.java:95)
        at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
        at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
        at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
        at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
        at $Proxy38.start(Unknown Source)
        at org.jboss.deployment.MainDeployer.start(MainDeployer.java:1025)
        at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:819)
        at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:782)
        at sun.reflect.GeneratedMethodAccessor54.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
        at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
        at org.jboss.mx.interceptor.AbstractInterceptor.invoke(AbstractInterceptor.java:133)
        at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
        at org.jboss.mx.interceptor.ModelMBeanOperationInterceptor.invoke(ModelMBeanOperationInterceptor.java:142)
        at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
        at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
        at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
        at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
        at $Proxy8.deploy(Unknown Source)
        at org.jboss.deployment.scanner.URLDeploymentScanner.deploy(URLDeploymentScanner.java:421)
        at org.jboss.deployment.scanner.URLDeploymentScanner.scan(URLDeploymentScanner.java:634)
        at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.doScan(AbstractDeploymentScanner.java:263)
        at org.jboss.deployment.scanner.AbstractDeploymentScanner.startService(AbstractDeploymentScanner.java:336)
        at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanSupport.java:289)
        at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMBeanSupport.java:245)
        at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
        at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
        at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
        at org
10:00:19,473 INFO  [STDOUT] .jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
        at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
        at org.jboss.system.ServiceController$ServiceProxy.invoke(ServiceController.java:978)
        at $Proxy0.start(Unknown Source)
        at org.jboss.system.ServiceController.start(ServiceController.java:417)
        at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
        at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
        at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
        at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
        at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
        at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
        at $Proxy4.start(Unknown Source)
        at org.jboss.deployment.SARDeployer.start(SARDeployer.java:302)
        at org.jboss.deployment.MainDeployer.start(MainDeployer.java:1025)
        at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:819)
        at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:782)
        at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:766)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
        at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
        at org.jboss.mx.interceptor.AbstractInterceptor.invoke(AbstractInterceptor.java:133)
        at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
        at org.jboss.mx.interceptor.ModelMBeanOperationInterceptor.invoke(ModelMBeanOperationInterceptor.java:142)
        at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
        at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
        at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
        at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:210)
        at $Proxy5.deploy(Unknown Source)
        at org.jboss.system.server.ServerImpl.doStart(ServerImpl.java:482)
        at org.jboss.system.server.ServerImpl.start(ServerImpl.java:362)
        at org.jboss.Main.boot(Main.java:200)
        at org.jboss.Main$1.run(Main.java:490)
        at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ClassCastException: org.hibernate.validator.event.ValidateEventListener cannot be cast to org.hibernate.event.PreInsertEventListener
        at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:783)
        at org.myapp.data.SessionFactory.<clinit>(SessionFactory.java:35)
        ... 145 more


Top
 Profile  
 
 Post subject: Still a problem
PostPosted: Fri Mar 30, 2007 12:03 am 
Newbie

Joined: Thu Mar 29, 2007 3:58 am
Posts: 5
Actually, that just delayed the error - help anyone?

Code:
Hibernate failed to initialise
java.lang.ClassCastException: org.hibernate.validator.event.ValidateEventListener cannot be cast to org.hibernate.event.PreInsertEventListener
        at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:783)
        at org.myapp.data.SessionFactory.<clinit>(SessionFactory.java:40)
        at org.myapp.DbIdFactory.getNewId(DbIdFactory.java:26)
        at org.myapp.emailgateway.mail.MailClient.checkMail(MailClient.java:103)
        at org.myapp.emailgateway.PollWork.run(PollWork.java:82)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
        at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:317)
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:150)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(ScheduledThreadPoolExecutor.java:98)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:181)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:205)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
        at java.lang.Thread.run(Thread.java:619)



Top
 Profile  
 
 Post subject: Perhaps resolved this time
PostPosted: Fri Mar 30, 2007 1:01 am 
Newbie

Joined: Thu Mar 29, 2007 3:58 am
Posts: 5
OK, it seems to work OK with Annotations 3.2.1 instead of 3.3... of course i'm a little reticent to jump the gun and say it's fixed lest it blow up in my face later in the process :0)


Top
 Profile  
 
 Post subject: Re: Failed cast of ValidateEventListener
PostPosted: Tue Nov 13, 2012 1:28 pm 
Newbie

Joined: Tue Nov 13, 2012 1:24 pm
Posts: 1
Hi robmcau,
I was getting similar validating casting errors and am trying your approach. However, after I make the session static, I am now gettign initialization exception error when I try to instantiate a new SessionFatory rule in my test case. The ruel class is as follow

public class SessionFactoryRule implements MethodRule {
private SessionFactory sessionFactory;
private Transaction transaction;
private Session session;
public static Logger log = Logger.getLogger(SessionFactoryRule.class);

@Override
public Statement apply(final Statement statement, FrameworkMethod method, Object test)
{
return new Statement()
{
@Override
public void evaluate() throws Throwable
{


createSession();
beginTransaction();
try{
statement.evaluate();
} finally {
shutdown();
}
}

};
}

private void shutdown()
{
try{
try{
try{
transaction.rollback();
} catch (Exception ex) {
ex.printStackTrace();
}
session.close();
} catch (Exception ex) {
ex.printStackTrace();
}
sessionFactory.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}

/**
* Sets session to use an in memory database for performance gains
*
* @author EricL
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
private static final SessionFactory hibernateSessions;
static
{
log.info("Session Factory Created");
try {
Class.forName("org.h2.Driver");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
try {
DriverManager.getConnection("jdbc:h2:mem:", "sa", "");
} catch (SQLException e) {
e.printStackTrace();
}

AnnotationConfiguration configuration = new AnnotationConfiguration();
//configuration.addAnnotatedClass(Facility.class);
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
configuration.setProperty("hibernate.connection.driver_class", "org.h2.Driver");
configuration.setProperty("hibernate.connection.url", "jdbc:h2:mem:");
configuration.setProperty("hibernate.hbm2ddl.auto", "create");

hibernateSessions = configuration.buildSessionFactory();

// return sessionFactory;
}

public Session createSession() {
// session = sessionFactory.openSession();
// return session;

return hibernateSessions.openSession();
}

public void commit()
{
transaction.commit();
}

public void beginTransaction()
{
transaction = session.beginTransaction();
}

public Session getSession()
{
return hibernateSessions.openSession();
}
}

And in my test case I do
@Rule
public final SessionFactoryRule sf = new SessionFactoryRule();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.