繁体   English   中英

考虑在您的配置中定义类型为“org.hibernate.SessionFactory”的 bean

[英]Consider defining a bean of type 'org.hibernate.SessionFactory' in your configuration

我是 JavaEE 的新手,我一直在从事一个简单的 Springboot 项目。 每次我运行它时,我都会收到此错误:

请随时回答我的问题,非常感谢代码中的任何改进。

Field sessionFactory in com.example.dao.CartDaoImpl required a bean of type 'org.hibernate.SessionFactory' that could not be found.

Action:

Consider defining a bean of type 'org.hibernate.SessionFactory' in your configuration.

可以从已经在spring-boot-starter-data-jpa中配置的 EntityManager 获取会话。 所以注入 EntityManager 而不是 SessionFactory:

    @Autowired
    private EntityManager entityManager;

    private Session getSession() {
        return entityManager.unwrap(Session.class);
    }

并在需要的地方使用 getSession() 方法。

  1. 你需要:
    • 在 Application 类中添加 SessionFactory bean。
    • 在 application.properties 中添加 Current Session Context 类。
    • 使用带有 @Autowired 注释的 SessionFactory。
    • 添加到 application.properties

spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext

并添加这个

spring.datasource.url=......
spring.datasource.username=....
spring.datasource.password=.....
spring.jpa.properties.hibernate.dialect=.......
spring.jpa.hibernate.ddl-auto=update
  1. 你使用了@Transactional,但你没有配置它。你也应该配置它。 将 @EnableTransactionManagement 添加到配置类中并配置此 bean。

这是配置A Guide to Hibernate with Spring 4的好例子


只需备注:1# 例如,您在 UserServiceImpl 中使用

@Component
@Service
public class UserServiceImpl implements UserService {...
  ....
}

仅使用 @Component 或 @Service 但不能同时使用,因为它是多余的。 服务已经是一个组件。

2# 在你只读取操作 us 而不是默认@Transactional this @Transactional(readOnly = true)的方法中

3# 在类似void addCustomerOrder(CustomerOrder customerOrder); 返回布尔值或某些对象(如 CustomerOrder)比返回 void 更好。

4# 类查询不可序列化

5# 比 fetch = FetchType.EAGER 更好地使用 lazy 作为默认值

6# dao 类 CartDaoImpl 依赖服务类,很奇怪。

7# 在某些情况下,您在另一个服务中有 dao 级别的事务

8# 如果您可以创建子包 impl 并将所有实现合并为一个。

您将拥有带有 N 个接口的 com.dao 和带有 N 个实现的 com.dao.impl,而不是一个带有 N+N 接口和类的包 com.dao


将此添加到 pom.xml

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

并创建包配置com.configs并像DatabaseConfig一样配置

我看到你的项目,我发现你的项目中没有SessionFactory类或SessionFactory配置,你在CartDaoImpl类中使用@Autowired作为SessionFactory 这是主要问题。 你需要配置SessionFactory

你可以参考下面的例子:http: //www.devglan.com/spring-boot/spring-boot-hibernate-5-example

创建服务并使用它从 EntityManager 获取会话:

@Service
public class HibernateService {

    @PersistenceContext
    private EntityManager entityManager;

    public <R> NativeQuery<R> createNativeQuery(String sqlString, Class<R> resultClass) {
        return getSession().createNativeQuery(sqlString, resultClass);
    }

    public NativeQuery createNativeQuery(String sqlString) {
        return getSession().createNativeQuery(sqlString);
    }

    public Session getSession() {
        return entityManager.unwrap(Session.class);
    }
}

您可以添加更多方法来减少编写的代码量。

使用 Hibernate 5 简单检查以下内容

-- 检查基础包是否扫描(HibernateConfig.java)

-- 检查文件@Configuration @EnableTransactionManagement 中的有效注解

-- 检查是否创建了所有有效 beans LocalSessionFactoryBean,HibernateTransactionManager,HibernateTemplate

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM