Skip to content

ElementCollection of Basic Types

Amresh edited this page Aug 6, 2013 · 2 revisions

Kundera supports ElementCollection of both Embeddable and Basic types. In this example, we'll demonstrate ElementCollection being used for storing things into Cassandra collection types.

Entity

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import com.impetus.kundera.index.Index;
import com.impetus.kundera.index.IndexCollection;

/**
 * Entity class for Blog post
 */
@Entity
@Table(name="blog_posts", schema="KunderaExamples@secIdxCassandraTest")
@IndexCollection(columns = { @Index(name = "body")})
public class BlogPost
{    
    @Id
    @Column(name="post_id")
    private int postId;
    
    //Body of the post
    @Column(name="body")
    private String body;       
    
    //Useful tags specified by author
    @ElementCollection
    @Column(name="tags")
    private Set<String> tags;  
    
    //List of user IDs who liked this blog post
    @ElementCollection
    @Column(name="liked_by")
    private List<Integer> likedBy;   
    
    //User IDs and their respective comments on this blog
    @ElementCollection
    @Column(name="comments")
    private Map<Integer, String> comments; 
    
    //Getters, setters and constructors    
}

CRUD

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.impetus.client.cassandra.common.CassandraConstants;
import com.impetus.client.persistence.CassandraCli;

/**
 * Demonstrates Storing and retrieving Blog posts. 1. Validates correct
 * functioning of Element collection for basic types 2. Checks all types of
 * collection (Set, Map, List)
 */
public class BlogPostTest
{
    EntityManagerFactory emf;

    EntityManager em;

    String persistenceUnit = "secIdxCassandraTest";

    private boolean RUN_IN_EMBEDDED_MOODE = true;
    private boolean AUTO_MANAGE_SCHEMA = true;

    /**
     * @throws java.lang.Exception
     */
    @Before
    public void setUp() throws Exception
    {
        if (RUN_IN_EMBEDDED_MOODE)
        {
            CassandraCli.cassandraSetUp();
        }

        if (AUTO_MANAGE_SCHEMA)
        {

            createKeyspace();            
            createColumnFamily();
        }

        emf = Persistence.createEntityManagerFactory(persistenceUnit);
        em = emf.createEntityManager();
        em.setProperty(CassandraConstants.CQL_VERSION, CassandraConstants.CQL_VERSION_3_0);
    }

    

    // @Test
    public void dummyTest()
    {

    }

    @Test
    public void testCRUD()
    {
        // Insert records
        BlogPost p1 = prepareBlogPost1();
        BlogPost p2 = prepareBlogPost2();

        em.persist(p1);
        em.persist(p2);

        // Find records by ID
        em.clear();
        BlogPost pp1 = em.find(BlogPost.class, 1);
        BlogPost pp2 = em.find(BlogPost.class, 2);

        assertPost1(pp1);
        assertPost2(pp2);

        // Update records
        modifyBlogPost1(pp1);
        modifyBlogPost2(pp2);

        em.merge(pp1);
        em.merge(pp2);

        em.clear();
        pp1 = em.find(BlogPost.class, 1);
        pp2 = em.find(BlogPost.class, 2);

        assertUpdatedPost1(pp1);
        assertUpdatedPost2(pp2);

        // Remove records
        em.remove(pp1);
        em.remove(pp2);

        em.clear();
        pp1 = em.find(BlogPost.class, 1);
        pp2 = em.find(BlogPost.class, 2);

        Assert.assertNull(pp1);
        Assert.assertNull(pp2);
    }

