寻找一个不被弃用的会话工厂 [英] Looking for a not-deprecated session-factory
问题描述
AnnotationConfiguration 我正在处理hibernate,当我打开当前项目时我发现我的Session-Factory已被弃用。 af = new AnnotationConfiguration();
SessionFactory factory = af.configure()。buildSessionFactory();
会话会话= factory.openSession();
AnnotationConfiguration现在似乎已被废弃......所以我查了JavaDoc,至:
org.hibernate.cfg.Configuration
我的代码到目前为止工作正常,实际上我不想改变它......但是我搜索了一下,发现有人在问自己同样的问题,为什么SessionFactory需要更改...
http://rgordon.co.uk/blog/ 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
我再次使用了Google。结果并不令人满意......
我开始修改代码...
ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
配置配置=新配置();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
引发异常...
< blockquote>
org.hibernate.HibernateException:当
'hibernate.dialect'未设置时,连接不能为空
at line:
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
我确定这是因为我没有指定任何配置设置。其实我不想。我对hibernate.cfg.xml感到满意。
我使用了配置文件.addFile(.. - 没有那么成功......
有没有人对此有所了解?
谢谢
更新:(hibernate.cfg.xml )
<?xml version =1.0encoding =UTF-8?>
<!DOCTYPE hibernate-configuration PUBLIC - // Hibernate / Hibernate Configuration DTD 3.0 // ENhttp://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>密码< / property> ;
< property name =current_session_context_class>线程< / 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>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-core< / artifactId>
< version> 3.6.10.Final< / version>
<排除项>
<排除>
< artifactId> hibernate-commons-annotations< / artifactId>
< groupId> org.hibernate.common< / groupId>
< /排除>
<排除>
< artifactId> hibernate-jpa-2.1-api< / artifactId>
< groupId> org.hibernate.javax.persistence< / groupId>
< /排除>
<排除>
< artifactId> hibernate-commons-annotations< / artifactId>
< groupId> org.hibernate< / groupId>
< /排除>
< /排除>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-entitymanager< / artifactId>
< version> 4.1.8.Final< / version>
<排除项>
<排除>
< artifactId> hibernate< / artifactId>
< groupId> org.hibernate< / groupId>
< /排除>
<排除>
< artifactId> hibernate-annotations< / artifactId>
< groupId> org.hibernate< / groupId>
< /排除>
<排除>
< artifactId> hibernate-commons-annotations< / artifactId>
< groupId> org.hibernate< / groupId>
< /排除>
<排除>
< artifactId> hibernate-jpa-2.1-api< / artifactId>
< groupId> org.hibernate.javax.persistence< / groupId>
< /排除>
< /排除>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-commons-annotations< / artifactId>
< version> 3.2.0.Final< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate.javax.persistence< / groupId>
< artifactId> hibernate-jpa-2.0-api< / artifactId>
< version> 1.0.1.Final< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-jpamodelgen< / artifactId>
< version> 1.1.1.Final< / version>
< scope>提供< / scope>
< /依赖关系>
为此付出一些努力并进行了一些研究:
http://www.codejava.net/frameworks/hibernate/building-hibernate-sessionfactory-from-service-registry
提供了一个现代 HibernateUtil
:
import org.hibernate.SessionFactory;
导入org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateUtil {
private static SessionFactory sessionFactory;
$ b $ public static SessionFactory getSessionFactory(){
if(sessionFactory == null){
Configuration configuration = new Configuration()。configure();
ServiceRegistryBuilder registry = new ServiceRegistryBuilder();
registry.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = registry.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
return sessionFactory;
}
}
尽管此版本似乎有效还有:
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.SessionFactory;
导入org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
Configuration cfg = new Configuration();
sessionFactory = cfg.configure(hibernate.cfg.xml)。buildSessionFactory();
} catch(Throwable ex){
Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE,null,ex);
抛出新的ExceptionInInitializerError(ex);
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
但我的问题是我不想将新版本与旧版本集成在一起。更新后,我遇到了一个
java.lang.NoSuchMethodError:
org.hibernate.annotations.common.reflection.java。通常,JavaReflectionManager.injectClassLoaderDelegate(Lorg / hibernate / annotations / common / reflection / ClassLoaderDelegate;)V
。
烦人。通常 Mkyong 提供了很好的解决方案,但在我的情况下,他写了 Stackoverflow 的相反解决方案。 。
因此,我搜索了一些存储库,发现了一个令人震惊的简单解决方案(比较: http://hibernate.org/orm/downloads/ ):
< ;! - HIBERNATE - >
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-core< / artifactId>
< version> 5.0.0.Final< / version>
< /依赖关系>
一些其他小问题阻止了我:
在 hibernate .cfg.xml
我必须将行从 update
更改为 auto
: p>
< property name =hbm2ddl.auto> auto< / property>
将它与我旧的pom.xml进行比较... - 当时我有第一次遭遇与Hibernate并添加了一切似乎有用,直到它的工作。在这样做之后,我不再接触它......几乎两年......永远不要改变一个胜利的团队,对吧?
<依赖性>
< groupId> org.hibernate.common< / groupId>
< artifactId> hibernate-commons-annotations< / artifactId>
< version> 4.0.2.Final< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-core< / artifactId>
< version> 4.1.12.Final< / version>
<排除项>
<排除>
< artifactId> hibernate-commons-annotations< / artifactId>
< groupId> org.hibernate.common< / groupId>
< /排除>
<排除>
< artifactId> hibernate-jpa-2.1-api< / artifactId>
< groupId> org.hibernate.javax.persistence< / groupId>
< /排除>
<排除>
< artifactId> hibernate-commons-annotations< / artifactId>
< groupId> org.hibernate< / groupId>
< /排除>
< /排除>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-entitymanager< / artifactId>
< version> 4.1.8.Final< / version>
<排除项>
<排除>
< artifactId> hibernate< / artifactId>
< groupId> org.hibernate< / groupId>
< /排除>
<排除>
< artifactId> hibernate-annotations< / artifactId>
< groupId> org.hibernate< / groupId>
< /排除>
<排除>
< artifactId> hibernate-commons-annotations< / artifactId>
< groupId> org.hibernate< / groupId>
< /排除>
<排除>
< artifactId> hibernate-jpa-2.1-api< / artifactId>
< groupId> org.hibernate.javax.persistence< / groupId>
< /排除>
< /排除>
< /依赖关系>
< dependency>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-commons-annotations< / artifactId>
< version> 3.2.0.Final< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate.javax.persistence< / groupId>
< artifactId> hibernate-jpa-2.0-api< / artifactId>
< version> 1.0.1.Final< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-jpamodelgen< / artifactId>
< version> 1.1.1.Final< / version>
< scope>提供< / scope>
< /依赖关系>
I am dealing with hibernate and as I opened my current project I figured out my Session-Factory is deprecated:
AnnotationConfiguration af = new AnnotationConfiguration();
SessionFactory factory = af.configure().buildSessionFactory();
Session session = factory.openSession();
AnnotationConfiguration seems to be deprecated by now... So I checked the JavaDoc and I got told it moved to:
org.hibernate.cfg.Configuration
My code works fine so far, actually I don't want to change it... But I googled and found someone who's asking himself the same question whythe SessionFactory needs to be changed... http://rgordon.co.uk/blog/2012/02/24/hibernate-please-dont-deprecate-yourself/
The Article is from 2012 (so not that old...) and describes everything in that way:
ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder();
serviceRegistryBuilder.applySettings(properties);
ServiceRegistry serviceRegistry = serviceRegistryBuilder.buildServiceRegistry();
Configuration configuration = new Configuration().addClass(FeedTradePersistable.class);
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
I implemented that. The JavaDoc proves that wrong - again! Deprecated. It refers to:
org.hibernate.boot.registry.StandardServiceRegistryBuilder
I googled that again. The results weren't so satisfying...
I started to modify the code...
ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
Configuration configuration = new Configuration();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
And an exception is thrown...
org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
at line:
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
I am pretty sure this is because I haven't specified any configuration-settings. Actually, I don't want to. I feel comfortable with the hibernate.cfg.xml.
I played a bit around with configuration.addFile(.. - wasn't that successful...
Has anyone an idea about that? Thanks
UPDATE: (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>
UPDATE (16. Feb 2014): I think it is necessary to show you my pom.xml as well. It took me a while until I figured out which combination of the Hibernate-Framework works for me...
<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>
As I got some time to modernize my software I decided to put some effort into it and did some research:
http://www.codejava.net/frameworks/hibernate/building-hibernate-sessionfactory-from-service-registry
provides a modern HibernateUtil
:
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
Configuration configuration = new Configuration().configure();
ServiceRegistryBuilder registry = new ServiceRegistryBuilder();
registry.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = registry.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
return sessionFactory;
}
}
Even though this version seems to work as well:
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
Configuration cfg = new Configuration();
sessionFactory = cfg.configure("hibernate.cfg.xml").buildSessionFactory();
} catch (Throwable ex) {
Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE, null, ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
But my problem was I didn't want to integrate a new version with the old libs. After updating I ran into a
java.lang.NoSuchMethodError: org.hibernate.annotations.common.reflection.java.JavaReflectionManager.injectClassLoaderDelegate(Lorg/hibernate/annotations/common/reflection/ClassLoaderDelegate;)V
often. Annoying. Usually Mkyong provides good solutions, but in my case he wrote the opposite solution of Stackoverflow...
So I searched some repositories and found a shockingly easy solution (compare: http://hibernate.org/orm/downloads/):
<!-- HIBERNATE -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.0.Final</version>
</dependency>
Some other minor issues which stopped me:
In the hibernate.cfg.xml
I had to change the line from update
to auto
:
<property name="hbm2ddl.auto">auto</property>
Compare it to my old pom.xml... - back then I had my first "encounter" with Hibernate and added everything which seemed useful until it worked. After it did so I stopped touching it for... Almost two years... Never change a "winning" team, right?
<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>4.1.12.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>
<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>
这篇关于寻找一个不被弃用的会话工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!