HibernateTemplate save()不会使用spring将数据插入数据库

时间:2017-09-02 17:02:25

标签: spring hibernate

我试过但找不到合适的答案你能不能帮帮我吧? 表正在创建,但数据未插入。我修复了上述错误,但数据未插入db for Hibernatetemplate save()。

2 个答案:

答案 0 :(得分:0)

@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
public class StudentDaoImplHT implements StudentDao {

    private HibernateTemplate ht;
    public void setHt(HibernateTemplate ht) {
        this.ht = ht;
    }
     @Transactional
    @Override
    public int save(Student st) {
    int i=(Integer) ht.save(st);;


    System.out.println(st.getId());
        System.out.println(st.getAddress());
        System.out.println(st.getEmail());
        System.out.println(st.getName());



            return i;   }
    @Override
    public boolean update(Student st) {
        ht.saveOrUpdate(st);
        return true;
    }
    @Override
    public boolean delete(Student st) {
        //ht.delete(st);
        return true;    
}
@Override
    public Student findByPK(int id ) {
    Student std=(Student) ht.get(Student.class, id);
    return std;
    }
@Override
    public List<Student> findByAllUsingHQL() {
        List<Student> list=(List<Student>) ht.find("from Student");
        return list;
    }
    @Override
    public List<Student> findByAllUsingCriteria() {
        DetachedCriteria dc=DetachedCriteria.forClass(Student.class);
        //dc.setProjection(projection);
        //ht.findByCriteria(criteria)
        List<Student> list= (List<Student>) ht.findByCriteria(dc);
        return null;
    }
}

答案 1 :(得分:0)

请确保您的hibernate.cfg.xml中具有以下属性:

"hibernate.connection.autocommit" true"

property name="hibernate.connection.autocommit" true property

希望有帮助。