    @Test
    public void testJPAQuery()
    {
        // Insert records
        BlogPost p1 = prepareBlogPost1();
        BlogPost p2 = prepareBlogPost2();

        em.persist(p1);
        em.persist(p2);

        // Select All query
        Query q = em.createQuery("Select p from BlogPost p");
        List<BlogPost> allPosts = q.getResultList();
        Assert.assertNotNull(allPosts);
        Assert.assertFalse(allPosts.isEmpty());
        Assert.assertEquals(2, allPosts.size());
        assertPost1(allPosts.get(0));
        assertPost2(allPosts.get(1));

        // Search over Row ID
        q = em.createQuery("Select p from BlogPost p where p.postId=:postId");
        q.setParameter("postId", 1);
        allPosts = q.getResultList();
        Assert.assertNotNull(allPosts);
        Assert.assertFalse(allPosts.isEmpty());
        Assert.assertEquals(1, allPosts.size());
        assertPost1(allPosts.get(0));

        // Search over Body column
        q = em.createQuery("Select p from BlogPost p where p.body=:body");
        q.setParameter("body", "Kundera - Knight in the shining armor!");
        allPosts = q.getResultList();
        Assert.assertNotNull(allPosts);
        Assert.assertFalse(allPosts.isEmpty());
        Assert.assertEquals(1, allPosts.size());
        assertPost2(allPosts.get(0));

        // Update Query
        q = em.createQuery("update BlogPost p set p.body=:body,p.tags=:tags,p.likedBy=:likedBy,p.comments=:comments where p.postId=1");
        modifyBlogPost1(p1);
        q.setParameter("body", p1.getBody());
        q.setParameter("tags", p1.getTags());
        q.setParameter("likedBy", p1.getLikedBy());
        q.setParameter("comments", p1.getComments());
        int updatedRecords = q.executeUpdate();
        Assert.assertEquals(1, updatedRecords);

        em.clear();
        q = em.createQuery("Select p from BlogPost p where p.postId=:postId");
        q.setParameter("postId", 1);
        allPosts = q.getResultList();
        Assert.assertNotNull(allPosts);
        Assert.assertFalse(allPosts.isEmpty());
        Assert.assertEquals(1, allPosts.size());
        assertUpdatedPost1(allPosts.get(0));

        // Delete Query
        q = em.createQuery("DELETE from BlogPost");
        int deleteCount = q.executeUpdate();
        Assert.assertEquals(2, deleteCount);

        em.clear();
        q = em.createQuery("Select p from BlogPost p");
        allPosts = q.getResultList();
        Assert.assertTrue(allPosts == null || allPosts.isEmpty());
    }

    @Test
    public void testNativeQuery()
    {
        // Insert records
        BlogPost p1 = prepareBlogPost1();
        BlogPost p2 = prepareBlogPost2();

        em.persist(p1);
        em.persist(p2);

        // Select All query
        Query q = em.createNativeQuery("select * from blog_posts", BlogPost.class);
        List<BlogPost> allPosts = q.getResultList();
        Assert.assertNotNull(allPosts);
        Assert.assertFalse(allPosts.isEmpty());
        Assert.assertEquals(2, allPosts.size());
        assertPost1(allPosts.get(0));
        assertPost2(allPosts.get(1));

        // Search over a column
        q = em.createNativeQuery("select * from blog_posts where body='Working with MongoDB using Kundera'",
                BlogPost.class);
        allPosts = q.getResultList();
        Assert.assertNotNull(allPosts);
        Assert.assertFalse(allPosts.isEmpty());
        Assert.assertEquals(1, allPosts.size());
        assertPost1(allPosts.get(0));

        // Updating set, list and map for Blog Post 1
        q = em.createNativeQuery("update blog_posts set body = 'Updated body 1' where post_id = 1", BlogPost.class);
        q.executeUpdate();
        q = em.createNativeQuery("update blog_posts set tags = tags + {'new tag 1'} where post_id = 1", BlogPost.class);
        q.executeUpdate();
        q = em.createNativeQuery("update blog_posts set liked_by = liked_by - [111] where post_id = 1", BlogPost.class);
        q.executeUpdate();
        q = em.createNativeQuery("update blog_posts set comments = comments + {888:'New comment 1'} where post_id = 1",
                BlogPost.class);
        q.executeUpdate();

        q = em.createNativeQuery("select * from blog_posts where post_id = 1", BlogPost.class);
        allPosts = q.getResultList();
        Assert.assertNotNull(allPosts);
        Assert.assertFalse(allPosts.isEmpty());
        Assert.assertEquals(1, allPosts.size());
        assertUpdatedPost1(allPosts.get(0));

        // Updating set, list and map for Blog Post 2
        q = em.createNativeQuery("update blog_posts set body = 'Updated body 2' where post_id = 2", BlogPost.class);
        q.executeUpdate();
        q = em.createNativeQuery("update blog_posts set tags = tags + {'new tag 2'} where post_id = 2", BlogPost.class);
        q.executeUpdate();
        q = em.createNativeQuery("update blog_posts set liked_by = liked_by - [444] where post_id = 2", BlogPost.class);
        q.executeUpdate();
        q = em.createNativeQuery("update blog_posts set comments = comments + {999:'New comment 2'} where post_id = 2",
                BlogPost.class);
        q.executeUpdate();

        q = em.createNativeQuery("select * from blog_posts where post_id = 2", BlogPost.class);
        allPosts = q.getResultList();
        Assert.assertNotNull(allPosts);
        Assert.assertFalse(allPosts.isEmpty());
        Assert.assertEquals(1, allPosts.size());
        assertUpdatedPost2(allPosts.get(0));

        // Delete all posts
        q = em.createNativeQuery("delete from blog_posts where post_id = 1", BlogPost.class);
        q.executeUpdate();
        q = em.createNativeQuery("delete from blog_posts where post_id = 2", BlogPost.class);
        q.executeUpdate();

        q = em.createNativeQuery("select * from blog_posts", BlogPost.class);
        allPosts = q.getResultList();
        Assert.assertTrue(allPosts == null || allPosts.isEmpty());
    }

