final String userId = "1";
final int tweetId = 12;
final UUID timeLineId = UUID.randomUUID();
final Date tweetDate = new Date();
CouchDBCompoundKey compoundKey = new CouchDBCompoundKey(userId, tweetId, timeLineId);
CouchDBPrimeUser user = new CouchDBPrimeUser(compoundKey);
user.setTweetBody("My tweet");
user.setTweetDate(tweetDate);
em.persist(user);
em.clear(); // clear cache.
javax.persistence.Query q = em.createQuery("select u from CouchDBPrimeUser u where u.key=:compoundKey");
q.setParameter("compoundKey", compoundKey);
List<CouchDBPrimeUser> users = q.getResultList();
Assert.assertNotNull(users);
Assert.assertEquals(1, users.size());
em.clear(); // clear cache.
final String withAllCompositeColClause = "Select u from CouchDBPrimeUser u where u.key.user_Id = :userId and u.key.tweetId = :tweetId and u.key.timeLineId = :timeLineId";
q = em.createQuery(withAllCompositeColClause);
q.setParameter("userId", "1");
q.setParameter("tweetId", 12);
q.setParameter("timeLineId", timeLineId);
users = q.getResultList();
Assert.assertNotNull(users);
Assert.assertEquals(1, users.size());
try
{
final String withOutAllCompositeColClause = "Select u from CouchDBPrimeUser u where u.key.user_Id = :userId and u.key.tweetId = :tweetId";
q = em.createQuery(withOutAllCompositeColClause);
q.setParameter("userId", "1");
q.setParameter("tweetId", 12);
users = q.getResultList();
}
catch (Exception e)
{
Assert.assertEquals("com.impetus.kundera.query.QueryHandlerException: There should be each and every field of composite key.", e.getMessage());
}
CouchDBPrimeUser found = em.find(CouchDBPrimeUser.class, compoundKey);
Assert.assertNotNull(found);
Assert.assertNotNull(found.getKey());
Assert.assertEquals(userId, found.getKey().getUserId());
Assert.assertEquals(tweetDate, found.getTweetDate());
em.remove(found);
em.clear(); // clear cache.
found = em.find(CouchDBPrimeUser.class, compoundKey);