-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Bidirectional ManyToOne Null Pointer Exception
PostPosted: Fri Nov 13, 2015 5:03 am 
Newbie

Joined: Fri Nov 13, 2015 12:19 am
Posts: 13
Hi,

On Implementing, Bidirectional ManyToOne Association using Hibernate OGM - hibernate-ogm-5.0.0.Alpha1 (also tried with hibernate-ogm-4.2.0.Final) and MongoDB (version 3.0.4) it ended up with an Null Pointer Exp org.hibernate.ogm.datastore.mongodb.MongoDBDialect.insertOrUpdateAssociation(MongoDBDialect.java:624) (PFB, is the Stack Trace)

Test Case.
1) Have Employeer and Employee Class having oneToMany and ManyToOne Association Respectively (PFB, contains POJO class for the same)

2) Inserted Record in Employeer Table (Got Successfully Inserted) in MongoDB
[Mongo DB Details on Insert]
{
"_id" : "288d0843-63d7-4177-9428-3c6795c0470e",
"name" : "Hibernate"
}

3) Tried Inserting an Employee Information with Employeer Details. It was successfully inserted with Employeer ID
[Mongo DB Details on Insert with Employeer Information]
{
"_id" : "eca8e45d-cbde-4d4c-a5cc-21640d177a58",
"EmployerID" : "288d0843-63d7-4177-9428-3c6795c0470e",
"name" : "DNadar"
}

4) Verified Employeer Table No Information of Employee is stored in the table (It should have had an Array having Employee ID)

5) On Verifying Log, i could see Null Pointer Exception at
org.hibernate.ogm.datastore.mongodb.MongoDBDialect.insertOrUpdateAssociation(MongoDBDialect.java:624)

Tried Integrating the Source Code to find out the Issue, AssociatonContext's getEntityTuple Method is returning Null value for hostingEntity(Employeer in my case)
On verifying the Object, the ObjectState is Null with AssociationContext in Managed state. And On insertOrUpdateAssociation method it is trying to get the EntityTuple which is null and threw null pointer exception.

Tried Verifying the same with loading the Employeer Object (with Session.get(Class, id)) and then Set it into Employee Object. in this case it worked fine (Had an Array Object created at Employeer Entity having Employee Id). AssociationContext in that case was in Loading State.


Please Note : On the Source Code of Verison 5.0.0.Alpha1 i can see some TODO statement(MongoDBDialect class, Methodname - insertOrUpdateAssociation Line no -623 ) //TODO would that fail if getCollectionRole has dots? (Not sure what it means)

Let me know in case of any more information

Thank you,
Domnic nadar

Code:
java.lang.NullPointerException
   at org.hibernate.ogm.datastore.mongodb.MongoDBDialect.insertOrUpdateAssociation(MongoDBDialect.java:624)
   at org.hibernate.ogm.dialect.impl.ForwardingGridDialect.insertOrUpdateAssociation(ForwardingGridDialect.java:132)
   at org.hibernate.ogm.dialect.impl.BatchOperationsDelegator.insertOrUpdateAssociation(BatchOperationsDelegator.java:126)
   at org.hibernate.ogm.util.impl.AssociationPersister.flushToDatastore(AssociationPersister.java:170)
   at org.hibernate.ogm.persister.impl.EntityAssociationUpdater.addNavigationalInformationForInverseSide(EntityAssociationUpdater.java:161)
   at org.hibernate.ogm.persister.impl.EntityAssociationUpdater.addNavigationalInformationForInverseSide(EntityAssociationUpdater.java:114)
   at org.hibernate.ogm.persister.impl.OgmEntityPersister.addToInverseAssociations(OgmEntityPersister.java:1278)
   at org.hibernate.ogm.persister.impl.OgmEntityPersister.insert(OgmEntityPersister.java:1386)
   at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89)
   at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:447)
   at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:333)
   at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:335)
   at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
   at org.hibernate.ogm.dialect.eventstate.impl.EventContextManagingFlushEventListener.onFlush(EventContextManagingFlushEventListener.java:39)
   at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1224)
   at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:464)
   at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:2890)
   at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2266)
   at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)
   at org.hibernate.ogm.transaction.impl.ForwardingTransactionCoordinatorOwner.beforeTransactionCompletion(ForwardingTransactionCoordinatorOwner.java:37)
[code]

Sample Test cases : Employeer Class

[code]
@Entity
@Table(name = "Employer")
public class Employeer implements Serializable
{
   private static final long serialVersionUID = -8732345803771451030L;
   private String id;
   private String name;

   private Set<Employee> employees;

   @Id
   @GeneratedValue(generator = "uuid")
   @GenericGenerator(name="uuid", strategy="uuid2")
   public String getId() {
      return id;
   }
   public void setId(String id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }

   @OneToMany(fetch=FetchType.EAGER,mappedBy="employeer")
   @Cascade({ CascadeType.PERSIST, CascadeType.SAVE_UPDATE, CascadeType.DELETE })
   public Set<Employee> getEmployees() {
      return employees;
   }
   public void setEmployees(Set<Employee> employees) {
      this.employees = employees;
   }

}
[/code]

Employee Class
[code]
@Entity
@Table(name = "Employee")
public class Employee implements Serializable
{
   private static final long serialVersionUID = -8732345803771451030L;
   private String id;
   private String name;
   private Employeer employeer;

   @Id
   @GeneratedValue(generator = "uuid")
   @GenericGenerator(name="uuid", strategy="uuid2")
   public String getId() {
      return id;
   }
   public void setId(String id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   
   @ManyToOne
   @JoinColumn(insertable = true, updatable = true, name = "EmployerID")
   public Employeer getEmployeer() {
      return employeer;
   }
   public void setEmployeer(Employeer employeer) {
      this.employeer = employeer;
   }

}


Top
 Profile  
 
 Post subject: Re: Bidirectional ManyToOne Null Pointer Exception
PostPosted: Mon Nov 16, 2015 11:01 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Hi DNadar,
I've tried to replicate the issue but it seems to work fine on my machine.

I'have created a test with the entities you have provided (https://github.com/DavideD/hibernate-ogm/commit/ae8cb58d6d15561c652a31e1ebe2107028f461bc), and I've executed the tests using different configuration.

Mapping for ManyToOneInEntityTest:
Code:
> db.Employer.find().pretty();
{
        "_id" : "ee263b1a-1e1e-4608-bc18-f8c5c050e509",
        "name" : "Hibernate",
        "employees" : [
                "308c2866-01ec-4bde-9386-977ec97c858b"
        ]
}

> db.Employee.find().pretty();
{
        "_id" : "308c2866-01ec-4bde-9386-977ec97c858b",
        "EmployerID" : "ee263b1a-1e1e-4608-bc18-f8c5c050e509",
        "name" : "DNadar"
}


Mapping for ManyToOneGlobalTest:
Code:
> db.Associations.find().pretty();
{
        "_id" : {
                "EmployerID" : "db546c06-f624-4c4f-b39b-0bd8c6eee0f9",
                "table" : "Employee"
        },
        "rows" : [
                "0c8dfdaf-de6c-4821-98f0-0e936c182a9c"
        ]
}

> db.Employer.find().pretty();
{ "_id" : "db546c06-f624-4c4f-b39b-0bd8c6eee0f9", "name" : "Hibernate" }

> db.Employee.find().pretty();
{
        "_id" : "0c8dfdaf-de6c-4821-98f0-0e936c182a9c",
        "EmployerID" : "db546c06-f624-4c4f-b39b-0bd8c6eee0f9",
        "name" : "DNadar"
}


Mapping for ManyToOneCollectionTest:
Code:
> db.Employer.find().pretty();
{ "_id" : "7ff8e672-d3c6-4645-900c-8f9875612742", "name" : "Hibernate" }

> db.Employee.find().pretty();
{
        "_id" : "7fde5f7a-6278-4e25-8d67-d87e214f4b95",
        "EmployerID" : "7ff8e672-d3c6-4645-900c-8f9875612742",
        "name" : "DNadar"
}
> db.associations_Employee.find().pretty();
{
        "_id" : {
                "EmployerID" : "7ff8e672-d3c6-4645-900c-8f9875612742"
        },
        "rows" : [
                "7fde5f7a-6278-4e25-8d67-d87e214f4b95"
        ]
}


It might be that I'm missing something, feel free to change the test or let me know what's wrong.

Davide


Top
 Profile  
 
 Post subject: Re: Bidirectional ManyToOne Null Pointer Exception
PostPosted: Mon Nov 16, 2015 11:47 am 
Newbie

Joined: Fri Nov 13, 2015 12:19 am
Posts: 13
Many Thanks for your reply, Davide.

Actually my test case is,
1) Set Employeer into MongoDB Database
2) Set Employee Details along with Employeer Object [Which breaks out]

PFB, is the MainClass
Code:
public static void main(String[] args) {

      // First Create Employeer Object and Persist it in DB.
      Employeer employeer = new Employeer();
      employeer.setName( "Hibernate" );

      Session session = SessionFactory.getOpenSession();
      Transaction transaction = session.beginTransaction();
      session.save( employeer );

      session.flush();
      transaction.commit();
      session.clear();

      // Create Employee and Map it with Employeer.
      Employee employee = new Employee();
      employee.setName( "DNadar" );
      //employeer.getEmployees().add(employee);
      employee.setEmployeer( employeer ); // This Object has all the information of Employeer.


      transaction = session.beginTransaction();
      session.save( employee );

      session.flush();
      transaction.commit();
      session.clear();
      session.close();

   }