    private BlogPost prepareBlogPost1()
    {
        BlogPost p1 = new BlogPost();
        p1.setPostId(1);
        p1.setBody("Working with MongoDB using Kundera");

        p1.addTag("nosql");
        p1.addTag("kundera");
        p1.addTag("mongo");

        p1.addLikedBy(111);
        p1.addLikedBy(222);

        p1.addComment(111, "What a post!");
        p1.addComment(222, "I am getting NPE on line no. 145");
        p1.addComment(333, "My hobby is to spam blogs");
        return p1;
    }

    private BlogPost prepareBlogPost2()
    {
        BlogPost p2 = new BlogPost();
        p2.setPostId(2);
        p2.setBody("Kundera - Knight in the shining armor!");

        p2.addTag("nosql");
        p2.addTag("cassandra");
        p2.addTag("kundera");
        p2.addTag("jpa");

        p2.addLikedBy(333);
        p2.addLikedBy(444);
        p2.addLikedBy(555);

        p2.addComment(333, "Great work");
        p2.addComment(555, "Doesn't work on my machine");
        p2.addComment(777, "Wanna buy medicines from my store?");
        return p2;
    }

    private void modifyBlogPost1(BlogPost p)
    {
        p.setBody("Updated body 1");
        p.getTags().add("new tag 1");
        p.getLikedBy().remove(0);
        p.addComment(888, "New comment 1");
    }

    private void modifyBlogPost2(BlogPost p)
    {
        p.setBody("Updated body 2");
        p.getTags().add("new tag 2");
        p.getLikedBy().remove(1);
        p.addComment(999, "New comment 2");
    }

    private void assertPost1(BlogPost p)
    {
        Assert.assertNotNull(p);
        Assert.assertEquals(1, p.getPostId());
        Assert.assertEquals("Working with MongoDB using Kundera", p.getBody());

        Assert.assertNotNull(p.getTags());
        Assert.assertFalse(p.getTags().isEmpty());
        Assert.assertEquals(3, p.getTags().size());
        for (String tag : p.getTags())
        {
            Assert.assertTrue(tag.equals("nosql") || tag.equals("kundera") || tag.equals("mongo"));
        }

        Assert.assertNotNull(p.getLikedBy());
        Assert.assertFalse(p.getLikedBy().isEmpty());
        Assert.assertEquals(2, p.getLikedBy().size());
        for (int likedUserId : p.getLikedBy())
        {
            Assert.assertTrue(likedUserId == 111 || likedUserId == 222);
        }

        Assert.assertNotNull(p.getComments());
        Assert.assertFalse(p.getComments().isEmpty());
        Assert.assertEquals(3, p.getComments().size());
        for (int commentedBy : p.getComments().keySet())
        {
            String commentText = p.getComments().get(commentedBy);
            Assert.assertTrue(commentedBy == 111 || commentedBy == 222 || commentedBy == 333);
            Assert.assertTrue(commentText.equals("What a post!")
                    || commentText.equals("I am getting NPE on line no. 145")
                    || commentText.equals("My hobby is to spam blogs"));
        }
    }

    private void assertPost2(BlogPost p)
    {
        Assert.assertNotNull(p);
        Assert.assertEquals(2, p.getPostId());
        Assert.assertEquals("Kundera - Knight in the shining armor!", p.getBody());

        Assert.assertNotNull(p.getTags());
        Assert.assertFalse(p.getTags().isEmpty());
        Assert.assertEquals(4, p.getTags().size());
        for (String tag : p.getTags())
        {
            Assert.assertTrue(tag.equals("nosql") || tag.equals("cassandra") || tag.equals("kundera")
                    || tag.equals("jpa"));
        }

        Assert.assertNotNull(p.getLikedBy());
        Assert.assertFalse(p.getLikedBy().isEmpty());
        Assert.assertEquals(3, p.getLikedBy().size());
        for (int likedUserId : p.getLikedBy())
        {
            Assert.assertTrue(likedUserId == 333 || likedUserId == 444 || likedUserId == 555);
        }

        Assert.assertNotNull(p.getComments());
        Assert.assertFalse(p.getComments().isEmpty());
        Assert.assertEquals(3, p.getComments().size());
        for (int commentedBy : p.getComments().keySet())
        {
            String commentText = p.getComments().get(commentedBy);
            Assert.assertTrue(commentedBy == 333 || commentedBy == 555 || commentedBy == 777);
            Assert.assertTrue(commentText.equals("Great work") || commentText.equals("Doesn't work on my machine")
                    || commentText.equals("Wanna buy medicines from my store?"));
        }
    }

