当前位置: 首页 > 知识库问答 >
问题:

hibernate无法保存数据

陈法
2023-03-14
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:property-placeholder location="classpath:application.properties"/>


    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${driverClassName}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${db.userName}"></property>
        <property name="password" value="${db.password}"></property>
    </bean>


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

        <property name="packagesToScan" value="com.gtis"/>
    </bean>


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

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


    <!--<bean id="transactionProxy"
          class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
          abstract="true">
        <property name="transactionManager" ref="transactionManager"></property>
        <property name="transactionAttributes">
            <props>
                <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="modify*">PROPAGATION_REQUIRED,-myException</prop>
                <prop key="del*">PROPAGATION_REQUIRED</prop>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>-->
    <!--<tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="import*" propagation="REQUIRED" />
            <tx:method name="*" propagation="NOT_SUPPORTED" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="serviceOperation" expression="execution(* com.gtis.service.*Service.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
    </aop:config>-->

</beans>

package com.gtis.dao;

import com.gtis.model.Student;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by u on 2016/3/15.
 */
@Repository
public class StudentDao {

    @Resource
    private SessionFactory sessionFactory;

    private Session getSession(){
        return sessionFactory.getCurrentSession();
    }

    public Student query(String id) throws Exception{
        if(StringUtils.isBlank(id)){
            return (Student)getSession().get(Student.class,id);
        }else{
            throw new Exception("id is required");
        }
    }

    public List<Student> queryAll(){
        String hql = "from com.gtis.model.Student";
        Query query = getSession().createQuery(hql);
        return query.list();
    }

    public void save(Student student)throws Exception{
        if(student!=null){
            System.out.println("sessionFactory="+sessionFactory);
            System.out.println("session="+getSession());
//            Session session = getSession();
            getSession().save(student);
//            session.flush();
//            session.clear();
//            session.close();
        }else{
            throw new Exception("object is required");
        }
    }

    public void delete(Student student)throws Exception{
        if(student!=null){
            getSession().delete(student);
        }else{
            throw new Exception("object is required");
        }
    }
}

服务

    package com.gtis.service;

    import com.gtis.dao.StudentDao;
    import com.gtis.model.Student;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.List;

    /**
     * Created by u on 2016/3/15.
     */
    @Service
    public class StudentService {
        @Autowired
        StudentDao studentDao;

        public Student getStudent(String id) throws Exception {
            return studentDao.query(id);
        }

        public List<Student> getAll() throws Exception{
            return studentDao.queryAll();
        }

        @Transactional
        public void save(Student student)throws Exception{
            studentDao.save(student);
        }

        @Transactional
        public void delete(Student student) throws Exception{
            studentDao.delete(student);
        }
    }


> Controller

package com.gtis.controller;

import com.gtis.model.Student;
import com.gtis.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by u on 2016/3/15.
 */
@Controller
public class StudentController {
    @Autowired
    StudentService studentService;

    @RequestMapping("get")
    public String get(){
        Student student = new Student();
        student.setName("avc");
        try {
            studentService.save(student);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "somwhere";
    }
}

共有1个答案

华献
2023-03-14

您的DAO应该实现一个接口,并通过autowired将该接口注入到您的服务,该服务使用@transactional注释来工作。

所以:

@Repository
public class StudentDao implements StudentDaoInterface {
    //your rest code here
}

和:

@Service
public class StudentService {

    @Autowired
    StudentDaoInterface studentDao;

    public Student getStudent(String id) throws Exception {
        return studentDao.query(id);
    }

    public List<Student> getAll() throws Exception{
        return studentDao.queryAll();
    }

    @Transactional
    public void save(Student student)throws Exception{
        studentDao.save(student);
    }

    @Transactional
    public void delete(Student student) throws Exception{
        studentDao.delete(student);
    }
}
 类似资料:
  • 07-24 12:36:23.742: W/System.err(10386):java.io.IO异常:拒绝许可07-24 12:36:23.750: W/System.err(10386): atjava.io.File.createNewFileImpl(本地方法)07-24 12:36:23.750: W/System.err(10386): atjava.io.File.createNe

  • 问题内容: 我正在尝试使用hibernate将数据插入数据库。这是我要执行的动作 但这给我一个错误的说法 线程“主”中的异常org.hibernate.TransientPropertyValueException:非null属性引用了一个瞬态值- 必须在当前操作之前保存瞬态实例 这是我的学生详细信息实体 我的学生详细信息hbm.xml 我的主题实体看起来像 Subject.hbm.xml 这是S

  • 这是使用电子邮件密码注册帐户的所有代码,保存验证电子邮件,将用户数据保存到Firestore数据库中。只有Firestore数据库无法运行。 else { Toast.makeText(register.this, “Error ! ” task.getException().getMessage(), Toast.LENGTH_SHORT).show();progressBar.setVisib

  • 我是Hibernate的新手,并要求使用具有这些列的表的数据库 表:TBL _ product//库存项目列表< br >列:< br > key _ product < br > key _ category < br > fld _ product _ name < br > fld _ Inventory _ qty < br > fld _ unit _ price < br > fld

  • 我想删除所有具有某些lineId的记录,以保存另一个具有相同lineId的记录(作为刷新),但删除后我无法保存任何内容。没有任何错误,但数据库中没有我的记录。 当我没有删除代码时,一切都会正确保存。