Hi,
When using all the adding all the required jars of the hibernate-ogm for mongodb getting a null pointer exception.
Person.java
Code:
package com.mongodb;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.bson.types.ObjectId;
import org.hibernate.annotations.Type;
@Entity
public class Person {
@Id
@Type(type = "objectid")
private String id;
private String firstName;
private String lastName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Person(){
}
public Person(String firstName,String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
}
Persistence.xml
Code:
<?xml version="1.0"?>
<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="mongodb-test" transaction-type="JTA">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<properties>
<property name="hibernate.ogm.datastore.provider" value="mongodb"></property>
<property name="hibernate.ogm.datastore.host" value="192.168.0.237"></property>
<property name="hibernate.ogm.datastore.create_database" value="true"></property>
<property name="hibernate.ogm.datastore.database" value="anand_test"></property>
<property name="hibernate.ogm.mongodb.connection_timeout" value="5000"></property>
<property name="hibernate.ogm.mongodb.authentication_mechanism" value="PLAIN"></property>
<property name="hibernate.ogm.datastore.document.association_storage" value="IN_ENTITY"></property>
<property name="hibernate.ogm.mongodb.association_document_storage" value="GLOBAL_COLLECTION"></property>
<property name="hibernate.ogm.mongodb.write_concern" value="ACKNOWLEDGED"></property>
<property name="hibernate.ogm.mongodb.read_preference" value="PRIMARY_PREFERRED"></property>
</properties>
</persistence-unit>
</persistence>
TestMongoConnection.java
Code:
package test;
import com.mongodb.Person;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class TestMongoConnection {
public static void main(String[] args) {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("mongodb-test");
EntityManager manager = entityManagerFactory.createEntityManager();
Person anandMohan = new Person("Anand","Mohan");
manager.persist(anandMohan);
manager.close();
}
}
I am getting
Code:
Jun 10, 2015 5:36:45 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Jun 10, 2015 5:36:45 PM org.hibernate.ogm.datastore.impl.DatastoreProviderInitiator initiateService
INFO: OGM000016: NoSQL Datastore provider: org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider
Jun 10, 2015 5:36:45 PM org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider createMongoClient
INFO: OGM001201: Connecting to MongoDB at 192.168.0.237:27017, with a timeout set at 5000 millisecond(s)
Jun 10, 2015 5:36:45 PM org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider extractDatabase
INFO: OGM001207: Connecting to Mongo database named [anand_test].
Jun 10, 2015 5:36:45 PM org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider extractDatabase
INFO: OGM001206: Mongo database named [anand_test] is not defined. Creating it!
Jun 10, 2015 5:36:45 PM org.hibernate.ogm.dialect.impl.GridDialectInitiator$GridDialectInstantiator newInstance
INFO: OGM000017: Grid Dialect: org.hibernate.ogm.dialect.impl.BatchOperationsDelegator
Jun 10, 2015 5:36:45 PM org.hibernate.ogm.dialect.impl.GridDialectInitiator$GridDialectInstantiator newInstance
INFO: Grid dialect logs are disabled
Jun 10, 2015 5:36:45 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.ogm.dialect.impl.OgmDialect
Jun 10, 2015 5:36:46 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000422: Disabling contextual LOB creation as connection was null
Jun 10, 2015 5:36:46 PM org.hibernate.ogm.transaction.impl.OgmJtaPlatformInitiator initiateService
INFO: OGM000076: No explicit or implicit defined JTAPlatform. Using NoJtaPlatform
Jun 10, 2015 5:36:46 PM org.hibernate.ogm.cfg.impl.Version <clinit>
INFO: OGM000001: Hibernate OGM 4.2.0.Final
Exception in thread "main" java.lang.NullPointerException
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:76)
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:76)
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:118)
at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1602)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:210)
at org.hibernate.jpa.internal.EntityManagerImpl.<init>(EntityManagerImpl.java:91)
at org.hibernate.jpa.internal.EntityManagerFactoryImpl.internalCreateEntityManager(EntityManagerFactoryImpl.java:345)
at org.hibernate.jpa.internal.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:313)
at org.hibernate.ogm.jpa.impl.OgmEntityManagerFactory.createEntityManager(OgmEntityManagerFactory.java:44)
at test.TestMongoConnection.main(TestMongoConnection.java:21)
Java Result: 1