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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45120775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:31:03  来源:igfitidea点击:

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

javaspringhibernate

提问by Igor Rybak

I am new to JavaEE and i have been working on a simlpe Springboot project. Each time i run it i get this error:

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

Please feel free to answer to my question and any improvemnet in the code is Highly Appreciate.

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

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.

回答by Igor Rybak

It is possible to get Session from EntityManager which is already configured in spring-boot-starter-data-jpa. So inject EntityManager instead of SessionFactory:

可以从已经在spring-boot-starter-data-jpa. 所以注入 EntityManager 而不是 SessionFactory:

    @Autowired
    private EntityManager entityManager;

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

And use getSession() method where you need.

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

回答by Anshul Sharma

i see you project and i found that there is no SessionFactoryclass or configuration for SessionFactoryinto your project and you are use @Autowiredfor SessionFactoryin CartDaoImplclas. that is the main issue. you need to configure the SessionFactory

我看到你的项目,我发现,没有任何SessionFactory类或配置SessionFactory到你的项目,你使用@AutowiredSessionFactoryCartDaoImplCLAS。这是主要问题。你需要配置SessionFactory

yo can refer bellow example : http://www.devglan.com/spring-boot/spring-boot-hibernate-5-example

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

回答by xyz

  1. you need :
    • Add the SessionFactory bean in the Application class.
    • Add the Current Session Context class in application.properties.
    • Use the SessionFactory using @Autowired annotation.
    • add into application.properties
  1. 你需要 :
    • 在 Application 类中添加 SessionFactory bean。
    • 在 application.properties 中添加 Current Session Context 类。
    • 使用 SessionFactory 使用 @Autowired 批注。
    • 添加到 application.properties

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

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

and add this

并添加这个

spring.datasource.url=......
spring.datasource.username=....
spring.datasource.password=.....
spring.jpa.properties.hibernate.dialect=.......
spring.jpa.hibernate.ddl-auto=update
  1. you use @Transactional , but you didn't configured it.You should also configure it. add @EnableTransactionManagement into config class and config this bean.
  1. 你使用 @Transactional ,但你没有配置它。你也应该配置它。将 @EnableTransactionManagement 添加到配置类中并配置此 bean。

here is good example for configuration A Guide to Hibernate with Spring 4

这是配置Spring 4 休眠指南的好例子



Just remarks : 1# you use in UserServiceImpl for example

只是备注:1# 你在 UserServiceImpl 中使用例如

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

use only @Component or @Service but not both as it's redundant. Service is alread a Component.

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

2# in methods where you have only read operation us instead of default @Transactionalthis @Transactional(readOnly = true)

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

3# in methods like void addCustomerOrder(CustomerOrder customerOrder);better return boolean or some object like CustomerOrder than return just void.

3# 在诸如void addCustomerOrder(CustomerOrder customerOrder);更好地返回布尔值或诸如 CustomerOrder 之类的对象之类的方法中,而不是仅返回 void。

4# class Queries is not Serializable

4# 类查询不可序列化

5# better use lazy as default value than fetch = FetchType.EAGER

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

6# dao class CartDaoImpl has dependency on service class , it's strange.

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

7# in some cases you have transaction on dao level in another on service

7# 在某些情况下,您在另一个服务中进行了 dao 级别的交易

8# if you can create sub package impl and move all implementation into one.

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

you will have com.dao with N interfaces and com.dao.impl with N implementations for them , and not one package com.dao with N+N intercases and classes

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



add this into pom.xml

将此添加到 pom.xml

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

and create package configs com.configsand configure like DatabaseConfig

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

回答by xrom888

Create service and use it to get session from EntityManager:

创建服务并使用它从 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);
    }
}

you can add more methods to reduce amount of written code.

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