问题描述
从hibernate.cfg.xml文件加载映射文件时遇到问题
<?xml version =1.0encoding =UTF-8?>
<!DOCTYPE hibernate-configuration PUBLIC - // Hibernate / Hibernate配置DTD 3.0 // EN
http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd\"> ;
< hibernate-configuration>
< session-factory>
< property name =hibernate.connection.driver_class> org.hsqldb.jdbcDriver< / property>
< property name =hibernate.connection.url> jdbc:hsqldb:hsql:// localhost< / property>
< property name =hibernate.dialect> org.hibernate.dialect.HSQLDialect< / property>
< property name =hibernate.hbm2ddl.auto> create-drop< / property>
< mapping resource =User.hbm.xml/>
< / session-factory>
< / hibernate-configuration>
映射文件没有被加载到 SessionFactory $ c $
$ b
但是如果我在配置
实例中手动添加映射:
static {
Configuration config = new Configuration()。configure()。addResource(User.hbm.xml) ;
ServiceRegistry registry = new StandardServiceRegistryBuilder()。applySettings(config.getProperties())。build();
sessionFactory = config.buildSessionFactory(registry);
$ b 映射工作正常......,有什么建议?
解决方案看起来像一个问题是一样的。您不能将配置与新配置()。configure()
和 config.buildSessionFactory(注册表)
混合。您应该使用 StandardServiceRegistryBuilder
进行所有配置。
像这样
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()。
configure()。build();
SessionFactory sessionFactory = new Configuration()。buildSessionFactory(serviceRegistry);
I'm having problems when loading a mapping file from the hibernate.cfg.xml file
<?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">org.hsqldb.jdbcDriver</property>
<property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<mapping resource="User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
The mapping file is not getting loaded in the SessionFactory
but if i add the mapping manually in the Configuration
instance:
static {
Configuration config = new Configuration().configure().addResource("User.hbm.xml");
ServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
sessionFactory = config.buildSessionFactory(registry);
}
The mapping works correctly..., any suggestions??
解决方案 Looks like a problem is the same as here. You can't mix configurations with new Configuration().configure()
and config.buildSessionFactory(registry)
. You should do all configuration with StandardServiceRegistryBuilder
.Something like this
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
configure().build();
SessionFactory sessionFactory= new Configuration().buildSessionFactory(serviceRegistry);
这篇关于加载映射文件Hibernate 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!