Announcement Announcement Module
No announcement yet.
SessionFactory configuration problem Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • SessionFactory configuration problem

    Hello everyone! How are you?

    I'm starting with Spring and got some problems in a project I am doing here. I am using Spring 3.0.6 along with Hibernate 3.2.5 in NetBeans 7.1.

    I decided to use annotation-based configuration to work with Hibernate. I find it easier than programming XML files.
    During deployment, my application connects to the database and creates the tables just as expected. But when I run a test file to test the DAOs and insert a new object in the database, I receive an error message regarding the Session Factory, that prevents my Application Context of being correctly loaded.

    I have sent to pastebin some of the files I think are important. Please click on the links to check their code.
    Just as a reference, the code I pasted as (click here to view) is the one I used to load the application context and instantiate the beans in the XML.

    And, well... after running the file showed above, the following log/error message appeared: click here to view.

    As you can see, the main exception revealed is a bean creation exception due to and unknown method:
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'sessionFactory' defined in URL [file:C:/Users/Isabela/Documents/MuitoBafao/Code/MuitoBafao/web/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V

    Problem is, I didn't even change the SessionFactory XML code. I copied it from a book I am following and just changed the annotated packages.
    I have also tried using the property "annotatedClasses" and adding all off them from muitobafao.model, one by one, on the list, but had the same result.

    Can anyone please enlighten me? :/

    Last edited by toffolo; May 9th, 2012, 08:39 AM. Reason: grammar correction :P

  • #2
    Judging from the stacktrace you have an incorrect version of the asm libraries. Make sure you have a version to match compatible for the hibernate version you use.


    • #3
      Hey Marten, thank you for your answer.

      I thought it was strange as all the libraries were automatically included by NetBeans when I created the project. I haven't changed any of Spring or Hibernate configuration since I installed the IDE (like 45 days ago).

      Anyway ... I redownloaded the whole bundle from Hibernate repository and replaced the org.objectweb.asm library in the project libraries (as that's the one causing the error, right?), but it apparently had no effect. The exception returned stays pretty much the same.

      Just as a reference, I posted the new stacktrace here. You can verify it's not different from the first one.

      As Spring's asm library isn't even listed in the stacktrace, I believe that I don't need to touch it.

      Any other ideas?



      • #4
        Also make sure you have a proper cglib library... (In general I suggest maven to manage your dependencies).

        I doubt you have classloading issues as this is a command line app.


        • #5
          Well, in fact I have cglib-2.1.3 in my libraries. NetBeans also preconfigured it when setting up the project.

          Below you may see a snapshot of all the libraries that are included in the project.

          I'll attempt to create a new project using Maven + Spring + Hibernate to see if I get any success.
          Attached Files


          • #6
            2.1.3 is pretty old (well actually cglib is pretty old for that matter ). I suggest using the 2.2.2 version and the nodep version (that is/was the version that shipped with the spring framework < 2.5 and is also the last cglib version).

            Also the snapshots get resized so it is pretty much unreadable .


            • #7
              Well, I created a Maven + Spring + Hibernate project in NetBeans and as it didn't have a hibernate.cfg.xml file I include the properties at applicationContext.xml, modifying the SessionFactory and resulting in this XML file.

              The stacktrace is at But you can see in the last lines that my Session Factory is still a problem.


              • #8
                No it isn't... It is your configuration... Have you actually READ the stack trace? Judging from the stack trace you point to a package which contains your entities and NOT to the classes, you must list the classes. I strongly suggest a read of the reference guide or at least follow the tutorial you are following.


                • #9
                  Wow Marten, now you got me confused here.

                  The book I'm following (Manning: "Spring in Action", 2nd Ed) uses the annotated model classes as the entities to be used in the XML configuration for sessionFactory (in applicationContext.xml). The exact code provided by the book for this configuration is:
                  <bean id="sessionFactory"
                     <property name="dataSource" ref="dataSource" />
                     <property name="annotatedClasses">
                     <property name="hibernateProperties">
                           <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                  As said in the book just after presenting this code:
                  "However, instead of configuring one or more mapping files, we must configure AnnotationSessionFactoryBean with one or more classes that are annotated for persistence with Hibernate. Here we’ve listed the domain objects in the RoadRantz application."

                  As you can see, I adjusted the code with my own domain objects (POJO classes annotated for Hibernate persistence) and, as I already had a Hibernate configuration file, I replaced the "hibernateProperties" property with a reference to my hibernate.cfg.xml file previously created by NetBeans.

                  Once again, my applicationContext.xml using the annotatedClasses property for sessioNFactory configuration is at this link.

                  The book configures the Hibernate-backed DAO beans with the code
                  <bean id="rantDao" class="com.roadrantz.dao.hibernate.HibernateRantDao">
                     <property name="sessionFactory" ref="sessionFactory" />
                  and, as you can see in my first post, I adjusted my XML accordingly.

                  Reading your last comment, I'm afraid I couldn't abstract the difference you see between "entity" and "class". In my point of view, an "entity" is a model class correctly annotated for persistence. Am I missing something here?


                  • #10
                    An entity is a class with the @Entity and other JPA/hibernate mapping annotations without it those are just classes and not entities.

                    Your stacktrace clearly indicates that somewhere it thinks the 'muitobafao.model' is a class so it must be configured somewhere either that or the fully qualified name is wrong.

                    On a side note you shouldn't be using HibernateTemplate and HibernateDaoSupport (as explained in the reference guide) you should be simply using the SessionFactory directly (HibernateTemplate doesn't add anything anymore since about hibernate 3.0.1).


                    • #11
                      Hey Marten, good morning!

                      Ok ... so I got the right difference from classes to entities since the beginning. Good news!

                      The fact is that my classes in 'muitobafao.model' are completely annotated (they are, in fact, entities). I pasted as an example class (as it is considerably -- 85% -- smaller than so you can see how my classes are annotated. They all follow the same annotation style (I chose to annotate the fields).

                      Why do you say I shouldn't be using HibernateTemplate? The book says exactly the opposite... because when I use HibernateDaoSupport it simplifies my code (and as a side effect makes it more dependable upon Spring).
                      Would it be better for me to use SessionFactory directly and pass it as a constructor argument to my DAO classes?

                      If I got it right ... you are saying I should proceed the way taught by this tutorial. Is it?

                      Thanks again.



                      Well, I tried following that tutorial I posted above but when manually creating the SessionFactory (using the exact same piece of code provided in the website) the same exception happened:

                      Initial SessionFactory creation org.objectweb.asm.ClassWriter.<init>(I)V
                      Exception in thread "main" java.lang.ExceptionInInitializerError
                      	at muitobafao.util.HibernateUtil.<clinit>(
                      	at muitobafao.util.Example1.main(
                      Caused by: java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V
                      	at net.sf.cglib.core.DebuggingClassWriter.<init>(
                      	at net.sf.cglib.core.DefaultGeneratorStrategy.getClassWriter(
                      	at net.sf.cglib.core.DefaultGeneratorStrategy.generate(
                      	at net.sf.cglib.core.AbstractClassGenerator.create(
                      	at net.sf.cglib.core.KeyFactory$Generator.create(
                      	at net.sf.cglib.core.KeyFactory.create(
                      	at net.sf.cglib.core.KeyFactory.create(
                      	at net.sf.cglib.core.KeyFactory.create(
                      	at net.sf.cglib.proxy.Enhancer.<clinit>(
                      	at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxyFactory(
                      	at org.hibernate.proxy.pojo.cglib.CGLIBProxyFactory.postInstantiate(
                      	at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(
                      	at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(
                      	at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(
                      	at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(
                      	at org.hibernate.tuple.entity.EntityMetamodel.<init>(
                      	at org.hibernate.persister.entity.AbstractEntityPersister.<init>(
                      	at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(
                      	at org.hibernate.persister.PersisterFactory.createClassPersister(
                      	at org.hibernate.impl.SessionFactoryImpl.<init>(
                      	at org.hibernate.cfg.Configuration.buildSessionFactory(
                      	at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(
                      	at muitobafao.util.HibernateUtil.<clinit>(
                      	... 1 more
                      Java Result: 1
                      I have already changed the libraries. And it didn't get fixed.
                      Last edited by toffolo; May 10th, 2012, 07:13 AM.