【问题标题】:How Can I set sessionFactory properties using application.properties如何使用 application.properties 设置 sessionFactory 属性
【发布时间】:2018-12-16 00:00:26
【问题描述】:

我想连接 H2 - 使用 Spring Boot 嵌入。我想使用会话工厂。我知道如何使用 applicationContext.xml 和 dispatcher-servlet.xml 处理 web.xml

但是当我开始使用 spring initializr 时,我想使用 application.properties 配置将数据库与 SessionFactory 和 Session 连接起来,但它失败了。它给了我:

需要一个找不到的“org.hibernate.SessionFactory”类型的bean。

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp


spring.datasource.url=jdbc:h2:~/test
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password= 

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

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

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.naming.physical-      
strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.h2.console.enabled=true
spring.h2.console.path=/console

我已经下载了需要的hibernate。

我只是搜索 sessionFactory 配置来连接数据库。

我的 pom.xml 是这样的:

  <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>

        </dependency>


        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>

        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>

        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>

        </dependency>

我的 UserDaoImpl:

import com.alpcan.springproject.dao.UserDao;
import com.alpcan.springproject.model.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
@Transactional
public class UserDaoImpl implements UserDao {

    @Autowired
    private SessionFactory sessionFactory;

    public List<User> allUsers(){
        Session session = sessionFactory.getCurrentSession();
         Query query = session.createQuery("from User");
        List<User> users=query.list();
        return users;

    }
}

P.S : 我可以使用 RpaRepository 代替 SessionFactory 吗?会有什么不同?会有什么速度表现?哪个更好?

如果RpaRepository强大,我只想用它。

【问题讨论】:

  • 你添加依赖了吗?
  • 我刚刚在上面添加了我的 pom.xml。我错过了任何依赖项吗?
  • 示例项目如何做到这一点:github.com/mdeinum/samples/tree/master/…
  • 只需使用 EntityManager 而不是 SessionFactory 与 JPA 的当前状态,使用普通休眠几乎没有优势。如果您真的想使用会话,您可以随时使用entityManager.unwrap(Session.class)。另外org.hibernatejpa 依赖已经包含在spring-boot-starter-data-jpa 依赖中,不需要添加它们。

标签: spring hibernate spring-boot h2


【解决方案1】:
  <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>

它真的是你在运行时提供的吗??

将范围更改为compile

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 2014-01-06
    • 2016-08-24
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多