我正在处理休眠,当我打开我当前的项目时,我发现我的 Session-Factory 已被弃用:
AnnotationConfiguration af = new AnnotationConfiguration();
SessionFactory factory = af.configure().buildSessionFactory();
Session session = factory.openSession();
AnnotationConfiguration 现在似乎已被弃用......所以我检查了JavaDoc,并被告知它已移至:
org.hibernate.cfg.Configuration
到目前为止,我的代码运行良好,实际上我不想更改它......但我用谷歌搜索并发现有人问自己同样的问题,为什么 SessionFactory 需要更改...... http://rgordon.co.uk/博客/2012/02/24/hibernate-please-dont-deprecate-yourself/
这篇文章来自 2012 年(所以不是那么旧......)并以这种方式描述了所有内容:
ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder();
serviceRegistryBuilder.applySettings(properties);
ServiceRegistry serviceRegistry = serviceRegistryBuilder.buildServiceRegistry();
Configuration configuration = new Configuration().addClass(FeedTradePersistable.class);
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
我实现了这一点。JavaDoc 再次证明这是错误的!已弃用。它指的是:
org.hibernate.boot.registry.StandardServiceRegistryBuilder
我又用谷歌搜索了一遍。结果不是很满意...
我开始修改代码...
ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
Configuration configuration = new Configuration();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
并抛出异常...
org.hibernate.HibernateException:当未设置“hibernate.dialect”时,连接不能为空
在线:
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
我很确定这是因为我没有指定任何配置设置。其实,我不想。我对 hibernate.cfg.xml 感觉很舒服。
我玩了一下 configuration.addFile(.. - 不是那么成功......
有人对此有任何想法吗?谢谢
更新:(hibernate.cfg.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost\SQLEXPRESS</property>
<property name="hibernate.connection.username">qohelet</property>
<property name="hibernate.connection.password">password</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="bonecp.setIdleMaxAgeInMinutes">240</property>
<property name="bonecp.setIdleConnectionTestPeriodInMinutes">5</property>
<property name="bonecp.partitionCount">3</property>
<property name="bonecp.acquireIncrement">10</property>
<property name="bonecp.maxConnectionsPerPartition">60</property>
<property name="bonecp.minConnectionsPerPartition">20</property>
<property name="bonecp.statementsCacheSize">50</property>
<property name="bonecp.releaseHelperThreads">3</property>
<mapping class="order.Line" />
<mapping class="order.Order" />
<mapping class="order.Group" />
</session-factory>
</hibernate-configuration>
更新(2014 年 2 月 16 日): 我认为也有必要向您展示我的 pom.xml。我花了一段时间才弄清楚 Hibernate-Framework 的哪种组合对我有用……
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.10.Final</version>
<exclusions>
<exclusion>
<artifactId>hibernate-commons-annotations</artifactId>
<groupId>org.hibernate.common</groupId>
</exclusion>
<exclusion>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<groupId>org.hibernate.javax.persistence</groupId>
</exclusion>
<exclusion>
<artifactId>hibernate-commons-annotations</artifactId>
<groupId>org.hibernate</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.8.Final</version>
<exclusions>
<exclusion>
<artifactId>hibernate</artifactId>
<groupId>org.hibernate</groupId>
</exclusion>
<exclusion>
<artifactId>hibernate-annotations</artifactId>
<groupId>org.hibernate</groupId>
</exclusion>
<exclusion>
<artifactId>hibernate-commons-annotations</artifactId>
<groupId>org.hibernate</groupId>
</exclusion>
<exclusion>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<groupId>org.hibernate.javax.persistence</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>1.1.1.Final</version>
<scope>provided</scope>
</dependency>