Skip to content

Building Applications with Kundera and Spring

chhavigangwal edited this page Jun 2, 2014 · 11 revisions

Introduction

Spring framework is an open source enterprise application framework for java platform. It is a very popular framework to build web application on the top of j2ee platform.

Spring with Kundera

Being an object mapper, Kundera can be used at DAO layer to persist and retrieve data from NoSQL as well as RDBMS datastores. Kundera also supports polyglot persistence to store data across NoSQL as well as RDBMS datastores using Spring.

Configuration

In order to configure Spring application with Kundera you only need to create appContext.xml for spring. It is a XML file for spring configuration. All bean specific configurations can be specified here.

appContext.xml :

Create a bean called emf-p for Local Entity Manager Factory for persistence-unit “cassandra-pu”.

<bean id="emf-p"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
	<property name="persistenceUnitName" value="pu-name" />
	<property name="loadTimeWeaver">
		<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
	</property>
</bean>

Configure location of your persistence.xml ( default location would be META-INF/persistence.xml).

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
</bean>
<bean id="pum"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
	<property name="persistenceXmlLocations">
		<list>
			<value>META-INF/persistence.xml</value>
		</list>
	</property>
</bean>

Creating/Injecting EntityManager/EntityManagerFactory :

For container managed Entity Manager, use a annotation called persistence context annotation. And provide persistence-unit name and type of persistence context to that annotation at field for creating entity manager.

@PersistenceContext (unitName="pu-name", type=PersistenceContextType.EXTENDED)
private EntityManager em;

For Application managed EntityManager use a annotation called called persistence unit annotation and provide persistence-unit name to that annotation at field level for creating entity manager factory. After that you can create entity manager from entity manager factory by calling createEntityManager method.

@PersistenceUnit (unitName="pu-name")
private EntityManagerFactory emf;

Create Entity Manager

private EntityManager em = emf.createEntityManager ();

Polyglot persistence using Spring + Kundera

Follow the given steps to implement polyglot persistence using Spring with Kundera.

A sample project for same is available at https://github.com/impetus-opensource/Kundera/tree/trunk/examples

Configure applicationContext.xml:

	<bean id="emf-p"
	class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="persistenceUnitName" value="cassandra-pu,mongo-pu" />
		<property name="loadTimeWeaver">
			<bean		class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
		</property>
	</bean>

        <!-- emf-p1 and emf-p2 are required only in case of polyglot persistence
        (i.e. using multiple cross data store persistence unit --> 
	<bean id="emf-p1"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
	<property name="persistenceUnitName" value="cassandra-pu" />
		<property name="loadTimeWeaver">
			<bean				class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
		</property>
	</bean>
	<bean id="emf-p2"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="persistenceUnitName" value="mongo-pu" />
		<property name="loadTimeWeaver">
			<bean 				class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
		</property>
	</bean>

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" >
	</bean>
<bean id="pum"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
		<property name="persistenceXmlLocations">
			<list>
				<value>META-INF/persistence.xml</value>
			</list>
		</property>
	</bean>
</beans>

Persistence.xml : Create three persistence-unit one for cassandra-pu one for mongo-pu and last one for polyglot persistence i.e. cassandra-pu,mongo-pu.

	<persistence-unit name="cassandra-pu">
		<provider>com.impetus.kundera.KunderaPersistence</provider>
    	 <class>com.impetus.kundera.datakeeper.entities.DocumentInfo</class>
		<class>com.impetus.kundera.datakeeper.entities.Employee</class>
		<class>com.impetus.kundera.datakeeper.entities.SubordinatesCounter</class>
		<exclude-unlisted-classes>true</exclude-unlisted-classes>
		<properties>
			<property name="kundera.nodes" value="localhost" />
			<property name="kundera.port" value="9160" />
			<property name="kundera.keyspace" value="datakeeper" />
			<property name="kundera.client" value="thrift" />
			<property name="kundera.client.lookup.class"
				value="com.impetus.client.cassandra.thrift.ThriftClientFactory" />
		</properties>
	</persistence-unit>

	<persistence-unit name="mongo-pu">
		<provider>com.impetus.kundera.KunderaPersistence</provider>
       	 <class>com.impetus.kundera.datakeeper.entities.DocumentInfo</class>
		<exclude-unlisted-classes>true</exclude-unlisted-classes>
		<properties>
			<property name="kundera.nodes" value="localhost" />
			<property name="kundera.port" value="27017" />
			<property name="kundera.keyspace" value="datakeeper" />
			<property name="kundera.client.lookup.class"
				value="com.impetus.mongodb.MongoDBClientFactory" />
		</properties>
	</persistence-unit>

	<persistence-unit name="cassandra-pu,mongo-pu">
		<provider>com.impetus.kundera.KunderaPersistence</provider>
	</persistence-unit>
</persistence>

Note: In case of polyglot persistence you need to mention classes under persistence-unit class tag to specify that entities belong to a particular datastore.

Deploy war in Tomcat:

Build the project using

mvn clean install

Copy war of project and deploy it on Tomcat.

Hit the following URL to start http://localhost:8080/datakeeper/xhtml/login/login.jsf

You should be able to see following screens if your project is deployed properly on tomcat.

  1. Registration page :

    Provide all Employee details to register. Register

  2. Login page :

    After success full registeration you will be able to login and following page will appear. Login

Validate from database:

Now validate the Employee data which is stored in Cassandra via Cassandra's command line utility : Cli output

Home

Clone this wiki locally