Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Getting Null Pointer Exception
PostPosted: Wed Jun 10, 2015 8:16 am 
Newbie

Joined: Wed Jun 10, 2015 7:54 am
Posts: 3
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


Top
 Profile  
 
 Post subject: Re: Getting Null Pointer Exception
PostPosted: Thu Jun 11, 2015 4:53 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 386
Hi,

Try using transaction type "RESOURCE_LOCAL" instead of "JTA" (alternatively, you'd have to add a JTA implementation such as JBoss' Narayana (http://narayana.io/) to your classpath). Also you need to wrap your application code into a "transaction":

Code:
EntityManager manager = ...;

manager.getTransaction().begin();
Person anandMohan = new Person("Anand","Mohan");
manager.persist(anandMohan);
manager.getTransaction().commit();


The reason being that Hibernate OGM uses the transaction context for collecting changes and applying them at commit/flush time to the datastore in an optimized manner. Also check out what the reference guide (http://docs.jboss.org/hibernate/ogm/4.2/reference/en-US/html_single/#transactions) has to say on this topic.

Hth,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Getting Null Pointer Exception
PostPosted: Fri Jun 12, 2015 2:14 am 
Newbie

Joined: Wed Jun 10, 2015 7:54 am
Posts: 3
Thanks Gunnar.

The problem was with the jars, I had already tried it with the 2.13.1 mongodb java jar version but was not working, now it's it all fine.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.