diff --git a/spring-data-cassandra/.classpath b/spring-data-cassandra/.classpath
new file mode 100644
index 000000000000..698778fef337
--- /dev/null
+++ b/spring-data-cassandra/.classpath
@@ -0,0 +1,31 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/spring-data-cassandra/.project b/spring-data-cassandra/.project
new file mode 100644
index 000000000000..239fa4f00275
--- /dev/null
+++ b/spring-data-cassandra/.project
@@ -0,0 +1,29 @@
+
+
+ spring-data-cassandra
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+ org.eclipse.m2e.core.maven2Builder
+
+
+
+
+ org.springframework.ide.eclipse.core.springbuilder
+
+
+
+
+
+ org.springframework.ide.eclipse.core.springnature
+ org.eclipse.jdt.core.javanature
+ org.eclipse.m2e.core.maven2Nature
+
+
diff --git a/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/config/CassandraConfig.java b/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/config/CassandraConfig.java
index 5f2c4c6d477d..2edd5551a5a7 100644
--- a/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/config/CassandraConfig.java
+++ b/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/config/CassandraConfig.java
@@ -17,8 +17,8 @@
@PropertySource(value = { "classpath:cassandra.properties" })
@EnableCassandraRepositories(basePackages = "org.baeldung.spring.data.cassandra.repository")
public class CassandraConfig extends AbstractCassandraConfiguration {
-
private static final Log LOGGER = LogFactory.getLog(CassandraConfig.class);
+
@Autowired
private Environment environment;
@@ -27,15 +27,17 @@ protected String getKeyspaceName() {
return environment.getProperty("cassandra.keyspace");
}
+ @Override
@Bean
public CassandraClusterFactoryBean cluster() {
- CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
+ final CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
cluster.setContactPoints(environment.getProperty("cassandra.contactpoints"));
cluster.setPort(Integer.parseInt(environment.getProperty("cassandra.port")));
LOGGER.info("Cluster created with contact points [" + environment.getProperty("cassandra.contactpoints") + "] " + "& port [" + Integer.parseInt(environment.getProperty("cassandra.port")) + "].");
return cluster;
}
+ @Override
@Bean
public CassandraMappingContext cassandraMapping() throws ClassNotFoundException {
return new BasicCassandraMappingContext();
diff --git a/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/model/Book.java b/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/model/Book.java
index 6c099d99bc7b..a8ec81d6b583 100644
--- a/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/model/Book.java
+++ b/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/model/Book.java
@@ -1,28 +1,31 @@
package org.baeldung.spring.data.cassandra.model;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.UUID;
+
import org.springframework.cassandra.core.Ordering;
import org.springframework.cassandra.core.PrimaryKeyType;
import org.springframework.data.cassandra.mapping.Column;
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.mapping.Table;
-import java.util.HashSet;
-import java.util.Set;
-import java.util.UUID;
-
@Table
public class Book {
+
@PrimaryKeyColumn(name = "id", ordinal = 0, type = PrimaryKeyType.CLUSTERED, ordering = Ordering.DESCENDING)
private UUID id;
+
@PrimaryKeyColumn(name = "title", ordinal = 1, type = PrimaryKeyType.PARTITIONED)
private String title;
@PrimaryKeyColumn(name = "publisher", ordinal = 2, type = PrimaryKeyType.PARTITIONED)
private String publisher;
+
@Column
private Set tags = new HashSet<>();
- public Book(UUID id, String title, String publisher, Set tags) {
+ public Book(final UUID id, final String title, final String publisher, final Set tags) {
this.id = id;
this.title = title;
this.publisher = publisher;
@@ -45,19 +48,20 @@ public Set getTags() {
return tags;
}
- public void setId(UUID id) {
+ public void setId(final UUID id) {
this.id = id;
}
- public void setTitle(String title) {
+ public void setTitle(final String title) {
this.title = title;
}
- public void setPublisher(String publisher) {
+ public void setPublisher(final String publisher) {
this.publisher = publisher;
}
- public void setTags(Set tags) {
+ public void setTags(final Set tags) {
this.tags = tags;
}
+
}
diff --git a/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/repository/BookRepository.java b/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/repository/BookRepository.java
index e37ae78b595b..66d656ac3a0f 100644
--- a/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/repository/BookRepository.java
+++ b/spring-data-cassandra/src/main/java/org/baeldung/spring/data/cassandra/repository/BookRepository.java
@@ -7,6 +7,8 @@
@Repository
public interface BookRepository extends CassandraRepository {
+
@Query("select * from book where title = ?0 and publisher=?1")
Iterable findByTitleAndPublisher(String title, String publisher);
+
}
diff --git a/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/BookRepositoryIntegrationTest.java b/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/BookRepositoryIntegrationTest.java
index e5a723714531..8cbcdc3195ac 100644
--- a/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/BookRepositoryIntegrationTest.java
+++ b/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/BookRepositoryIntegrationTest.java
@@ -1,9 +1,11 @@
package org.baeldung.spring.data.cassandra.repository;
-import com.datastax.driver.core.Cluster;
-import com.datastax.driver.core.Session;
-import com.datastax.driver.core.utils.UUIDs;
-import com.google.common.collect.ImmutableSet;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+
+import java.io.IOException;
+import java.util.HashMap;
+
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -11,7 +13,11 @@
import org.baeldung.spring.data.cassandra.config.CassandraConfig;
import org.baeldung.spring.data.cassandra.model.Book;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
-import org.junit.*;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cassandra.core.cql.CqlIdentifier;
@@ -19,16 +25,14 @@
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-import java.io.IOException;
-import java.util.HashMap;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotEquals;
+import com.datastax.driver.core.Cluster;
+import com.datastax.driver.core.Session;
+import com.datastax.driver.core.utils.UUIDs;
+import com.google.common.collect.ImmutableSet;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CassandraConfig.class)
public class BookRepositoryIntegrationTest {
-
private static final Log LOGGER = LogFactory.getLog(BookRepositoryIntegrationTest.class);
public static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };";
@@ -43,13 +47,14 @@ public class BookRepositoryIntegrationTest {
@Autowired
private CassandraAdminOperations adminTemplate;
+ //
+
@BeforeClass
public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException {
EmbeddedCassandraServerHelper.startEmbeddedCassandra();
- Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1")
- .withPort(9142).build();
+ final Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142).build();
LOGGER.info("Server Started at 127.0.0.1:9142... ");
- Session session = cluster.connect();
+ final Session session = cluster.connect();
session.execute(KEYSPACE_CREATION_QUERY);
session.execute(KEYSPACE_ACTIVATE_QUERY);
LOGGER.info("KeySpace created and activated.");
@@ -63,54 +68,54 @@ public void createTable() throws InterruptedException, TTransportException, Conf
@Test
public void whenSavingBook_thenAvailableOnRetrieval() {
- Book javaBook = new Book(UUIDs.timeBased(), "Head First Java",
- "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
+ final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
bookRepository.save(ImmutableSet.of(javaBook));
- Iterable books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media");
+ final Iterable books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media");
assertEquals(javaBook.getId(), books.iterator().next().getId());
}
@Test
public void whenUpdatingBooks_thenAvailableOnRetrieval() {
- Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
+ final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
bookRepository.save(ImmutableSet.of(javaBook));
- Iterable books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media");
+ final Iterable books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media");
javaBook.setTitle("Head First Java Second Edition");
bookRepository.save(ImmutableSet.of(javaBook));
- Iterable updateBooks = bookRepository.findByTitleAndPublisher("Head First Java Second Edition", "O'Reilly Media");
+ final Iterable updateBooks = bookRepository.findByTitleAndPublisher("Head First Java Second Edition", "O'Reilly Media");
assertEquals(javaBook.getTitle(), updateBooks.iterator().next().getTitle());
}
@Test(expected = java.util.NoSuchElementException.class)
public void whenDeletingExistingBooks_thenNotAvailableOnRetrieval() {
- Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
+ final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
bookRepository.save(ImmutableSet.of(javaBook));
bookRepository.delete(javaBook);
- Iterable books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media");
+ final Iterable books = bookRepository.findByTitleAndPublisher("Head First Java", "O'Reilly Media");
assertNotEquals(javaBook.getId(), books.iterator().next().getId());
}
@Test
public void whenSavingBooks_thenAllShouldAvailableOnRetrieval() {
- Book javaBook = new Book(UUIDs.timeBased(), "Head First Java",
- "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
- Book dPatternBook = new Book(UUIDs.timeBased(), "Head Design Patterns",
- "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
+ final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
+ final Book dPatternBook = new Book(UUIDs.timeBased(), "Head Design Patterns", "O'Reilly Media", ImmutableSet.of("Computer", "Software"));
bookRepository.save(ImmutableSet.of(javaBook));
bookRepository.save(ImmutableSet.of(dPatternBook));
- Iterable books = bookRepository.findAll();
+ final Iterable books = bookRepository.findAll();
int bookCount = 0;
- for (Book book : books) bookCount++;
+ for (final Book book : books) {
+ bookCount++;
+ }
assertEquals(bookCount, 2);
}
@After
public void dropTable() {
- adminTemplate.dropTable(CqlIdentifier.cqlId(DATA_TABLE_NAME));
+ adminTemplate.dropTable(CqlIdentifier.cqlId(DATA_TABLE_NAME));
}
@AfterClass
public static void stopCassandraEmbedded() {
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
}
+
}
diff --git a/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/CQLQueriesIntegrationTest.java b/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/CQLQueriesIntegrationTest.java
index 031b5c0b6f1b..584d5f868b5c 100644
--- a/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/CQLQueriesIntegrationTest.java
+++ b/spring-data-cassandra/src/test/java/org/baeldung/spring/data/cassandra/repository/CQLQueriesIntegrationTest.java
@@ -1,12 +1,13 @@
package org.baeldung.spring.data.cassandra.repository;
-import com.datastax.driver.core.Cluster;
-import com.datastax.driver.core.Session;
-import com.datastax.driver.core.querybuilder.Insert;
-import com.datastax.driver.core.querybuilder.QueryBuilder;
-import com.datastax.driver.core.querybuilder.Select;
-import com.datastax.driver.core.utils.UUIDs;
-import com.google.common.collect.ImmutableSet;
+import static junit.framework.TestCase.assertEquals;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.UUID;
+
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -14,7 +15,11 @@
import org.baeldung.spring.data.cassandra.config.CassandraConfig;
import org.baeldung.spring.data.cassandra.model.Book;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
-import org.junit.*;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cassandra.core.cql.CqlIdentifier;
@@ -23,18 +28,17 @@
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.UUID;
-
-import static junit.framework.TestCase.assertEquals;
+import com.datastax.driver.core.Cluster;
+import com.datastax.driver.core.Session;
+import com.datastax.driver.core.querybuilder.Insert;
+import com.datastax.driver.core.querybuilder.QueryBuilder;
+import com.datastax.driver.core.querybuilder.Select;
+import com.datastax.driver.core.utils.UUIDs;
+import com.google.common.collect.ImmutableSet;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CassandraConfig.class)
public class CQLQueriesIntegrationTest {
-
private static final Log LOGGER = LogFactory.getLog(CQLQueriesIntegrationTest.class);
public static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace " + "WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };";
@@ -49,12 +53,14 @@ public class CQLQueriesIntegrationTest {
@Autowired
private CassandraOperations cassandraTemplate;
+ //
+
@BeforeClass
public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException {
EmbeddedCassandraServerHelper.startEmbeddedCassandra(25000);
- Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142).build();
+ final Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142).build();
LOGGER.info("Server Started at 127.0.0.1:9142... ");
- Session session = cluster.connect();
+ final Session session = cluster.connect();
session.execute(KEYSPACE_CREATION_QUERY);
session.execute(KEYSPACE_ACTIVATE_QUERY);
LOGGER.info("KeySpace created and activated.");
@@ -68,40 +74,40 @@ public void createTable() throws InterruptedException, TTransportException, Conf
@Test
public void whenSavingBook_thenAvailableOnRetrieval_usingQueryBuilder() {
- UUID uuid = UUIDs.timeBased();
- Insert insert = QueryBuilder.insertInto(DATA_TABLE_NAME).value("id", uuid).value("title", "Head First Java").value("publisher", "OReilly Media").value("tags", ImmutableSet.of("Software"));
+ final UUID uuid = UUIDs.timeBased();
+ final Insert insert = QueryBuilder.insertInto(DATA_TABLE_NAME).value("id", uuid).value("title", "Head First Java").value("publisher", "OReilly Media").value("tags", ImmutableSet.of("Software"));
cassandraTemplate.execute(insert);
- Select select = QueryBuilder.select().from("book").limit(10);
- Book retrievedBook = cassandraTemplate.selectOne(select, Book.class);
+ final Select select = QueryBuilder.select().from("book").limit(10);
+ final Book retrievedBook = cassandraTemplate.selectOne(select, Book.class);
assertEquals(uuid, retrievedBook.getId());
}
@Test
public void whenSavingBook_thenAvailableOnRetrieval_usingCQLStatements() {
- UUID uuid = UUIDs.timeBased();
- String insertCql = "insert into book (id, title, publisher, tags) values " + "(" + uuid + ", 'Head First Java', 'OReilly Media', {'Software'})";
+ final UUID uuid = UUIDs.timeBased();
+ final String insertCql = "insert into book (id, title, publisher, tags) values " + "(" + uuid + ", 'Head First Java', 'OReilly Media', {'Software'})";
cassandraTemplate.execute(insertCql);
- Select select = QueryBuilder.select().from("book").limit(10);
- Book retrievedBook = cassandraTemplate.selectOne(select, Book.class);
+ final Select select = QueryBuilder.select().from("book").limit(10);
+ final Book retrievedBook = cassandraTemplate.selectOne(select, Book.class);
assertEquals(uuid, retrievedBook.getId());
}
@Test
public void whenSavingBook_thenAvailableOnRetrieval_usingPreparedStatements() throws InterruptedException {
- UUID uuid = UUIDs.timeBased();
- String insertPreparedCql = "insert into book (id, title, publisher, tags) values (?, ?, ?, ?)";
- List