没有配置文件的Hibernate配置

时间:2015-02-13 12:06:21

标签: java hibernate

以下是我试图获取hibernate SessionFactory的方法。这是通过 一个简单的Java应用程序所有配置都已添加到属性中。

private SessionFactory buildSessionFactory() {
        Configuration configuration = new Configuration();
        configuration.setProperties(properties);
        StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        return sessionFactory;
    }

但后来我得到了例外

eb 13, 2015 2:03:47 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Feb 13, 2015 2:03:47 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.7.Final}
Feb 13, 2015 2:03:47 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Feb 13, 2015 2:03:47 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Feb 13, 2015 2:03:47 PM org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
Exception in thread "main" 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:104)
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:209)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)

我的属性如下

final Properties properties = new Properties();
properties.put("connection.url", TEST_CONNECTION_URL);
properties.put("connection.username", TEST_USERNAME);
properties.put("connection.password", TEST_PASSWORD);
properties.put("connection.driver_class", "org.postgresql.Driver");
properties.put("dialect", "org.hibernate.dialect.PostgreSQLDialect");
properties.put("show_sql", "true");
properties.put("connection.pool_size", "1");
properties.put("current_session_context_class", "thread");

2 个答案:

答案 0 :(得分:2)

您应该使用此source

Configuration cfg = new Configuration()
    .addClass(org.hibernate.auction.Item.class)
    .addClass(org.hibernate.auction.Bid.class)
    .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect")
    .setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/test")
    .setProperty("hibernate.order_updates", "true");

答案 1 :(得分:1)

我没有在简单的java中配置属性,但是当我查看我的hibernate.properties文件时,它被定义为hibernate.dialect=respectiveDialect。你发布的定义hibernate.dialect的错误也是null,所以可能它应该是

properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");