0

I've been trying to mess around with MongoDB and Hibernate in Java. I'm having some troubles with the configuration file for it.

I've already used Hibernate in the past with SQL DB, but it seems that the config file has to be quite different for MongoDB.

According to this documentation, it should looks something like this:

<?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>

However, when I try to create a session with

new Configuration().configure().buildSessionFactory();

I get a following error:

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'.

Is my aproach wrong or is there something I overlooked?

Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30

3 Answers3

1

You can try basic test set up like below for your config.

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory( "eshop" );
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
// perform operations here
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
s7vr
  • 73,656
  • 11
  • 106
  • 127
  • It says `javax.persistence.PersistenceException: No Persistence provider for EntityManager named eshop` when I try to create the `EntityManagerFactory` for me, any idea what could cause it? – Honza Štefánik Jul 13 '17 at 21:08
  • I have no idea, but my `persistence.xml` is located under `src/main/resources/META_INF`. Quick search leads me to this post https://stackoverflow.com/questions/19322827/no-persistence-provider-for-entitymanager-named-x – s7vr Jul 13 '17 at 22:05
  • Yea I've already seen these questions, my `persistence.xml` is located correctly. – Honza Štefánik Jul 15 '17 at 15:57
1

From the javadoc of configure():

Use the mappings and properties specified in an application resource named hibernate.cfg.xml.

You are setting the persistence.xml instead. Using using javax.persistence.Persistence should work:

EntityManagerFactory emf = Persistence.createEntityManagerFactory( "eshop" );

If for some reason you need the session factory instead, but you are working with JPA, you can obtain it using the unwrap() method

SessionFactory sf = emf.unwrap( SessionFactory.class );    

UPDATE: You can also create the factory programmatically, there is a class OgmConfiguration (that extends 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();

In this example I've added several options to give an overview but if you are using defaults and you don't need the authentication, only the database name and the datastore provider are needed

Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30
0

use bellow code :

<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">
Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17
  • I tried having the usual `hibernate.cfg.xml` file which started with this, but it does require a dialect set, I get an exception because of it. The documentation I linked also says that the dialect isn't set with NoSQL DBs ("More interesting is a list of options that do not apply to Hibernate OGM and that should not be set: hibernate.dialect"). Is there some way to use the usual configuration file with the `persistence.xml` file together that I missed? – Honza Štefánik Jul 12 '17 at 17:42
  • I've updated my answer with another example, if that doesn't help, could you post somewhere an small test project to show what are you trying to do? Thansk – Davide D'Alto May 11 '18 at 10:14