qq_32042227
-翼Y-
2017-07-25 02:30

关于hibernate只能查询不能增加修改删除数据的问题

  • spring
  • hibernate
  • 事务

spring事务配置如下

     <!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <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>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.connection.autocommit">true</prop>
            </props>
        </property>
        <property name="annotatedClasses">
            <list>
                <value>com.sunsharing.model.User</value>
            </list>
        </property>
    </bean>
    <!-- 自动扫描配置 -->
    <context:annotation-config />
    <context:component-scan base-package="com.sunsharing.dao"/>
    <!-- 支持  @Transactional 标记 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <!-- 定义事务管理器 -->
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

dao类如下

   @Resource(name="sessionFactory")
    private void setSessionFacoty(SessionFactory sessionFacoty){
        super.setSessionFactory(sessionFacoty);
    }
        public void addUser(User user) {
        getHibernateTemplate().getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);
        this.getHibernateTemplate().save(user);
    }
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答