2

I'm trying to use hibernate in spring framework. Following are my files :

Employee.java :

package com.springstarter;

    public class Employee
    {
        private int id;

        private String name;

        private float salary;

        public Employee()
        {
        }

        public Employee( int id, String name, float salary )
        {
            super();
            this.id = id;
            this.name = name;
            this.salary = salary;
        }

        public int getId()
        {
            return id;
        }

        public void setId( int id )
        {
            this.id = id;
        }

        public String getName()
        {
            return name;
        }

        public void setName( String name )
        {
            this.name = name;
        }

        public float getSalary()
        {
            return salary;
        }

        public void setSalary( float salary )
        {
            this.salary = salary;
        }

    }

EmployeeDao.java

    package com.springstarter;

    import java.util.ArrayList;
    import java.util.List;

    import org.springframework.orm.hibernate3.HibernateTemplate;

    public class EmployeeDao
    {
        HibernateTemplate template;

        public void setTemplate( HibernateTemplate template )
        {
            this.template = template;
        }

        public void saveEmployee( Employee e )
        {
            template.save( e );
        }

        public void updateEmployee( Employee e )
        {
            template.update( e );
        }

        public void deleteEmployee( Employee e )
        {
            template.delete( e );
        }

        public Employee getById( int id )
        {
            Employee e = ( Employee ) template.get( Employee.class, id );
            return e;
        }

        public List<Employee> getEmployees()
        {
            List<Employee> list = new ArrayList<Employee>();
            list = template.loadAll( Employee.class );
            return list;
        }
    }

Driver class :

package com.springstarter;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringORMApp
{
    public static void main( String[] args )
    {
        ApplicationContext ctx = new ClassPathXmlApplicationContext( "SpringStarter.xml" );
        EmployeeDao dao = ( EmployeeDao ) ctx.getBean( "edao" );
        System.out.println( ( ( Employee ) dao.getById( 1 ) ).getName() );
    }
}

Beans xml file :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@[10.113.49.82]:1521:roc12c" />
        <property name="username" value="milli_sec" />
        <property name="password" value="milli_sec" />
    </bean>
    <bean id="mysessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mappingResources">
            <list>
                <value>employee.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
    <bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="mysessionFactory"></property>
    </bean>

    <bean id="edao" class="com.springstarter.EmployeeDao">
        <property name="template" ref="template"></property>
    </bean>
</beans>  

employee.hbm.xml file

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-mapping PUBLIC  
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.springstarter.Employee" table="employee">
        <id name="id">
            <generator class="assigned"></generator>
        </id>
        <property name="name"></property>
        <property name="salary"></property>
    </class>
</hibernate-mapping>  

enter image description here

No compilation issues.But expecting the employee object of id 1. The following are the logs.

INFO: HHH000397: Using ASTQueryTranslatorFactory
Jan 28, 2016 4:28:15 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
Jan 28, 2016 4:28:15 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
Jan 28, 2016 4:28:16 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
Jan 28, 2016 4:28:46 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: MILLI_SEC.EMPLOYEE
Jan 28, 2016 4:28:46 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [id, name, salary]
Jan 28, 2016 4:28:46 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
Jan 28, 2016 4:28:46 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [employee_pk]
Jan 28, 2016 4:28:46 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
2
  • Try changing this property "hibernate.hbm2ddl.auto" from update to validate or remove it at all. Also, make sure to catch any exception in your application and log it properly. Commented Jan 28, 2016 at 11:18
  • Pls provide complete log Commented Jan 28, 2016 at 12:58

1 Answer 1

1

I think, the main issue that you don't use any transactions. For reading data with Hibernate you need transactions too. So you need to extend your SpringStarter.xml configuration with a transaction manager. You can use @Transactional annotation or add transactions to methods by methods masks in the SpringStarter.xml.

And there are some problems in your code

org.springframework.orm.hibernate3.HibernateTemplate

should be

org.springframework.orm.hibernate4.HibernateTemplate

public List<Employee> getEmployees()
{
    List<Employee> list = new ArrayList<Employee>();
    list = template.loadAll( Employee.class );
    return list;
}

should be

public List<Employee> getEmployees()
{
    return template.loadAll( Employee.class );
}

and

HibernateTemplate template;

should be

private HibernateTemplate template;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.