首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >即使在属性中设置了'hibernate.dialect‘,也没有设置它

即使在属性中设置了'hibernate.dialect‘,也没有设置它
EN

Stack Overflow用户
提问于 2018-06-01 17:45:23
回答 1查看 505关注 0票数 0

我是hibernate的新手,创建了一个简单的hibernate应用程序,其中我试图创建一个会话工厂对象,我的代码如下所示:

代码语言:javascript
复制
public class Hibernateutil {

    private static final SessionFactory sessionFactory;
    static {
        try {
            sessionFactory = new Configuration()
                    .buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
 }    

这就是我尝试从工厂获取会话并尝试保存/提交实体的地方

代码语言:javascript
复制
@Repository
public class StudentRepository1 {


    SessionFactory factory = Hibernateutil.getSessionFactory();

    Session currentSession =factory.getCurrentSession();

    //do something
}

application.yml文件:

代码语言:javascript
复制
# Details for our datasource
spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/postgres
    username: postgres
    password: postgres
    driver-class-name: org.postgresql.Driver
# Hibernate properties
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQL95Dialect
      #current_session_context_class: thread
    show-sql: false
    hibernate.ddl-auto: 
#application properties
  application:
    name: hibernate_demo
server:
  port: 10091

我得到的错误是:

代码语言:javascript
复制
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:100) ~[hibernate-core-5.3.1.Final.jar:5.3.1.Final]
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54) ~[hibernate-core-5.3.1.Final.jar:5.3.1.Final]
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137) ~[hibernate-core-5.3.1.Final.jar:5.3.1.Final]
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) ~[hibernate-core-5.3.1.Final.jar:5.3.1.Final]
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:94) ~[hibernate-core-5.3.1.Final.jar:5.3.1.Final]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263) ~[hibernate-core-5.3.1.Final.jar:5.3.1.Final]
    ... 39 common frames omitted`

我的项目的依赖关系:

代码语言:javascript
复制
dependencies {
    //compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '1.5.6.RELEASE'
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.3.1.Final'
    compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.2.Final'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.9.RELEASE'
    compile group: 'org.postgresql', name: 'postgresql', version: '42.2.2'

}

`

即时通信工具使用postgres 9.5

请帮帮我,我已经浪费了一周的时间尝试了许多网站上的一些变通方法,但都没有对我起作用。

EN

回答 1

Stack Overflow用户

发布于 2018-06-03 04:48:53

您的问题在于如何创建SessionFactory

如果您的spring-boot应用程序在类路径上有spring-boot-autoconfigure,那么您需要确保在类路径上有Hibernate和所有需要的依赖项,以便它能够自动创建EntityManagerFactorySessionFactory bean。这都是由spring-boot-autoconfigure处理的,如果您需要自定义默认行为,请参阅其文档。

如果您的spring-boot应用程序没有在类路径中包含spring-boot-autoconfigure,那么您将需要在spring配置中手动配置必要的EntityManagerFactorySessionFactory bean;同样,spring文档对此进行了广泛的介绍。

在基于spring的应用程序中,这里确实不需要HibernateUtil类,这是问题的症结所在。Spring的依赖注入可以根据需要为您的类提供适当的EntityManagerSessionFactory实例,尽管我在这里通常建议使用JPA。

因此,在您的存储库类中,您可以这样做

代码语言:javascript
复制
@Repository
public class MyFancyRepositoryImpl implements MyFancyRepository {
  @PersistenceContext
  private EntityManager entityManager;

  public void doSomethingCool(TheEntity entity) {
    entityManager.persist( entity );

    // should you need access to the raw Hibernate session, unwrap 
    final Session session = entityManager.unwrap( Session.class );
    // now you can use the `Session` as needed/
  }
}

这意味着您的HibernateUtil类可以删除,因为它完全不必要。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50640625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档