Announcement Announcement Module
Collapse
No announcement yet.
netbeans + spring + hibernate - help needed Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • netbeans + spring + hibernate - help needed

    Hello everyone,

    before I posted this problem I tried to find its solution, not only at this forum, but all over the internet.

    I'm pretty new to spring technology and I'm trying to create simple console application that connects to mysql database using spring framework and hibernate. I'm using Netbeans 6.5 IDE.

    Simple entity class that represents database table is given below:

    Code:
    package javabeat.spring.hibernate;
    
    public class Employee {
    
        private String id;
        private String name;
        private int age;
        private double salary;
    
        public Employee() {
        }
    
        public String getId(){
            return id;
        }
    
        public void setId(String id){
            this.id = id;
        }
    
        public String getName(){
            return name;
        }
    
        public void setName(String name){
            this.name = name;
        }
    
        public int getAge(){
            return age;
        }
    
        public void setAge(int age){
            this.age = age;
        }
    
        public double getSalary(){
            return salary;
        }
    
        public void setSalary(double salary){
            this.salary = salary;
        }
    
        public String toString(){
            return "Id = " + id + ", Name = " + name + ", Age = "
                + age + ", Salary = " + salary;
        }
    }
    and relevant mapping file:
    Code:
    <hibernate-mapping>
        <class name="javabeat.spring.hibernate.Employee" table="Employee">
        <id name="id" column="Id">
            <generator class="assigned"/>
        </id>
    
        <property name="name">
            <column name="Name"/>
        </property>
        <property name="age">
            <column name="Age"/>
        </property>
        <property name="salary">
            <column name="Salary"/>
        </property>
    </class>
    </hibernate-mapping>
    Simple DAO class:
    Code:
    package javabeat.spring.hibernate;
    
    import java.sql.SQLException;
    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.springframework.orm.hibernate3.HibernateCallback;
    import org.springframework.orm.hibernate3.HibernateTemplate;
    
    public class EmployeeDao {
    
        private HibernateTemplate hibernateTemplate;
    
        public void setHibernateTemplate(HibernateTemplate hibernateTemplate){
            this.hibernateTemplate = hibernateTemplate;
        }
    
        public HibernateTemplate getHibernateTemplate(){
            return hibernateTemplate;
        }
    ...
        public void saveOrUpdate(Employee employee){
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session)
                    throws HibernateException,SQLException {
                    session.saveOrUpdate(employee);
                    return null;
                }
            };
            hibernateTemplate.execute(callback);
        }
    }
    When I try to get EmployeeDao from main program
    Code:
    Resource resource = new ClassPathResource("newSpringXMLConfig.xml");
    BeanFactory factory = new XmlBeanFactory(resource);
    ...
    
    EmployeeDao employeeDao = (EmployeeDao)factory.getBean("employeeDao"); // line #23
    ...
    I get following stack trace:
    Code:
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeDao' defined in class path resource [newSpringXMLConfig.xml]: Cannot resolve reference to bean 'hibernateTemplate' while setting bean property 'hibernateTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in class path resource [newSpringXMLConfig.xml]: Cannot resolve reference to bean 'mySessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in class path resource [newSpringXMLConfig.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V
            at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:274)
            at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1274)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1042)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:485)
            at java.security.AccessController.doPrivileged(Native Method)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
            at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
            at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:169)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:170)
            at javabeat.spring.hibernate.Main.main(Main.java:23)
    Spring configuration is:
    Code:
    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" >
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost/samples"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>
    
    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    	<property name="dataSource" ref="myDataSource" />
    	<property name="mappingResources" >
    		<list>
    			<value>employee.hbm.xml</value>
    		</list>
    	</property>
    	<property name="hibernateProperties" >
    		<props>
    			<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
    		</props>
    	</property>
    </bean>
    
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory">
            <ref bean="mySessionFactory"/>
        </property>
    </bean>
    
    <bean id="employeeDao" class="javabeat.spring.hibernate.EmployeeDao">
        <property name="hibernateTemplate">
            <ref bean="hibernateTemplate"/>
        </property>
    </bean>
    Has anyone faced similar problem? I'd like to here any proposal for solving this one.

  • #2
    Wrong asm library version (2.2.x), but some component need newer one 3.x.

    2.2.x is for spring 2.5.x so you will have hard time if you need both . good luck maybe OSGi can help you.

    Comment


    • #3
      I downloaded the latest spring release (3.0.0.M1) and problem was solved.

      Comment


      • #4
        Spring and Hibernate Integration Example

        Comment


        • #5
          Hai need help

          Hi,


          I am working with the same example .i am using netbeans 6.5 ,spring 2.5 ,and hibernate 3 . can u explain me clearly how u solved this exception. me too getting same exception..


          nested exception is java.lang.NoSuchMethodError:
          ---------------------
          org.objectweb.asm.ClassWriter.<init>



          regards,
          sagar gv.

          Comment


          • #6
            As the earlier post states it's probably a problem with the version of asm you are using. I'd recommend using the JARs that come in the Spring with dependencies download.

            Comment


            • #7
              i have the same problem but at Unit Testing (JUnit) and only on NetBeans.
              My proyect works fine, but if i run the unit test at NetBeans(7.0.1 & 7.1) throws me the Caused by: java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V Error; but if i run the Unit test at eclipse, it works fine o.O (both in Apache Tomcat 7.0.26) wth.

              FYI: im testing with glassfish at NetBeans with the same result as tomcat.

              Comment

              Working...
              X