Output in MongoDB: It gives null Pointer exception at MongoDialet (Its doesnt have Employee Information in Employer entity)

Code:
db.getCollection('Employer').find({})
{
    "_id" : "081858a4-748d-416c-8fba-98f05a713b31",
    "name" : "Hibernate"
}

db.getCollection('Employee').find({})
{
    "_id" : "143a9bdd-0b98-4e67-adfc-c6c5751e8fb5",
    "EmployerID" : "081858a4-748d-416c-8fba-98f05a713b31",
    "name" : "DNadar"
}


Another Test Case by loading the Object (Which is working fine)
Code:
public static void main(String[] args) {

      // First Create Employeer Object and Persist it in DB.
      Employeer employeer = new Employeer();
      employeer.setName( "Hibernate" );

      Session session = SessionFactory.getOpenSession();
      Transaction transaction = session.beginTransaction();
      String id = (String)session.save( employeer );

      session.flush();
      transaction.commit();
      session.clear();

      transaction = session.beginTransaction();
      // Create Employee and Map it with Employeer by loading it from DB.
      Employee employee = new Employee();
      employee.setName( "DNadar" );
      employee.setEmployeer((Employeer)session.get(Employeer.class, id)  ); // Loading the Object works fine.

      session.save( employee );

      session.flush();
      transaction.commit();
      session.clear();
      session.close();
   }


Output from DB:
Code:
db.getCollection('Employee').find({})
{
    "_id" : "a0fbe0fe-a5e9-4a67-b971-d6377c724d4c",
    "EmployerID" : "6ce4c7d4-44f9-40f1-b96a-06d6ffbf8633",
    "name" : "DNadar"
}


db.getCollection('Employer').find({})

{
    "_id" : "6ce4c7d4-44f9-40f1-b96a-06d6ffbf8633",
    "name" : "Hibernate",
    "employees" : [
        "a0fbe0fe-a5e9-4a67-b971-d6377c724d4c"
    ]
}


Wanted to persist Bidirectional data without loading the Entity (Session.get(class,id)) if the object has all the information just like we in ORM.

Thank you,
Domnic Nadar.


Top
 Profile  
 
 Post subject: Re: Bidirectional ManyToOne Null Pointer Exception
PostPosted: Tue Nov 17, 2015 5:53 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Thanks,
I'll have a look

Davide


Top
 Profile  
 
 Post subject: Re: Bidirectional ManyToOne Null Pointer Exception
PostPosted: Sun Nov 22, 2015 1:43 pm 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
I think the object is expected to be managed by the session, the second test case works because you are getting it using the session.

Removing the session.clear() in the first test will make the code works.


Top
 Profile  
 
 Post subject: Re: Bidirectional ManyToOne Null Pointer Exception
PostPosted: Mon Nov 23, 2015 3:38 am 
Newbie

Joined: Fri Nov 13, 2015 12:19 am
Posts: 13
Hi Davide,

Thanks for your reply. The Main Method which i posted was just to recreate the Issue (Exception Occured)

In my application, I persist Employer details first and have the Object in UI. (you can consider this as an API triggered from UI to insert Employer details and then call another API call to get the Employer Detail using Hibernate Sessions)

Now User enters Employee details and link it to the corresponding Employeer object and triggers an API with Session.openSession to persist Employee info and the corresponding oneToMany relationShip.

For all the API calls, its a New Session created and then closed.

This works perfectly with ORM, i guess it internally calls a Select query to see whether Employer record is available to form relationship.

Let me know if you need more details.

Thank you,
Domnic Nadar.


Top
 Profile  
 
 Post subject: Re: Bidirectional ManyToOne Null Pointer Exception
PostPosted: Mon Nov 23, 2015 6:42 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Is it possible that in your application the object is still managed somehow after the new session is open?

Maybe it get merged (or has cascade merge set on the collection), or updated after opening the new session.


Top
 Profile  
 
 Post subject: Re: Bidirectional ManyToOne Null Pointer Exception
PostPosted: Mon Nov 23, 2015 6:44 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
I will check what happens in ORM next to see what's the difference in OGM.


Top
 Profile  
 
 Post subject: Re: Bidirectional ManyToOne Null Pointer Exception
PostPosted: Mon Nov 23, 2015 7:11 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The clean code would require indeed to have the object or its proxy in the session. If you don't want to load the Employer object, you can use session.load(Employer.class, id); instead of session.get(Employer.class, id) in your second test.
That would be the clean form.

But we should not throw an NPE on you, so let's investigate.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.