相关疑难解决方法(0)

Hibernate SessionFactory与EntityManagerFactory

我是Hibernate的新手,不知道是否要使用SessionFactoryEntityManagerFactory获取hibernate会话.两者有什么区别?优点缺点?

java hibernate jpa sessionfactory hibernate-entitymanager

234
推荐指数
5
解决办法
12万
查看次数

如何在Spring启动时自动装配Hibernate SessionFactory

我已经创建了一个spring启动应用程序,我想处理Hibernate SessionFactory,所以在我的服务类中,我可以像下面这样调用Hibernate SessionFactory:

@Autowired
private SessionFactory sessionFactory;
Run Code Online (Sandbox Code Playgroud)

我在stackoverflow中发现了一个类似的问题,我必须在application.properties中添加以下行:

spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
Run Code Online (Sandbox Code Playgroud)

但我收到这个错误:

Cannot resolve property 'current_session_context_class' in java.lang.String
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

pom.xml依赖项:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>
Run Code Online (Sandbox Code Playgroud)

spring hibernate spring-boot

8
推荐指数
2
解决办法
3万
查看次数

如何在 Spring Boot 应用程序中访问 AuditReaderFactory?

我正在使用 spring boot 和 spring 数据 jpa。我也在使用 hibernate envers,我需要访问 AuditReaderFactory 以便我可以编写审计查询。

因为它是一个弹簧引导和弹簧数据 jpa,所以一切都是自动配置的。所以当我这样做时,

@Autowired
AuditReaderFactory auditReaderFactory;
Run Code Online (Sandbox Code Playgroud)

它不起作用。我收到以下错误。

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.envers.AuditReaderFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency
Run Code Online (Sandbox Code Playgroud)

如何在我的存储库类中正确引用 AuditReaderFactory?

java spring-data-jpa spring-boot nhibernate-envers

7
推荐指数
4
解决办法
4565
查看次数

Spring Boot:在单元测试中使用 Hibernate Session

在我的 Spring Boot 应用程序中,我访问我的 Hibernate 会话,如以下答案所示: https: //stackoverflow.com/a/33881946/272180

现在我还想在单元测试中访问 Hibernate Session。

如何在 Spring Boot 应用程序的单元测试中设置数据源并访问 Hibernate Session?

当我简单地自动装配它时,我得到org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread.

自动装配和使用@Service工作完美无缺。

我的单元测试类看起来像这样:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = App.class)
@WebIntegrationTest
public class WebTest {

    @Autowired
    private SessionFactory  sessionFactory;

    @Autowired
    private UserService     userService;

    @Test
    public void testService() {

        final List<User> users = userService.getUsers();

        // do something with users
        // ...

    }
}
Run Code Online (Sandbox Code Playgroud)

App.class指的是具有用于运行 Spring Boot 应用程序的 main 方法的类。

java junit4 spring-boot

4
推荐指数
1
解决办法
5306
查看次数

'xxxDAOImpl中的字段会话需要一个无法找到'org.hibernate.SessionFactory'类型的bean

我收到以下错误(从我相信,Dao层 - 但我可能会读错了).

我现在有一个Spring启动应用程序,创建一个数据库架构.这些表正在正确创建,但是当我尝试添加Dao和DaoImpl文件时,它会崩溃,并显示以下错误消息:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field session in xx.dao.ParkingSpaceDaoImpl 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.
Run Code Online (Sandbox Code Playgroud)

在我的DaoImpl文件中,我有:

@Repository 
public class xxDaoImpl implements xxDao {
    @Autowired
    private SessionFactory session;
Run Code Online (Sandbox Code Playgroud)

以下是我的POM.xml文件的外观:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>xx.xx</groupId>
    <artifactId>xxx</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath/> 
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency> …
Run Code Online (Sandbox Code Playgroud)

hibernate spring-mvc spring-data

2
推荐指数
1
解决办法
8946
查看次数

在 Spring Boot 应用程序中以编程方式重新启动 HikariPool?

我有一个 Spring Boot 应用程序,它使用 Hibernate 和 HikariDataSource / HikariPool 与数据库通信。

应用程序中的一个特殊功能会触发数据库重新启动。目前这会破坏 HikariPool 中的连接:

Caused by: org.postgresql.util.PSQLException: ERROR: relation "relation_which_really_exists" does not exist
Position: 113
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2532)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2267)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:312)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:448)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:369)
Run Code Online (Sandbox Code Playgroud)

旧版本的应用程序确实以编程方式调用org.hibernate.SessionFactory.close();,这会导致 HikariDataSource / HikariCP 重新启动:

2020-08-17T11:36:42.628Z [qtp1340328248-76] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown initiated...
2020-08-17T11:36:42.698Z [qtp1340328248-76] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown completed.
2020-08-17T11:36:51.266Z [qtp1340328248-12] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-2 - Starting...
2020-08-17T11:36:51.515Z [qtp1340328248-12] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-2 - Start completed.
Run Code Online (Sandbox Code Playgroud)

我想做同样的事情,但是如何以编程方式重新启动应用程序中的连接池?我见过像Spring Boot - …

java spring hibernate spring-boot hikaricp

1
推荐指数
1
解决办法
7277
查看次数