Skip to content

Using Kundera with Spring

xamry edited this page Mar 26, 2013 · 1 revision

We'll demonstrate here how to use Kundera with Spring. Since Spring supports JPA and Kundera is built on top of JPA, this combination works smooth and seamlessly. You can find all source codes used in the example inside package com.impetus.kundera.examples.spring.

Spring Configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	
	<bean id="springExampleDao" class="com.impetus.kundera.examples.spring.SpringExampleDao">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>		
	<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="persistenceUnitName" value="cassandra_pu" />
	</bean>	
</beans>

JPA configuration

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
	http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
	version="2.0">
	<persistence-unit name="cassandra_pu">
		<provider>com.impetus.kundera.KunderaPersistence</provider>		
		<properties>			
			<property name="kundera.nodes" value="localhost"/>
			<property name="kundera.port" value="9160"/>
			<property name="kundera.keyspace" value="KunderaExamples"/>
			<property name="kundera.dialect" value="cassandra"/>
			<property name="kundera.client.lookup.class" value="com.impetus.client.cassandra.pelops.PelopsClientFactory" />
			<property name="kundera.cache.provider.class" value="com.impetus.kundera.cache.ehcache.EhCacheProvider"/>
                        <property name="kundera.cache.config.resource" value="/ehcache-test.xml"/>               
 		</properties>
       </persistence-unit>
</persistence>	

Entity Class

package com.impetus.kundera.examples.spring;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name="SIMPLE_COMMENT", schema="KunderaExamples@cassandra_pu")
@XmlRootElement(name = "SimpleComment")
public class SimpleComment
{
    @Id
    @Column(name = "COMMENT_ID")
    private int id;

    @Column(name = "USER_NAME")
    private String userName;

    @Column(name = "COMMENT_TEXT")
    private String commentText;

    public SimpleComment() {
    }    
    public SimpleComment(int commentId, String userName, String commentText) {
        this.id = commentId;
        this.userName = userName;
        this.commentText = commentText;
    }      
    public int getId()
    {
        return id;
    }   
    public void setId(int id)
    {
        this.id = id;
    }
    public String getUserName()
    {
        return userName;
    }
    public void setUserName(String userName)
    {
        this.userName = userName;
    }
    public String getCommentText()
    {
        return commentText;
    }
    public void setCommentText(String commentText)
    {
        this.commentText = commentText;
    }  
}

DAO Service

package com.impetus.kundera.examples.spring;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import org.springframework.stereotype.Service;

@Service
public class SpringExampleDao
{
    EntityManagerFactory entityManagerFactory;
    public SimpleComment addComment(int id, String userName, String commentText)
    {
        SimpleComment simpleComment = new SimpleComment(id, userName, commentText);        
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.persist(simpleComment);
        entityManager.close();
        return simpleComment;
    }

    public SimpleComment getCommentById(String Id)
    {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        SimpleComment simpleComment = entityManager.find(SimpleComment.class, Id);
        return simpleComment;
    }

    public List<SimpleComment> getAllComments()
    {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        Query query = entityManager.createQuery("SELECT c from SimpleComment c");
        List<SimpleComment> list = query.getResultList();
        return list;
    }

    public EntityManagerFactory getEntityManagerFactory()
    {
        return entityManagerFactory;
    }

    public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory)
    {
        this.entityManagerFactory = entityManagerFactory;
    }
}

Running Code Sample

Create Keysapce and Column family in Cassandra:

impadmin@impetus-ubuntu:/usr/local/apache-cassandra-1.0.6/bin$ ./cassandra-cli --host localhost --port 9160
Connected to: "Test Cluster" on localhost/9160
Welcome to Cassandra CLI version 1.0.6

Type 'help;' or '?' for help.
Type 'quit;' or 'exit;' to quit.

[default@unknown] create keyspace KunderaExamples;
e1803920-5953-11e1-0000-242d50cf1fff
Waiting for schema agreement...
... schemas agree across the cluster
[default@unknown] use KunderaExamples;
Authenticated to keyspace: KunderaExamples
[default@KunderaExamples] create column family SIMPLE_COMMENT with comparator=UTF8Type and default_validation_class=UTF8Type and key_validation_class=UTF8Type and column_metadata=[{column_name: USER_NAME, validation_class:UTF8Type, index_type: KEYS},{column_name: COMMENT_TEXT, validation_class:UTF8Type, index_type: KEYS}];
333dab30-5954-11e1-0000-242d50cf1fff
Waiting for schema agreement...
... schemas agree across the cluster
[default@KunderaExamples] 

Run Java Code:

XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
dao = (SpringExampleDao) beanFactory.getBean("springExampleDao"); 

//Add simple comment
dao.addComment(1, "xamry", "No comment!");

//Find a simple comment
System.out.println(dao.getCommentById("1"));

//Get all comments
System.out.println(dao.getAllComments());

Verify Results:

[default@KunderaExamples] list SIMPLE_COMMENT;
Using default limit of 100
-------------------
RowKey: 1
=> (column=COMMENT_TEXT, value=No comment!, timestamp=1329475553576)
=> (column=USER_NAME, value=xamry, timestamp=1329475553576)

1 Row Returned.
Elapsed time: 156 msec(s).
[default@KunderaExamples] 

Home

Clone this wiki locally