- 创建SessionFactory时buildSessionFactory()方法过期
SessionFactory sf = new Configuration()
.configure() // 直接用hibernate.cfg.xml来进行配置和创建
.buildSessionFactory();
B 在4.0.0版本中,因Configuration().configure()方法被废除了,故用以下方式来创建
Configuration config = new Configuration()
.addResource("org/monday/domain/Customer.hbm.xml");
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.buildServiceRegistry();
SessionFactory sf = config.buildSessionFactory(serviceRegistry);
在此方法中默认使用hibernate.properties文件来进行配置,而hibernate.properties文件中不能添加映射文件(.hbm.xml),所以只能用.addResource("org/monday/domain/Customer.hbm.xml")来添加映射文件,这里必须是全路径。使用如下方法时,读取hibernate.cfg.xml时会出错(找不到元素 'hibernate-configuration' 的声明)
Unable to perform unmarshalling at line number 4 and column 26 in RESOURCE hibernate.cfg.xml
可能是这个版本对DTD不识别,或者是方法运用错误。
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.configure() //想通过这个来读取hibernate.cfg.xml来进行配置
.buildServiceRegistry();
SessionFactory sf = new MetadataSources( serviceRegistry )
.buildMetadata()
.buildSessionFactory();
C 在4.0及以上版本中,用hibernate.cfg.xml来创建sessionFactory的方法
从Hibernate 4之后,Configuration类中,原先常用的,获取SessionFactory的方法buildSessionFactory()被标记为过时,官方建议使用buildSessionFactory(ServiceRegistry sr)这个方法来获取SessionFactory。新的方法,我理解,是因为用户有可能(虽然很罕见)根据不同的cfg.xml文件来创建出不同的Configuration对象实例,新的方法可以根据不同的实例来构建出不同的ServiceRegistry,进而得到不同需求的sessionFactory.
这样做的具体用例我虽然还没有遇到,但是可以想象,新的方法对分布式数据库将有良好的支持。
比如,一个分布式数据库系统中包含了分布在多个不同主机,不同类型的数据库,我们开发一个WEB应用,要求对这些数据库都要进行操作,假如这些数据库的信息分别写在了多个cfg.xml中,那么,我们就可以根据这些不同的配置文件,生成针对不同数据库SessionFactory,并将其保存在一个Map中,数据库名作为键值,这样我们就可以在实现DAO层的时候,根据访问不同数据库的需要而获取到不用的Session.
private static final SessionFactory sessionFactory = buildSessionFactory(); private static ServiceRegistry serviceRegistry; Configuration configuration = new Configuration(); configuration.configure(); serviceRegistry = new ServiceRegistryBuilder().applySettings( configuration.getProperties()).buildServiceRegistry(); return configuration.buildSessionFactory(serviceRegistry);
package cn.com.zyx.model;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class StudentTest {
public static void main(String[] args) {
Student s = new Student();
s.setName("111");
s.setSex("男");
Configuration cfg = new Configuration();
cfg.configure();
//SessionFactory sf=cfg.configure().buildSessionFactory();//*过期方法*解析Hibernate.cfg.xml 然后返回一个已经拥有配置选项的Configuration
ServiceRegistry sr=new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
//SessionFactory类似数据库库的一个Connection
SessionFactory sf =cfg.buildSessionFactory(sr);
//打开一个连接
Session session =sf.openSession();
//开始一个事务
session.beginTransaction();
session.save(s);
//获得一个事务,并提交
session.getTransaction().commit();
//关闭连接
session.close();
//关闭工厂
sf.close();
}
}
- Error applying BeanValidation relational constraints错误的解决
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.applyRelationalConstraints(BeanValidationIntegrator.java:219)
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:126)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:303)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1750)
at cn.com.zyx.model.StudentTest.main(StudentTest.java:20)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.applyRelationalConstraints(BeanValidationIntegrator.java:208)
... 4 more
Caused by: org.hibernate.HibernateException: Unable to build the default ValidatorFactory
at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:524)
at org.hibernate.cfg.beanvalidation.TypeSafeActivator.applyDDL(TypeSafeActivator.java:119)
... 9 more
Caused by: javax.validation.ValidationException: Unable to instantiate Configuration.
at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:272)
at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111)
at org.hibernate.cfg.beanvalidation.TypeSafeActivator.getValidatorFactory(TypeSafeActivator.java:521)
... 10 more
javax.persistence.validation.mode默认情况下是auto的,就是说如果不设置的话它是会自动去你的classpath下面找一个bean-validation**包,但是找不到,所以beanvalitionFactory错误。