0

I am using Spring-ORM to add a Employee* object in Table called emp using HibernateTemplate.

But when I'm trying to execute the program I'm getting this error:

Error creating bean with name 'testBean': Unsatisfied dependency expressed through field 'dao'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException:E rror creating bean with name 'empDaoImpl': Unsatisfied dependency expressed through field 'ht'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ht' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is 
org.hibernate.tool.schema.spi.SchemaManagementException: Unable to execute schema management to JDBC target [alter table emp add empno number(10,0) not null]

This is My java bean

@Service
public class TestBean 
{
    @Autowired
    private EmpDao dao;

    public void persistEmp(int empid,String empName,int sal,int deptno)
    {
        Employee e=new Employee();
        e.setEmpid(empid);
        e.setEmpName(empName);
        e.setEmpSalary(sal);
        e.setDeptNum(deptno);
        dao.save(e);

    }
    public void updateEmp(int empid,String empName,int sal,int deptno)
    {
        Employee e=new Employee();
        e.setEmpid(empid);
        e.setEmpName(empName);
        e.setEmpSalary(sal);
        e.setDeptNum(deptno);
        dao.update(e);
    }
    public void deleteEmp(int empid)
    {
        dao.deleteEmployee(empid);
    }
    public void selectEmps()
    {
        List lt=dao.selectEmployees();
        Iterator it=lt.iterator();
        while(it.hasNext())
        {
            Employee e1=(Employee)it.next();
            System.out.println(e1);
        }
    }
}

*This is my Entity class

@Entity
@Table(name="emp")
public class Employee 
{
    @Id
    @Column(name="empno")
    private int empid;
    @Column(name="ename")
    private String empName;
    @Column(name="sal")
    private int empSalary;
    @Column(name="deptno")
    private int deptNum;
    //Setters and Getters
    public String toString()
    {
        return "Employee["+empid+" "+empName+" "+empSalary+" "+deptNum+"]";
    }
}

This is my EmpDaoImpl class

@Repository
@Transactional
public class EmpDaoImpl implements EmpDao 
{   
    @Autowired
    private HibernateTemplate ht;

    public void deleteEmployee(int empid) 
    {
        Employee e=(Employee)ht.get(Employee.class, empid);
        ht.delete(e);
        System.out.println("one Employee object is deleted");
    }

    public List selectEmployees() 
    {
        List empList=ht.find("from Employee e");
        return empList;
    }
    public void save(Employee e) 
    {
        ht.save(e);
        System.out.println("Object is saved "); 
    }
    public void update(Employee e) 
    {
        ht.update(e);
        System.out.println("Object is Updated");    
    }
}

This is my Main class

public class Main 
{
    public static void main(String[] args) 
    {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        TestBean testB=(TestBean)ctx.getBean("testBean");
        testB.persistEmp(7999, "naveen", 55555, 40);
        System.out.println("===============================");
    }
}

This is my applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.pack1"></context:component-scan>
<bean id="ht"   class="org.springframework.orm.hibernate5.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean   id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="ds"></property>
    <property name="annotatedClasses">
        <list>
            <value>com.pack1.entity.Employee</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
            hibernate.show_sql=true
            hibernate.hbm2ddl.auto=update
        </value>
    </property>
</bean>

<bean id="txm"  class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean   id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName"    value="oracle.jdbc.driver.OracleDriver"/>
    <property name="url"                value="jdbc:oracle:thin:@localhost:1521:xe"/>
    <property name="username"           value="system"/>
    <property name="password"           value="tiger"/>
</bean>
<tx:annotation-driven   transaction-manager="txm"/>
</beans>
OAM
  • 105
  • 8
sai
  • 25
  • 6

1 Answers1

0

When hibernate.hbm2ddl.auto is set to update Hibernate won't modify existing table column definitions. So, you can manually alter the column definition or drop the table/column and run your code. In the latter case, hibernate will create the column if it is not present.

Please refer here, here and here for more info on hibernate.hbm2ddl.auto options and how they work.

Ram
  • 1,743
  • 2
  • 18
  • 40
  • i'm trying to insert/cerate a column into the table. Not updating the table – sai Feb 28 '18 at 09:08
  • I said hibernate is trying to alter the table. Looking at the log, hibernate is trying to alter the table to add column empno. I believe the table was created earlier and mapped in hibernate. Can you post emp table definition? – Ram Feb 28 '18 at 09:39