java - 没有配置文件的 Hibernate 配置

标签 java hibernate

这是我尝试获取 hibernate session 工厂的方法。这称为通过 一个简单的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;
    }

但后来我得到了异常(exception)

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");

最佳答案

你应该使用这个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");

关于java - 没有配置文件的 Hibernate 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28499271/

相关文章:

java - Java中的反向HashMap键和值

hibernate - 使用 Hibernate Criteria api 检索嵌入式或组件

hibernate - 如何在序列化过程中限制hibernate相关对象

java - 无法删除由于 Selenium Grid 创建的 %temp% 文件

java - 使用 java 绘制 ASCII 艺术

java - SSH认证失败: Java JSCH

java - Hibernate 产生我不想要也不需要的唯一约束

java - hibernate 域模型

java - Hibernate中什么时候调用flush()和commit()?

java - 无法理解以下程序的行为(使用 java 集合)