0

I am trying a simple Mongo DB and Hibernate OGM example. Here is my persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
  <persistence-unit name="mongo-ogm" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
    <class>com.test.Employee</class>
    <properties>
      <!-- <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform"/> -->
      <property name="hibernate.ogm.datastore.provider" value="mongodb"/>
      <property name="hibernate.ogm.datastore.database" value="mongotest"/>
      <property name="hibernate.ogm.datastore.create_database" value="true"/>
      <property name="hibernate.ogm.datastore.host" value="mymongo"/>
      <property name="hibernate.ogm.datastore.port" value="1234"/>
    </properties>
  </persistence-unit>
</persistence>

In the logs, I see

INFO: OGM001206: Mongo database named [mongotest] is not defined. Creating it!

I expect that the database mongotest should be created in Mongo DB after the application boots up.. but it does not show up when I execute show dbs in Mongo. Also, my JPA based create (em.persist) does not throw any errors - I am not sure what's happening behind the scenes. This is fat JAR based setup (not a Java EE WAR)

Abhishek
  • 1,175
  • 1
  • 11
  • 21
  • It's normal. MongoDB does not actually create either a database namespace or a collection until you actually write something to it. Once you actually write some data, then you will see both database and collection exist. – Neil Lunn Jun 18 '17 at 09:00
  • Ok thanks for confirming. But I tried this as well i.e. used `EntityManager.persist()` to create a record - no error. I was expecting the collection to get created - but neither do I see the database nor the collection. Guess I am missing something, but not sure what – Abhishek Jun 18 '17 at 11:53

1 Answers1

1

Ok my bad.. Spoilt by the implicit Java EE transaction management ! This is a plain Java SE application and my create logic was missing transaction demarcation i.e. I added em.getTransaction().begin(); before calling persist and added em.getTransaction().commit(); after that and I can see the expected results

Abhishek
  • 1,175
  • 1
  • 11
  • 21