繁体   English   中英

Hibernate Bean创建异常

[英]Hibernate Bean Creating Exception

我正在使用Spring-ORM使用HibernateTemplate在名为emp表中添加Employee*对象。

但是,当我尝试执行程序时,出现此错误:

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]

这是我的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);
        }
    }
}

*这是我的实体课

@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+"]";
    }
}

这是我的EmpDaoImpl

@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");    
    }
}

这是我的主班

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("===============================");
    }
}

这是我的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>

hibernate.hbm2ddl.auto设置为update Hibernate将不会修改现有的表列定义。 因此,您可以手动更改列定义或删除表/列并运行代码。 在后一种情况下,如果该列不存在,则hibernate将创建该列。

在此处此处此处参考以获取有关hibernate.hbm2ddl.auto选项及其工作方式的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM