Springboot集成hibernate+HibernateDaoSupport 5C
package com.ruoyi.project.common.base.dao;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.dto.BaseDtoImpl;
import com.ruoyi.common.dto.IBaseDto;
import com.ruoyi.common.utils.SystemExtUtils;
import com.ruoyi.common.utils.json.Json;
@Transactional
@Repository("baseDao")
public class BaseDaoImpl extends HibernateDaoSupport implements IBaseDao {
    @Autowired
    public void setSuperSessionFactory(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }
//    @Autowired
//    private EntityManagerFactory entityManagerFactory;
//    public Session getSession() {
//        return entityManagerFactory.unwrap(SessionFactory.class).openSession();
//    }
    @Override
    public <T> void saveOrUpdateNow(T clazz){
        Session session=this.getSession();
        session.merge(clazz); 
         //立即写入数据库并释放内存 
        session.flush(); 
        session.clear(); 
    }
    @Override
    @Transactional
    public <T> void saveOrUpdate(T clazz){
        Session session=this.getSession();
        //方法三
        session.beginTransaction();
        session.saveOrUpdate(clazz);


        Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

图片说明

Maven工程,是网上若依的,但是我们这边用是Hibernate,所以想改成Hibernate的,但是真心问题多
我们还Hibernate这里继承HibernateDaoSupport来实现,出现如上错误

不写方法
@Autowired
public void setSuperSessionFactory(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
的时候错误出下
Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
用Hibernate的人少了,人熟悉这块的吗

3个回答

Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
HibernateDaoSupport不能注入sessionFactiry和hibernateTemplemet
https://www.cnblogs.com/weilengdeyu/p/5595610.html

1、@Autowired改成@Resource
2、spring配置中加上

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory></property>
      </bean>
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!