我一直试图在Java中使用MongoDB和Hibernate。我的配置文件有些麻烦。
过去我已经在SQL DB中使用过Hibernate,但似乎配置文件必须与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'.
我的方法是错误的还是我忽略了什么?
答案 0 :(得分:1)
您可以尝试使用以下基本测试设置进行配置。
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory( "eshop" );
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
// perform operations here
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
答案 1 :(得分:1)
来自configure()
的javadoc:
使用应用程序资源中指定的映射和属性 命名为 hibernate.cfg.xml 。
您正在设置persistence.xml
。使用javax.persistence.Persistence
应该有效:
EntityManagerFactory emf = Persistence.createEntityManagerFactory( "eshop" );
如果由于某种原因您需要会话工厂,但是您正在使用JPA,则可以使用unwrap()
方法获取它
SessionFactory sf = emf.unwrap( SessionFactory.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();
在此示例中,我添加了几个选项以概述,但如果您使用默认值而不需要身份验证,则只需要数据库名称和数据存储提供程序
答案 2 :(得分:0)
使用以下代码:
<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">