-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Karthik Sankar edited this page Mar 23, 2016
·
64 revisions
- Store EntityManager as a static variable at the application level.
- Have a separate entity manager for each, keyspace.
- You can connect to SSL enabled nodes.
- Each property of createEntityManager is documented below.
public class App{
private static CqlEntityManager em;
public static void init(){
Properties props=new Properties();
//cql.contactpoints: if you have multiple nodes, specify their ip addresses separated by comma.
// example: 111.222.222.1,111.222.222.2,111.222.222.3"
props.put("cql.contactpoints", "localhost");
props.put("cql.keyspace","jom_test");
//cql.synctableschema: If true, the table schema will be auto created, otherwise schema will be untouched.
// Any new fields added will be synced. But, it can not delete columns.
props.put("cql.synctableschema", "true");
//cql.packagestoscan Specify all your packages that contain your entity objects.
//Annotate your domain objects with @CqlEntity annotation.
props.put("cql.packagestoscan", "org.w3cloud.jom.testmodels");
//Following properties are only required if you clusters communicate using SSL.
props.put("cql.ssl", "true");
props.put("cql.ssl.truststorePath", "/full/path/.truststore"); //Truststore file name with full path
props.put("cql.ssl.truststorePassword", "password");
props.put("cql.ssl.keystorePath", "/full/path/.keystore");
props.put("cql.ssl.keystorePassword", "yourpassword");
em=CqlEntityManagerFactory.createEntityManger(props);
}
}
####@CqlEntity Annotate your domain objects with @CqlEntity annotation. JOM will create schema, if it does not exist. If the schema exists, it compares and adds the missing columns.
@CqlEntity
public class CarModel {
....
}
####@CqlId
- Partition key field is annotated with CqlId(idType=IdType.PARTITION_KEY) annotation.
- Cluster key field is annotated with CqlId(idType=IdType.CLUSTER_KEY) annotation.
- More than one field can be annotated with this annotation to make a composite key.
####@CqlAutoGen
- Any UUID field can be annotated with @CqlAutoGen
- JOM auto populates such ID fields with a GUID value.
####@CqlTransient
- Any in-memory variables that you do not want to persist in the database, mark it with @CqlTransit annotation.
####@CqlEmbed
- Do not annotate embedded (eg. Address) object with @CqlEntity.
- JOM create fields for each embedded object.
- Field naming convention: deliver_address__city.
@CqlEntity
public class Customer{
.....
@CqlEmbed
public Address deliveryAddress;
...
}
####@CqlStoreAsJson
- List or array of String can be stored as json
@CqlStoreAsJson public List\ phoneNos;
- @CqlStoreAsJson does not work well with list of Objects like List of Addresses.
- For such situations, use String addressListJson to store json and Serialize and Load manually.
@JsonIgnore public String addressesJson; @CqlTransient public List\ addresses;
- int, long, boolean, String.
- java.util.Date
- UUID
- java.math.BigDecimal
CqlEntityManager is the core interface that your DAO will be using to perform the CRUD operations.
public interface CqlEntityManager {
//Insert Methods
void insert(Object entity);
void insert(BatchStatement batchStatement, Object entity; //Insert in batch.
//Update Methods
void update(Object entity);
void update(BatchStatement batchStatement, Object entity);
void updateColumn(T entity, CqlStatement statement);
void updateColumn(BatchStatement batchStatement, T entity, CqlStatement statement);
//Delete Methods
void delete(Object entity);
void deleteByKey(ClassentityClass, Object...keys);
void delete(BatchStatement batchStatement, Object entity);
//Operations are committed only when the execute method is called
void execute(BatchStatement batchStatement);
// Query Methods
T findOne(CqlStatement statement);
List findAll(CqlStatement statement);
long count(CqlStatement statement);
List findAll(CqlStatement statement, CqlFilter filter);
Object[] findAllOneColumn(String coloumnNameToBeSelected, CqlStatement statement);
void findAllOneColumn(SetobjSet, String coloumnNameToBeSelected, CqlStatement statement);
}
####Multiple updates in One transaction
- With BatchStatment, multiple Create/update/delete operations can be performed in a single transaction.
- Multiple operations are performed and at the end the operations are committed only when the execute method is called.
BatchStatement stmt=new BatchStatement(); CustOrder order=new CustOrder(); //... Set required Order fields em.insert(stmt, order); //Still not commited. CustOrderIndex orderIndex=new CustOrderIndex(); //..set the requried fields. em.insert(stmt, orderIndex); //Committed only after the execute call. //If an exception is encoutnered, the transaction is rolled back. em.execute(stmt);
####updateColumn
- In cassandra updating a single column is more faster.
- If you want to update just a column, use this method.
em.updateColumn(restaurant, CqlBuilder.update(Customer.class).field("firstName").set("John"));
####Query Methods Examples
@Override
public CustOrder findById(UUID id) {
CustOrder order=em.findOne(CqlBuilder.select(CustOrder.class).field("id").eq(id));
return order;
}
@Override
public CustOrder findByOrderNo(UUID restaurantId, String orderNo) {
CustOrder order=null;
CustOrderIndex1 i1=em.findOne(CqlBuilder.select(CustOrderIndex1.class).field("restaurantId").eq(restaurantId).field("orderNo").eq(orderNo));
return order;
}
@Override
public List findByStatusAndDateRange(UUID restaurantId,
String status, Date dt1, Date dt2) {
Set idSet=new HashSet();
em.findAllOneColumn(idSet,"id",CqlBuilder.select(CustOrderLink5.class).field("restaurantId").eq(restaurantId).field("status").eq(status).field("orderDtTm").ge(dt1).field("orderDtTm").le(dt2).limit(500));
List orders=em.findAll(CqlBuilder.select(CustOrder.class).field("id").in(idSet));
return orders;
}