How to store Documents in Array using Hibernate OGM?

436 views Asked by At

I'm new to Hibernate OGM. Here I'm using MongoDB as my Database. I have three POJO classes and Employee and Address and Work. 1. Employee.java

import java.util.ArrayList;
import java.util.List;
import javax.persistence.ElementCollection;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OrderColumn;
import org.bson.types.ObjectId;
@Entity
public class Employee {
    private String name;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private ObjectId id;
    private String company;
    private int empId;
    @Embedded
    private Address add;
    @ElementCollection
    private List<String> tech;
    @ElementCollection
    @OrderColumn(name="CompanyName")
    private List<Work> work; // = new ArrayList<Work>();

    //setters and getters

    public Address getAdd() {
        return add;
    }

    public void setAdd(Address add) {
        this.add = add;
    }

    public List<String> getTech() {
        return tech;
    }

    public void setTech(List<String> tech) {
        this.tech = tech;
    }

    public List<Work> getWork() {
        return work;
    }

    public void setWork(List<Work> work) {
        this.work = work;
    }

    @Override
    public String toString() {
        return "id=" + id + ", name=" + name + ", company=" + company +","
                + "empId="+empId; //+"work "+work;
    }
}

2. Address.java

import javax.persistence.Embeddable;
@Embeddable
public class Address {
    private String localAdd;
    private String premAdd;
    //setters and getters
}

3.Work.java

import javax.persistence.Embeddable;
@Embeddable
public class Work {
    private String CompanyName;
    private String loc;
    private double totalexp;
    //setters and getters
}

Here I'm going to create Document in Mongodb with below statements

entityManagerFactory = HibernateOGMUtil.getEntityManagerFactory();
em = entityManagerFactory.createEntityManager();
Employee employee = new Employee();
Address address = new Address();
List<String> techLst = new ArrayList<String>();
List<Work> worklst = new ArrayList<Work>();
Work objwork1 = new Work();
techLst.add("Java");
techLst.add("SQL");
techLst.add("JavaScript");
address.setLocalAdd("Mumbai");
address.setPremAdd("Chennai");
objwork1.setCompanyName("XXX");
objwork1.setLoc("Mumbai");
objwork1.setTotalexp(5.2);
worklst.add(objwork1);
employee.setName("XYZ");
employee.setCompany("YYYY");
employee.setEmpId(12346);
employee.setAdd(address);
employee.setTech(techLst);
employee.setWork(worklst);
em.getTransaction().begin();
em.persist(employee);
em.getTransaction().commit();

I'm getting below error

2017-09-13 14:33:26.847  INFO 9184 --- [nio-8082-exec-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: myPu
    ...]
2017-09-13 14:33:26.928  INFO 9184 --- [nio-8082-exec-1] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.12.Final}
2017-09-13 14:33:26.931  INFO 9184 --- [nio-8082-exec-1] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2017-09-13 14:33:26.932  INFO 9184 --- [nio-8082-exec-1] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2017-09-13 14:33:26.988  INFO 9184 --- [nio-8082-exec-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-09-13 14:33:27.129  INFO 9184 --- [nio-8082-exec-1] o.h.o.d.impl.DatastoreProviderInitiator  : OGM000016: NoSQL Datastore provider: org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider
2017-09-13 14:33:27.153  INFO 9184 --- [nio-8082-exec-1] o.h.o.d.m.impl.MongoDBDatastoreProvider  : OGM001201: Connecting to MongoDB at localhost:27017,  with a timeout set at 10000 millisecond(s)
2017-09-13 14:33:27.154  INFO 9184 --- [nio-8082-exec-1] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2017-09-13 14:33:27.155  INFO 9184 --- [nio-8082-exec-1] org.mongodb.driver.cluster               : Adding discovered server localhost:27017 to client view of cluster
2017-09-13 14:33:27.161  INFO 9184 --- [nio-8082-exec-1] o.h.o.d.m.impl.MongoDBDatastoreProvider  : OGM001207: Connecting to Mongo database named [Test].
2017-09-13 14:33:27.199  INFO 9184 --- [localhost:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:2, serverValue:64}] to localhost:27017
2017-09-13 14:33:27.201  INFO 9184 --- [localhost:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 7]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=2031244}
2017-09-13 14:33:27.202  INFO 9184 --- [localhost:27017] org.mongodb.driver.cluster               : Discovered cluster type of STANDALONE
2017-09-13 14:33:27.202  INFO 9184 --- [nio-8082-exec-1] org.mongodb.driver.cluster               : No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
2017-09-13 14:33:27.216  INFO 9184 --- [nio-8082-exec-1] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:3, serverValue:65}] to localhost:27017
2017-09-13 14:33:27.282  INFO 9184 --- [nio-8082-exec-1] o.h.o.dialect.impl.GridDialectInitiator  : OGM000017: Grid Dialect: org.hibernate.ogm.dialect.impl.BatchOperationsDelegator
2017-09-13 14:33:27.283  INFO 9184 --- [nio-8082-exec-1] o.h.o.dialect.impl.GridDialectInitiator  : Grid dialect logs are disabled
2017-09-13 14:33:27.305  INFO 9184 --- [nio-8082-exec-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.ogm.dialect.impl.OgmDialect
2017-09-13 14:33:27.331  INFO 9184 --- [nio-8082-exec-1] o.h.e.j.e.i.LobCreatorBuilderImpl        : HHH000422: Disabling contextual LOB creation as connection was null
Initial EntityManagerFactory creation failed.javax.persistence.PersistenceException: Unable to build entity manager factory
2017-09-13 14:33:27.608 ERROR 9184 --- [nio-8082-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
    at com.cdt.controller.CDTController.createEmployeeData(CDTController.java:51) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_111]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_111]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_111]

If I remove the Work object from Employee and comment the "employee.setWork(worklst)" then it inserting records in mongodb. Can anyone help me to resolve the issue?

0

There are 0 answers