save方法无法使用Hibernate + Spring保存对象

时间:2017-03-17 05:27:08

标签: java spring hibernate

我的Dao图层有一个保存方法:

<bean id="oracleDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value>
    </property>
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:{mylocalInstance}" />
    <property name="username">
        <value>PersonDataBase</value>
    </property>
    <property name="password">
        <value>person</value>
    </property>
</bean>

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="oracleDataSource"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>

        </props>
    </property>
    <property name="mappingLocations" value="PersonBean.hbm.xml" />
</bean>

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

<!--<tx:annotation-driven transaction-manager="testTransactional"/>-->

<bean id="personDao" class="com.dao.PersonDaoImpl">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="personService" class="com.service.PersonServiceImpl">
    <property name="personDao" ref="personDao"/>
</bean>

applicationContext.xml定义为:

INFO: Using DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@68be8808] of Hibernate SessionFactory for HibernateTransactionManager
Opened Session
Hibernate: 
select
    max(PERSON_ID) 
from
    PERSON_BEAN
Data Saved

它能够创建表但是数据没有保存,因为我必须显示sql,这是尝试保存时生成的sql:

def self.ransackable_attributes(auth_object = nil)
 if auth_object == 'admin'
  super
 else
  #mention all the attributes u wish to search
  super & ['assigned_to'] 
 end
end

为什么在我尝试保存时会生成选择查询。

2 个答案:

答案 0 :(得分:2)

您还需要提交交易。

试试这个:

public void savePerson(PersonBean personBean) {
    Session currentSession;

    try {
        currentSession = sessionFactory.getCurrentSession(); 

    } catch (HibernateException e) {
        currentSession = sessionFactory.openSession();
        System.out.println("Opened Session");
    }

    currentSession.beginTransaction(); 
    currentSession.merge(personBean);
    currentSession.getTransaction().commit()
    System.out.println("Data Saved");
}

修改

如果您不想手动处理事务,也可以在Hibernate配置中将hibernate.connection.autocommit属性设置为true。

<property name="hibernate.connection.autocommit">true</property>   

答案 1 :(得分:0)

尝试 currentSession.save(personBean) 如果你正确配置了Spring,那么你就不需要

beginTransaction() Spring将处理该事务。

public void savePerson(PersonBean personBean){     Session currentSession;

try {
    currentSession = sessionFactory.getCurrentSession(); 

} catch (HibernateException e) {
    currentSession = sessionFactory.openSession();
    System.out.println("Opened Session");
}

currentSession.beginTransaction(); 
currentSession.save(personBean);
currentSession.getTransaction().commit()
System.out.println("Data Saved");

}