Skip to content

Kundera Data As Object

Devender Yadav edited this page May 17, 2018 · 7 revisions

Are you a non-JPA developer but still wish to leverage the capabilities of Kundera? Kundera always tries to make things simple for users but under the shades of JPA where a user needs to have an understanding of EntityManagerFactory, EntityManager and many more JPA related things.

We have tried to make things simpler with Kundera - Data As Object. Using Data As Object lightweight API, user can perform JPA operations directly on objects rather than create traditional EntityManagerFactory, EntityManager.

Note: This is an experimental feature. We would love to hear from you, the valuable feedback.

Kundera Normal Approach :

    User user = new User();
    user.setUserId("0001");
    user.setFirstName("John");
    user.setLastName("Smith");
    user.setCity("London");

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

    em.persist(user);
    em.close();    
    emf.close(); 

Data As Object Approach :

    User user = new User();
    user.setUserId("0001");
    user.setFirstName("John");
    user.setLastName("Smith");
    user.setCity("London");
    user.save();

How to Use

To use it, user needs to add the following dependency in pom.xml in addition to the client dependencies:

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

JSON instead of persistence.xml

Rather than creating a META-INF/persistence.xml user needs to add a JSON properties file in the classpath. Example: client-properties.json:

{
  "com.impetus.kundera.dataasobject.entities.Employee,com.impetus.kundera.dataasobject.entities.Department": {
    "kundera.nodes": "localhost",
    "kundera.port": "9160",
    "kundera.client": "cassandra",
    "kundera.keyspace": "DAOTest",
    "kundera.ddl.auto.prepare": "update",
    "cql.version": "3.0.0",
    "kundera.client.lookup.class": "com.impetus.client.cassandra.thrift.ThriftClientFactory"
    }
}

Minor Change in POJO

POJO class needs to extends DefaultKunderaEntity<EntityName, IdDatatype>. All the remaining things are same.

Usual Kundera Entity :

@Entity
public class User 
{
    @Id
    private String userId;

    @Column
    private String firstName;

    @Column
    private String lastName;

    @Column
    private String city;

    // Getters & Setters
}

Data As Object Entity :

@Entity
public class User extends DefaultKunderaEntity<User, String>
{
    @Id
    private String userId;

    @Column
    private String firstName;

    @Column
    private String lastName;

    @Column
    private String city;

    // Getters & Setters
}

Basic configuration

Before performing any operation on an object, User needs to use bind() method.

 User.bind("client-properties.json", User.class);

Internally it will create EntityManagerFactory and EntityManager instances.

After performing all operations, in the end, user needs to use unbind() method

 User.unbind();

Internally this will close instances of EntityManagerFactory and EntityManager.

Supported Operation :

CRUD

 User.bind("client-properties.json", User.class);

 User user = new User();
 user.setUserId("101");
 user.setFirstName("John");
 user.setLastName("Smith");
 user.setCity("London");

 // Save
 user.save();

 //Find
 User u = new User().find(101);

 u.setFirstName("Adam");

 //Update
 u.update();

 //Delete
 u.delete();

Refer Testcase for more Details.

Query

JPA Query:

JPA queries can be done using query() method. Example:

List<Book> results = new Book().query("select b from Book b where b.bookId=1");

Native Query:

Native queryies (e.g. CQL queries for cassandra) can be done using query() method with QueryType.NATIVE. Example:

List results = new Book().query("select \"TITLE\" from \"Book\"", QueryType.NATIVE);

Refer Testcase for more Details.

Additional Features:

If a user is using Kundera only for one datastore i.e. all the entities are having the same configuration. Then, don't need to repeat the same config for all the entity classes. This config should be used:

{
  "all": {
    "kundera.nodes": "localhost",
    "kundera.port": "9160",
    "kundera.client": "cassandra",
    "kundera.keyspace": "DAOTest",
    "kundera.ddl.auto.prepare": "update",
    "cql.version": "3.0.0",
   "kundera.client.lookup.class": "com.impetus.client.cassandra.thrift.ThriftClientFactory"
  }
}

Polyglot

Data As Object is in experimental phase. For polyglot, user needs to add persistence.xml(same Kundera route). Check wiki for polyglot for more details.

Additionally in JSON properties file user needs to add kundera.pu name as shown below:

{
  "com.impetus.kundera.dataasobject.entities.User": {
    "kundera.pu": "twirdbms"
  },
  "com.impetus.kundera.dataasobject.entities.Tweets": {
    "kundera.pu": "twingo"
  },
 "com.impetus.kundera.dataasobject.entities.Video": {
    "kundera.pu": "twissandra"
  }
}

Refer Testcase for more Details.

Limitation

  • Working fine for simple entities. Not tested for embedded entities, entities having relations(e.g. one to many).
  • Transactions are not supported.
  • No way to clear entity manager level cache.
Clone this wiki locally