java 寻找一个不被弃用的会话工厂
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17911308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Looking for a not-deprecated session-factory
提问by Qohelet
I am dealing with hibernate and as I opened my current project I figured out my Session-Factory is deprecated:
我正在处理 hibernate,当我打开当前项目时,我发现我的 Session-Factory 已被弃用:
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:
AnnotationConfiguration 现在似乎已被弃用......所以我检查了 JavaDoc 并被告知它移至:
org.hibernate.cfg.Configuration
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/
到目前为止,我的代码运行良好,实际上我不想更改它......但我用谷歌搜索并发现有人问自己同样的问题为什么需要更改 SessionFactory... http://rgordon.co.uk/博客/2012/02/24/hibernate-please-dont-deprecate-yourself/
The Article is from 2012 (so not that old...) and describes everything in that way:
这篇文章是 2012 年的(所以不是那么旧......)并以这种方式描述了一切:
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:
我实现了。JavaDoc 再次证明这是错误的!已弃用。它指的是:
org.hibernate.boot.registry.StandardServiceRegistryBuilder
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
org.hibernate.HibernateException:未设置“hibernate.dialect”时连接不能为空
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.
我很确定这是因为我没有指定任何配置设置。其实我不想 我对 hibernate.cfg.xml 感觉很舒服。
I played a bit around with configuration.addFile(.. - wasn't that successful...
我玩了一下 configuration.addFile(.. - 不是那么成功......
Has anyone an idea about that? Thanks
有没有人对此有想法?谢谢
UPDATE: (hibernate.cfg.xml)
更新:(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...
更新(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>
采纳答案by Qohelet
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-registryprovides a modern HibernateUtil
:
http://www.codejava.net/frameworks/hibernate/building-hibernate-sessionfactory-from-service-registry提供了一个现代的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
java.lang.NoSuchMethodError: org.hibernate.annotations.common.reflection.java.JavaReflectionManager.injectClassLoaderDelegate(Lorg/hibernate/annotations/common/reflection/ClassLoaderDelegate;)V
often. Annoying. Usually Mkyongprovides good solutions, but in my case he wrote the opposite solution of Stackoverflow...
经常。恼人的。通常Mkyong提供了很好的解决方案,但在我的情况下,他写了Stackoverflow的相反解决方案......
So I searched some repositories and found a shockingly easy solution (compare: http://hibernate.org/orm/downloads/):
所以我搜索了一些存储库并找到了一个非常简单的解决方案(比较: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
:
阻止我的其他一些小问题:在hibernate.cfg.xml
我不得不将行更改update
为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?
将它与我的旧 pom.xml 进行比较... - 那时我第一次与 Hibernate “相遇”并添加了所有看起来有用的东西,直到它起作用为止。这样做之后我就不再碰它了……差不多两年了……永远不要改变一支“获胜”球队,对吧?
<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>
回答by Owain Cole
I think for 4.3 it's now:
我认为 4.3 现在是:
Configuration configuration=new Configuration()
.configure(); // configures settings from hibernate.cfg.xml
StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
// If you miss the below line then it will complaing about a missing dialect setting
serviceRegistryBuilder.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
回答by Bharathiraja
yes, in hibernate version 4. 3 both buildSessionfactory() and ServiceRegistryBuilder() are deprecated.
是的,在休眠版本 4.3 中 buildSessionfactory() 和 ServiceRegistryBuilder() 都已弃用。
so u have change your coding like below to get session factory for hibernate version 4.3
所以你已经改变你的编码如下以获得休眠版本 4.3 的会话工厂
public class HibernateSessionFactory {
private static final SessionFactory sessionFactory = buildSessionFactory1();
private static SessionFactory buildSessionFactory1() {
Configuration configuration = new Configuration().configure(); // configuration
// settings
// from
// hibernate.cfg.xml
StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
serviceRegistryBuilder.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
return configuration.buildSessionFactory(serviceRegistry);
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
}