    private void assertUpdatedPost1(BlogPost p)
    {
        Assert.assertNotNull(p);
        Assert.assertEquals(1, p.getPostId());
        Assert.assertEquals("Updated body 1", p.getBody());

        Assert.assertNotNull(p.getTags());
        Assert.assertFalse(p.getTags().isEmpty());
        Assert.assertEquals(4, p.getTags().size());
        for (String tag : p.getTags())
        {
            Assert.assertTrue(tag.equals("nosql") || tag.equals("kundera") || tag.equals("mongo")
                    || tag.equals("new tag 1"));
        }

        Assert.assertNotNull(p.getLikedBy());
        Assert.assertFalse(p.getLikedBy().isEmpty());
        Assert.assertEquals(1, p.getLikedBy().size());
        for (int likedUserId : p.getLikedBy())
        {
            Assert.assertTrue(likedUserId == 222);
        }

        Assert.assertNotNull(p.getComments());
        Assert.assertFalse(p.getComments().isEmpty());
        Assert.assertEquals(4, p.getComments().size());
        for (int commentedBy : p.getComments().keySet())
        {
            String commentText = p.getComments().get(commentedBy);
            Assert.assertTrue(commentedBy == 111 || commentedBy == 222 || commentedBy == 333 || commentedBy == 888);
            Assert.assertTrue(commentText.equals("What a post!")
                    || commentText.equals("I am getting NPE on line no. 145")
                    || commentText.equals("My hobby is to spam blogs") || commentText.equals("New comment 1"));
        }

    }

    private void assertUpdatedPost2(BlogPost p)
    {
        Assert.assertNotNull(p);
        Assert.assertEquals(2, p.getPostId());
        Assert.assertEquals("Updated body 2", p.getBody());

        Assert.assertNotNull(p.getTags());
        Assert.assertFalse(p.getTags().isEmpty());
        Assert.assertEquals(5, p.getTags().size());
        for (String tag : p.getTags())
        {
            Assert.assertTrue(tag.equals("nosql") || tag.equals("cassandra") || tag.equals("kundera")
                    || tag.equals("jpa") || tag.equals("new tag 2"));
        }

        Assert.assertNotNull(p.getLikedBy());
        Assert.assertFalse(p.getLikedBy().isEmpty());
        Assert.assertEquals(2, p.getLikedBy().size());
        for (int likedUserId : p.getLikedBy())
        {
            Assert.assertTrue(likedUserId == 333 || likedUserId == 555);
        }

        Assert.assertNotNull(p.getComments());
        Assert.assertFalse(p.getComments().isEmpty());
        Assert.assertEquals(4, p.getComments().size());
        for (int commentedBy : p.getComments().keySet())
        {
            String commentText = p.getComments().get(commentedBy);
            Assert.assertTrue(commentedBy == 333 || commentedBy == 555 || commentedBy == 777 || commentedBy == 999);
            Assert.assertTrue(commentText.equals("Great work") || commentText.equals("Doesn't work on my machine")
                    || commentText.equals("Wanna buy medicines from my store?") || commentText.equals("New comment 2"));
        }
    }

    /**
     * @throws java.lang.Exception
     */
    @After
    public void tearDown() throws Exception
    {
        if (em != null && em.isOpen())
        {
            em.close();
        }
        if (emf != null && emf.isOpen())
        {
            emf.close();
        }

        if (AUTO_MANAGE_SCHEMA)
        {
            CassandraCli.executeCqlQuery("USE \"KunderaExamples\"");
            CassandraCli.executeCqlQuery("TRUNCATE blog_posts");
            CassandraCli.executeCqlQuery("DROP TABLE blog_posts");
            CassandraCli.executeCqlQuery("DROP KEYSPACE \"KunderaExamples\"");
        }

    }
    
    private void createColumnFamily()
    {
        try
        {
            CassandraCli.executeCqlQuery("USE \"KunderaExamples\"");
            CassandraCli
                    .executeCqlQuery("CREATE TABLE blog_posts (post_id int PRIMARY KEY, body text, tags set<text>, liked_by list<int>, comments map<int, text>)");
            CassandraCli.executeCqlQuery("CREATE INDEX ON blog_posts(body)");
        }
        catch (Exception e)
        {                
        }
    }

    private void createKeyspace()
    {
        try
        {
            CassandraCli
                    .executeCqlQuery("CREATE KEYSPACE \"KunderaExamples\" WITH replication = {'class':'SimpleStrategy','replication_factor':3}");
        }
        catch (Exception e)
        {

        }
    }

Home

Clone this wiki locally