无法使用 spring 和休眠集成自动创建 table 和插入记录

Unable to create a table automatically and insert records using spring and hibernate integration

我是 spring 的新手,正在尝试将休眠与 spring 集成。 我正在使用 MySql 数据库并尝试使用 saveOrUpdate() 方法插入记录。

所以程序正在触发 sql 查询以创建 table(它应该如此)。

但问题是,即使它正在触发对 'create table' 的查询,但并未在数据库中创建 table。 另外,如果 table 是在数据库中手动创建的,然后尝试插入记录,它什么也不做。

我试过使用 save() 和 persist() 方法而不是 saveOrUpdate(),但我一无所获。

这是主要的class。 bean 的值 class(Employee) 已设置以插入记录。

public static void main( String[] args )
{
    ApplicationContext context=new 
    ClassPathXmlApplicationContext("sphb.xml");
    EmployeeDAO edao=(EmployeeDAO) context.getBean("d");
    Employee e=new Employee();
    e.setId(1);
    e.setName("sourav");
    e.setSalary(100000);
    edao.saveEmployee(e);
}

这是豆子 class:- public class 员工 { 私有 int id; 私有字符串名称; 私人薪水; //吸气剂和吸气剂 }

这是包含所有配置的 xml 文件。

<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.apache.commons.dbcp.BasicDataSource">  
    <property name="driverClassName"  value="com.mysql.cj.jdbc.Driver"> 
    </property>  
    <property name="url" value="jdbc:mysql://localhost:3306/db"></property>  
    <property name="username" value="root"></property>  
    <property name="password" value="1234"></property>  
    </bean>

    <bean id="mysessionFactory" 
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"></property>  

    <property name="mappingResources">  
    <list>  
    <value>mapping.hbm.xml</value>  
    </list>  
    </property>  

    <property name="hibernateProperties">  
        <props>  
            <prop 
            key 
            ="hibernate.dialect">org.hibernate.dialect.MySQLDialect</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.hibernate5.HibernateTemplate">  
    <property name="sessionFactory" ref="mysessionFactory"></property>  
    <property name="checkWriteOperations" value="false"></property>
    </bean>  

    <bean id="d" class="demo.sphbIntegrate.EmployeeDAO">  
    <property name="template" ref="template"></property>  
    </bean>

    </beans>

这是 DAO class:-

public class EmployeeDAO 
{
    HibernateTemplate template;

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

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

未在数据库中手动创建table时的输出:-

log4j:WARN No appenders could be found for logger 
(org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for 
more info.
**Hibernate: create table employee (id integer not null, name varchar(255), 
salary integer, primary key (id)) type=MyISAM
Hibernate: select employee_.id, employee_.name as name2_0_, 
employee_.salary as salary3_0_ from employee employee_ where employee_.id=?
Exception in thread "main" 
org.springframework.dao.InvalidDataAccessResourceUsageException: could not 
extract ResultSet; SQL [n/a]; nested exception is 
org.hibernate.exception.SQLGrammarException: could not extract ResultSet**
at org. springframework. orm. hibernate5. SessionFactoryUtils. 
convertHibernateAccessException (SessionFactoryUtils.java:230)
at org. springframework. orm. hibernate5. HibernateTemplate. doExecute 
(HibernateTemplate.java:387)
and 15 more...

手动创建table时的输出:-

log4j:WARN No appenders could be found for logger 
(org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for 
more info.
**Hibernate: select employee_.id, employee_.name as name2_0_, 
employee_.salary as salary3_0_ from employee employee_ where employee_.id=?**

只执行这个 select 查询,而不是插入一个。

通过添加一个使用注释来管理事务的方法使其工作

 <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
       <property name="sessionFactory" ref="mysessionFactory"></property>
      </bean>
    <tx:annotation-driven transaction-manager="txManager"/>

还为sessionFactory添加了setter并为transaction使用了annotation并赋予了写操作的权限。

public class EmployeeDAO 
{
    HibernateTemplate template;

    public void setTemplate(HibernateTemplate template) {
        this.template = template;
    }
    public void setSessionFactory(SessionFactory sessionFactory)
    {
 this.template=new HibernateTemplate(sessionFactory);
    }

    @Transactional(readOnly=false)
    public void saveEmployee(Employee e) 
    {
 template.saveOrUpdate(e);
    }
}