MongoDB 的休眠配置
Hibernate configuration with MongoDB
我一直在尝试使用 MongoDB 和 Java 中的 Hibernate。我在使用它的配置文件时遇到了一些问题。
我过去已经将 Hibernate 与 SQL 数据库一起使用,但似乎 MongoDB 的配置文件必须完全不同。
根据 this documentation,它应该看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="eshop" transaction-type="JTA">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<class>org.hsnr.rest.domain.entities.Address</class>
<class>org.hsnr.rest.domain.entities.Order</class>
<class>org.hsnr.rest.domain.entities.Person</class>
<class>org.hsnr.rest.domain.entities.Product</class>
<class>org.hsnr.rest.domain.entities.User</class>
<properties>
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform" />
<property name="hibernate.ogm.datastore.provider"
value="mongodb" />
</properties>
</persistence-unit>
</persistence>
但是,当我尝试使用
创建会话时
new Configuration().configure().buildSessionFactory();
我收到以下错误:
org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 5 and column 28 in RESOURCE hibernate.cfg.xml. Message: cvc-elt.1: Cannot find the declaration of element 'persistence'.
是我的方法有误还是我忽略了什么?
您可以像下面这样为您的配置尝试基本测试设置。
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory( "eshop" );
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
// perform operations here
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
使用下面的代码:
<hibernate-configuration
xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
来自 configure()
的 javadoc:
Use the mappings and properties specified in an application resource
named hibernate.cfg.xml.
您设置的是 persistence.xml
。使用 using javax.persistence.Persistence
应该有效:
EntityManagerFactory emf = Persistence.createEntityManagerFactory( "eshop" );
如果由于某种原因您需要会话工厂,但您使用的是 JPA,则可以使用 unwrap()
方法
获取它
SessionFactory sf = emf.unwrap( SessionFactory.class );
更新:
您还可以以编程方式创建工厂,有一个 class OgmConfiguration(扩展 Configuraiton):
OgmConfiguration configuration = new OgmConfiguration();
// This is optional, if you want to set some options using
// a fluent API
configuration.configureOptionsFor( MongoDB.class )
.writeConcern( WriteConcernType.UNACKNOWLEDGED );
SessionFactory sf = configuration
.addAnnotatedClass( org.hsnr.rest.domain.entities.Address.class )
// ... Other annotated classes
.setProperty( OgmProperties.DATABASE, "mongodb_database" )
.setProperty( OgmProperties.DATASTORE_PROVIDER, DatastoreProviderType.MONGODB.name() )
// All this properties are optional, appropriate default will be used if missing
.setProperty( OgmProperties.CREATE_DATABASE, "false" )
.setProperty( OgmProperties.USERNAME, "username" )
.setProperty( OgmProperties.PASSWORD, "password" )
.setProperty( OgmProperties.HOST, "localhost:12897" )
// Check MongoDBProperties for MongoDB specific options
.setProperty( MongoDBProperties.AUTHENTICATION_MECHANISM, AuthenticationMechanismType.BEST.name() )
.buildSessionFactory();
在此示例中,我添加了几个选项以提供概述,但如果您使用默认设置并且不需要身份验证,则只需要数据库名称和数据存储提供程序
我一直在尝试使用 MongoDB 和 Java 中的 Hibernate。我在使用它的配置文件时遇到了一些问题。
我过去已经将 Hibernate 与 SQL 数据库一起使用,但似乎 MongoDB 的配置文件必须完全不同。
根据 this documentation,它应该看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="eshop" transaction-type="JTA">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<class>org.hsnr.rest.domain.entities.Address</class>
<class>org.hsnr.rest.domain.entities.Order</class>
<class>org.hsnr.rest.domain.entities.Person</class>
<class>org.hsnr.rest.domain.entities.Product</class>
<class>org.hsnr.rest.domain.entities.User</class>
<properties>
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform" />
<property name="hibernate.ogm.datastore.provider"
value="mongodb" />
</properties>
</persistence-unit>
</persistence>
但是,当我尝试使用
创建会话时new Configuration().configure().buildSessionFactory();
我收到以下错误:
org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 5 and column 28 in RESOURCE hibernate.cfg.xml. Message: cvc-elt.1: Cannot find the declaration of element 'persistence'.
是我的方法有误还是我忽略了什么?
您可以像下面这样为您的配置尝试基本测试设置。
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory( "eshop" );
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
// perform operations here
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
使用下面的代码:
<hibernate-configuration
xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
来自 configure()
的 javadoc:
Use the mappings and properties specified in an application resource named hibernate.cfg.xml.
您设置的是 persistence.xml
。使用 using javax.persistence.Persistence
应该有效:
EntityManagerFactory emf = Persistence.createEntityManagerFactory( "eshop" );
如果由于某种原因您需要会话工厂,但您使用的是 JPA,则可以使用 unwrap()
方法
SessionFactory sf = emf.unwrap( SessionFactory.class );
更新: 您还可以以编程方式创建工厂,有一个 class OgmConfiguration(扩展 Configuraiton):
OgmConfiguration configuration = new OgmConfiguration();
// This is optional, if you want to set some options using
// a fluent API
configuration.configureOptionsFor( MongoDB.class )
.writeConcern( WriteConcernType.UNACKNOWLEDGED );
SessionFactory sf = configuration
.addAnnotatedClass( org.hsnr.rest.domain.entities.Address.class )
// ... Other annotated classes
.setProperty( OgmProperties.DATABASE, "mongodb_database" )
.setProperty( OgmProperties.DATASTORE_PROVIDER, DatastoreProviderType.MONGODB.name() )
// All this properties are optional, appropriate default will be used if missing
.setProperty( OgmProperties.CREATE_DATABASE, "false" )
.setProperty( OgmProperties.USERNAME, "username" )
.setProperty( OgmProperties.PASSWORD, "password" )
.setProperty( OgmProperties.HOST, "localhost:12897" )
// Check MongoDBProperties for MongoDB specific options
.setProperty( MongoDBProperties.AUTHENTICATION_MECHANISM, AuthenticationMechanismType.BEST.name() )
.buildSessionFactory();
在此示例中,我添加了几个选项以提供概述,但如果您使用默认设置并且不需要身份验证,则只需要数据库名称和数据存储提供程序