HibernateTemplate.save()方法未插入DB

时间:2017-08-08 18:27:47

标签: spring

我是Spring和Hibernate的新手。我只是想使用Hibernate Spring Integration在我的表中插入一条新记录。请参阅以下代码

我的beans.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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">

<bean id="mainClient" class="Client.MainClient">
    <property name="contactDetailsDao" ref="contactDetailsBean"></property>
</bean>

<bean id="contactDetailsBean" class="DAO.ContactDetailsDAO">
    <property name="hibernateTemplate" ref="hibernateTemplateBean"></property>
</bean>

<bean id="hibernateTemplateBean" class="org.springframework.orm.hibernate4.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactoryBean"></property>
    <property name="checkWriteOperations" value="false"></property>
</bean>

<bean id="sessionFactoryBean" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSourceBean"></property>
    <property name="mappingResources" value="ContactDetails.xml"></property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="dataSourceBean" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost/SpringLearnDatabase"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
</bean>
</beans>

我的hibernate.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                    "http://www.hibernate.org/dtd/hibernate-mapping">

<hibernate-mapping>
    <class name="ContactDetailsDTO.ContactDetailsDTO" table="contactDetails">
    <id name="contactId" column="id" type="string"></id>
    <property name="name" column="Name" type="string"></property>
    <property name="mobileNumber" column="MobileNumber" type="string"></property>
    <property name="emailId" column="EmailId" type="string"></property>
    </class>
</hibernate-mapping>

我的DAO课程:

package DAO;

import java.util.List;

import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import ContactDetailsDTO.ContactDetailsDTO;

public class ContactDetailsDAO {

    private HibernateTemplate hibernateTemplate;

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    public void insertIntoContact(ContactDetailsDTO cd){
        try{
            hibernateTemplate.save(cd);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public List<ContactDetailsDTO> getAllAccounts(){
        return (List<ContactDetailsDTO>) hibernateTemplate.find("from ContactDetailsDTO");
    }

}

当我尝试getAllAccounts时,我可以看到控制台中的select查询和表中的记录,但是当我尝试在表中插入新记录时,插入查询不会显示在控制台上,并且新记录不会插入表中。

save()方法未插入新记录。请检查我的错误并回复

由于

0 个答案:

没有答案