Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Annotation x Local Session FactoryBean

  1. #1
    Join Date
    Dec 2008
    Posts
    4

    Default Annotation x Local Session FactoryBean

    Hello,

    I'm changing an existing working webapplication to use Annotation Session FactoryBean instead of Local Session FactoryBean.
    When i try to compile and run the existent tests, which worked before my changes, i get this Error:

    Error creating bean with name 'sessionFactory' defined in class path resource [spring/persistence.spring.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.cfg.AnnotationConfiguration

    I use maven to declare my dependencies, then i think i have already all necessary jar files.

    The SessionFactory was declared as:

    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean">
    <property name="annotatedClasses">
    <list>
    <value>....</value>
    <value>....</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">${database.dialect}</prop>
    <prop key="hibernate.cache.provider_class">
    org.hibernate.cache.EhCacheProvider
    </prop>
    <prop key="hibernate.cache.use_query_cache">true</prop>
    </props>
    <property name="dataSource" ref="dataSource" />
    </bean>

    Does anyone has an idea how to solve this problem?
    thanks in advance,

  2. #2
    Join Date
    Dec 2008
    Posts
    5

    Default

    I am also having the same issue. I am getting the "Could not initialize class org.hibernate.cfg.AnnotationConfiguration" error. Here is how my xml configuration for AnnotationSessionFactoryBean looks-

    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.search.default.indexBase">./lucene/indexes</prop>
    <prop key="hibernate.search.default.directory_provider"> org.hibernate.search.store.FSDirectoryProvider</prop>
    </props>
    </property>

    <property name="annotatedClasses">
    <list>
    <value>demo.myproduct.SearchBean</value>
    </list>
    </property>

    <property name="annotatedPackages">
    <list>
    <value>demo.myproduct</value>
    </list>
    </property>

    </bean>

  3. #3
    Join Date
    Jun 2007
    Location
    Minsk, Belarus
    Posts
    215

    Default

    NoClassDefFoundError - means that there is not required library in classpath.
    Do you have hibernate-annotations.jar in classpath?

  4. #4
    Join Date
    Dec 2008
    Posts
    5

    Default

    Andrei, I have verified that the hibernate-annotations.jar is there in the classpath. I am still getting this error. I am using maven and this is how the classpath looks-

    <classpathentry kind="var" path="M2_REPO/org/springframework/spring/2.5.4/spring-2.5.4.jar" sourcepath="M2_REPO/org/springframework/spring/2.5.4/spring-2.5.4-sources.jar"/>
    <classpathentry kind="var" path="M2_REPO/org/hibernate/hibernate/3.2.4.ga/hibernate-3.2.4.ga.jar"/>
    <classpathentry kind="var" path="M2_REPO/org/hibernate/hibernate-annotations/3.4.0.GA/hibernate-annotations-3.4.0.GA.jar"/>

  5. #5
    Join Date
    Jun 2007
    Location
    Minsk, Belarus
    Posts
    215

    Default

    Can you check whether jar really is deployed?
    tomcat\webapps\app\WEB-INF\lib

    Eclipse has "Java EE Module Dependencies" feature. jar can be in classpath but won't be deployed.

    And can you show full exception stack trace?

  6. #6
    Join Date
    Dec 2008
    Posts
    5

    Default

    I can see the jar files in tomcat deployment folder, so that probably is not a problem. Here is the stack trace-

    Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.cfg.AnnotationConfiguration
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Construc tor.java:513)
    at org.springframework.beans.BeanUtils.instantiateCla ss(BeanUtils.java:96)
    at org.springframework.beans.BeanUtils.instantiateCla ss(BeanUtils.java:74)
    at org.springframework.orm.hibernate3.LocalSessionFac toryBean.newConfiguration(LocalSessionFactoryBean. java:772)
    at org.springframework.orm.hibernate3.LocalSessionFac toryBean.buildSessionFactory(LocalSessionFactoryBe an.java:517)
    at org.springframework.orm.hibernate3.AbstractSession FactoryBean.afterPropertiesSet(AbstractSessionFact oryBean.java:211)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.invokeInitMethods(Abstr actAutowireCapableBeanFactory.java:1367)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1333)

  7. #7
    Join Date
    Jun 2007
    Location
    Minsk, Belarus
    Posts
    215

    Default

    Probably issue with hibernate library:

    <classpathentry kind="var" path="M2_REPO/org/hibernate/hibernate/3.2.4.ga/hibernate-3.2.4.ga.jar"/>
    hibernate-annotations declare dependency on hibernate-core 3.3.0.SP1
    http://repository.jboss.org/maven2/o...s-3.4.0.GA.pom


    I have tried to instantiate AnnotationConfiguration as BeanUtils does. And it is created successfully with libraries in classpath:

    Code:
    dom4j-1.6.1.jar
    hibernate-annotations-3.4.0.GA.jar
    hibernate-core-3.3.1.GA.jar
    hibernate-commons-annotations-3.1.0.GA.jar
    log4j-1.2.15.jar
    slf4j-api-1.5.6.jar
    slf4j-log4j12-1.5.6.jar
    Code:
    public class A {
    	public static void main(String[] args) throws Exception {
    		instantiateClass(org.hibernate.cfg.AnnotationConfiguration.class);
    	}
    
    	public static Object instantiateClass(Class clazz) throws Exception {
    			return instantiateClass(clazz.getDeclaredConstructor((Class[]) null), null);
    	}
    
    	public static Object instantiateClass(Constructor ctor, Object[] args) throws Exception {
    			ctor.setAccessible(true);
    			return ctor.newInstance(args);
    	}
    }
    Last edited by Andrei Tsibets; Dec 13th, 2008 at 03:51 PM.

  8. #8
    Join Date
    Dec 2008
    Posts
    5

    Default

    Thanks Andrei,

    It was an issue with missing entries in classpath. Once I downloaded missing jars and defined classpath correctly, everything worked as expected.

    A good lesson too - always verify against the dependency POM to make sure everything is in place.

    Thanks again!

  9. #9
    Join Date
    Dec 2004
    Location
    Asheville, NC
    Posts
    82

    Default

    I also get this exception when I run a test in a Spring project which used to use an Ant build but which I am now trying to use Maven.

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateSessionFactory' defined in class path resource [applicationContext_dataSourceOnly.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.cfg.AnnotationConfiguration
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
            at java.security.AccessController.doPrivileged(Native Method)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
            at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
            at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
            at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
            at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
            at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
            at org.springframework.test.AbstractSingleSpringContextTests.createApplicationContext(AbstractSingleSpringContextTests.java:213)
            at org.springframework.test.AbstractSingleSpringContextTests.loadContextLocations(AbstractSingleSpringContextTests.java:189)
            at org.springframework.test.AbstractSingleSpringContextTests.loadContext(AbstractSingleSpringContextTests.java:169)
    
    etc.
    I can see the class in the hibernate-annotations-3.4.0.GA.jar file, and I assume it is in the classpath since it is listed in the project's pom.xml.

    Below is the pom.xml for my project. Can anyone see what I'm missing? The build (compile) works fine, but running the tests from within Eclipse, NetBeans, or Maven at the command line gives me the above error.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.mycom.network</groupId>
        <artifactId>lifecycle</artifactId>
        <packaging>jar</packaging>
        <version>1.0-SNAPSHOT</version>
        <name>lifecycle</name>
        <description>Lifecycle Manager</description>
        <url>http://maven.apache.org</url>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        <dependencies>
            <dependency>
                <groupId>javax.jms</groupId>
                <artifactId>jms</artifactId>
                <version>1.1</version>
            </dependency>
            <dependency>
                <groupId>javax.transaction</groupId>
                <artifactId>jta</artifactId>
                <version>1.1</version>
            </dependency>
            <dependency>
                <groupId>com.sun.messaging.mq</groupId>
                <artifactId>imq</artifactId>
                <version>4.2</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring</artifactId>
                <version>2.5.6</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate</artifactId>
                <version>3.2.6.ga</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-annotations</artifactId>
                <version>3.4.0.GA</version>
            </dependency>
            <dependency>
                <groupId>org.easymock</groupId>
                <artifactId>easymock</artifactId>
                <version>2.4</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.5</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>2.5.6</version>
            </dependency>
            <dependency>
                <groupId>org.easymock</groupId>
                <artifactId>easymockclassextension</artifactId>
                <version>2.4</version>
            </dependency>
            <dependency>
                <groupId>hsqldb</groupId>
                <artifactId>hsqldb</artifactId>
                <version>1.8.0.7</version>
            </dependency> 
        </dependencies>
    </project>
    The tests were running fine before, but now this exception is killing all of them. Does anyone have any suggestions on where to look or what to try next? Thanks in advance for any ideas, I'm stumped.

    --James

  10. #10
    Join Date
    Dec 2008
    Posts
    5

    Default

    Make sure you have following in classpath -

    dom4j-1.6.1.jar
    hibernate-annotations-3.4.0.GA.jar
    hibernate-core-3.3.1.GA.jar
    hibernate-commons-annotations-3.1.0.GA.jar
    log4j-1.2.15.jar
    slf4j-api-1.5.6.jar
    slf4j-log4j12-1.5.6.jar

    I had the same issue, but after correcting classpath for above mentioned entries I was able to resolve this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •