Skip to content

Kundera with MongoDB

Devender Yadav edited this page Aug 16, 2017 · 3 revisions

MongoDB

MongoDB is an open-source document database with the dynamic schema that provides high performance, high availability and automatic scaling. It supports embedded data model that reduces I/O activities and makes reads & writes faster. Secondary indexes in MongoDB make querying faster. Automatic sharding is another key feature of MongoDB distributes collection data across machines.

MongoDB provides features like aggregations, geospatial querying, text search, etc. It also provides transport and storage encryption. MongoDB supports many authentication mechanisms like SCRAM-SHA-1, MongoDB-CR, etc. that clients can use to verify their identity.

Support

Being a JPA provider, Kundera provides support for MongoDB. It allows to perform CRUD and query operation over MongoDB using JPA specifications. Let's look into how to use Kundera over MongoDB as a database.

To use it, you need to have the following dependency in your pom.xml.

    <dependency>
         <groupId>com.impetus.kundera.client</groupId>
         <artifactId>kundera-mongo</artifactId>
         <version>${kundera.version}</version>
    </dependency>

Entity

    @Entity
    @Table(name = "PERSON", schema = "testDB@mongo_pu")
    public class PersonMongo
   {

    /** The person id. */
    @Id
    @Column(name = "PERSON_ID")
    private String personId;

    /** The person name. */
    @Column(name = "PERSON_NAME")
    private String personName;

    /** The age. */
    @Column(name = "AGE")
    private int age;
    // setters and getters. 
    }

Persistence unit configuration

    <persistence-unit name="mongo_pu">
            <provider>com.impetus.kundera.KunderaPersistence</provider>
            <properties>
                    <property name="kundera.nodes" value="localhost" />
                    <property name="kundera.port" value="27017" />
                    <property name="kundera.keyspace" value="testDB" />
                    <property name="kundera.dialect" value="mongodb" />                     
                    <property name="kundera.ddl.auto.prepare" value="create" />
                    <property name="kundera.client.lookup.class"
                            value="com.impetus.client.mongodb.MongoDBClientFactory" />
            </properties>
    </persistence-unit>

CRUD

Insert, Find, Update and Remove operations are straightforward

    PersonMongo person = new PersonMongo();
    person.setPersonId("1");
    person.setPersonName("John Smith");
    person.setAge(32);

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("mongo_pu");
    EntityManager em = emf.createEntityManager();

    //Insert data
    em.persist(person);

    em.clear();   //Clear cache before finding record

    //Search for data
    Person personFound = em.find(PersonMongo.class, "1");        

    //Update data
    person.setAge(33);
    em.merge(person);

    //Delete data
    em.remove(person);

    em.close();
    emf.close();

Queries

  • Find by key

      String findById = "Select p from PersonMongo p where p.personId=:personId";
      query = em.createQuery(findById);
      query.setParameter("personId", “1”);
    
  • Range

      // Find by greater than and less than clause over non row key
      String findAgeByGTELTEClause = "Select p from PersonMongo p where p.age <=:max AND p.age>=:min";
      query = em.createQuery(findAgeByGTELTEClause);
      query.setParameter("min", 32);
      query.setParameter("max", 35);
    
  • Between clause

      // find by between over non rowkey
      String findAgeByBetween = "Select p from PersonMongo p where p.age between :min AND :max";
      query = em.createQuery(findAgeByBetween);
      query.setParameter("min", 32);
      query.setParameter("max", 35);
    

For more details find this testcase.

GeoSpatial Queries

Kundera also supports GeoSpatial querying and that too in JPA way. Please find junits for more details.

Clone this wiki locally