0

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

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;
    }
 }    

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

@Repository
public class StudentRepository1 {


    SessionFactory factory = Hibernateutil.getSessionFactory();

    Session currentSession =factory.getCurrentSession();

    //do something
}

应用程序.yml 文件:

# 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

我得到的错误是:

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`

我的项目的依赖项:

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

请帮帮我,我浪费了一周时间尝试许多网站上的一些解决方法,但对我没有任何帮助

4

1 回答 1

0

您的问题是如何创建SessionFactory.

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

如果您的spring-boot应用程序不包含spring-boot-autoconfigure在类路径中,那么您需要在 spring 配置中手动配置必要的EntityManagerFactorySessionFactorybeans;spring 文档再次涵盖了这一点。

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

所以在你的存储库类中,你会这样做

@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课程可以被删除,因为它完全没有必要。

于 2018-06-02T20:48:52.923 回答