Announcement Announcement Module
Collapse
No announcement yet.
SessionFactory configuration problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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 App.java (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 App.java 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? :/

    Thanks!
    Last edited by toffolo; May 9th, 2012, 09: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.

    Comment


    • #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 3.2.5.ga 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?

      Regards,

      Comment


      • #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.

        Comment


        • #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.
          Attachment

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

          Comment


          • #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 .

            Comment


            • #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 http://pastebin.com/PfbCHzbD. But you can see in the last lines that my Session Factory is still a problem.

              Comment


              • #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.

                Comment


                • #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:
                  Code:
                  <bean id="sessionFactory"
                  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
                     <property name="dataSource" ref="dataSource" />
                     <property name="annotatedClasses">
                        <list>
                           <value>com.roadrantz.domain.Rant</value>
                           <value>com.roadrantz.domain.Motorist</value>
                           <value>com.roadrantz.domain.Vehicle</value>
                        </list>
                     </property>
                     <property name="hibernateProperties">
                        <props>
                           <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                        </props>
                     </property>
                  </bean>
                  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
                  Code:
                  <bean id="rantDao" class="com.roadrantz.dao.hibernate.HibernateRantDao">
                     <property name="sessionFactory" ref="sessionFactory" />
                  </bean>
                  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?

                  Comment


                  • #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).

                    Comment


                    • #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 Comentario.java as an example class (as it is considerably -- 85% -- smaller than Usuario.java) 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.



                      ------------------

                      EDIT:

                      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:

                      Code:
                      Initial SessionFactory creation failed.java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V
                      Exception in thread "main" java.lang.ExceptionInInitializerError
                      	at muitobafao.util.HibernateUtil.<clinit>(HibernateUtil.java:21)
                      	at muitobafao.util.Example1.main(Example1.java:21)
                      Caused by: java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V
                      	at net.sf.cglib.core.DebuggingClassWriter.<init>(DebuggingClassWriter.java:47)
                      	at net.sf.cglib.core.DefaultGeneratorStrategy.getClassWriter(DefaultGeneratorStrategy.java:30)
                      	at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:24)
                      	at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
                      	at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:144)
                      	at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:116)
                      	at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
                      	at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
                      	at net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69)
                      	at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyInitializer.java:117)
                      	at org.hibernate.proxy.pojo.cglib.CGLIBProxyFactory.postInstantiate(CGLIBProxyFactory.java:43)
                      	at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:162)
                      	at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:135)
                      	at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:55)
                      	at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:56)
                      	at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:295)
                      	at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:434)
                      	at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:109)
                      	at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
                      	at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:226)
                      	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
                      	at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
                      	at muitobafao.util.HibernateUtil.<clinit>(HibernateUtil.java:17)
                      	... 1 more
                      Java Result: 1
                      I have already changed the libraries. And it didn't get fixed.
                      Last edited by toffolo; May 10th, 2012, 08:13 AM.

                      Comment

                      Working...
                      X