Skip to content
This repository

A simple persistence example.

public class PersistenceDao {

private Persistence persistence;
private Class<T> baseClass;


public PersistenceDao(Class<T> baseClass) {
    this.baseClass = baseClass;

    persistence = PersistenceUtil.INSTANCE.getPersistence();
    PersistenceUtil.INSTANCE.addFamilyObject(baseClass);
}

public boolean insert(T bean) {
    return persistence.insert(bean);
}

public boolean remove(T bean) {
    return persistence.delete(bean);
}

public boolean removeFromRowKey(K rowKey) {
    return persistence.deleteByKey(rowKey, baseClass);
}

public boolean update(T bean) {
    return persistence.update(bean);
}

public T retrieve(Object id) {
    return   persistence.findByKey(id, baseClass);
}

public List<T> listAll() {
    return persistence.findAll(baseClass);
}

public List<T> listByIndex(String indexName, Object index) {
    return persistence.findByIndex(indexName, index, baseClass);
}

public List<T> listByKeyAndIndex(Object id, Object index) {
    return persistence.findByKeyAndIndex(id, index, baseClass);
}

public List<T> listByKeyAndIndexRange(Object id, Object indexStart,
        Object indexEnd, boolean inclusive) {
    return persistence.findByKeyAndIndexRange(id, indexStart, indexEnd,
            inclusive, baseClass);
}

public boolean executeUpdate(String query) {
    return persistence.executeUpdate(query);
}

public Long count() {
    return persistence.count(baseClass);
}

public boolean executeUpdateCql(String string) {
    return persistence.executeUpdate(string);
}

public SelectBuilder<T> select() {
    return persistence.selectBuilder(baseClass);
}

public UpdateBuilder<T> update() {
    return persistence.updateBuilder(baseClass);
}

public InsertBuilder<T> insertBuilder() {
    return persistence.insertBuilder(baseClass);
}

public InsertBuilder<T> insertBuilder(T bean) {
    return persistence.insertBuilder(bean);
}

public DeleteBuilder<T> deleteBuilder(String... columnNames) {
    return persistence.deleteBuilder(baseClass, columnNames);
}

public DeleteBuilder<T> deleteBuilder(Object id, String... columnNames) {
    return persistence.deleteBuilder(baseClass, id, columnNames);
}

public void removeAll() {
    persistence.removeAll(baseClass);
}

public BatchBuilder batchBuilder() {
    return persistence.batchBuilder();
}

public UpdateBuilder<T> update(SimpleID id) {

    return persistence.updateBuilder(baseClass, id);
}
}

Using on a DAO

public class PersonDAOTest {

private PersistenceDao<Person, Long> dao = new PersistenceDao<Person, Long>(
        Person.class);
public void insert() {

    Person person = getPerson();
    person.setName(NAME);
    person.setId(FOUR);
    person.setYear(YEAR);
    Address address = getAddress();
    person.setAddress(address);

    dao.insert(person);

}

public void retrieve() {
    Person person = dao.retrieve(FOUR);
}

public void removeFromRowKeyTest() {
    dao.removeFromRowKey(new Long(2));
}


public void remove() {
    Person person = getPerson();
    person.setId(1L);
    dao.remove(person);
}

public void list() {
    Person person = getPerson();
    person.setId(1L);
    dao.insert(person);
   List<Person> persons = dao.listAll();

}
public void findIndex() {
    List<Person> persons = dao.listByIndex("name", NAME);
}
public void count() {
    Long count = dao.count();
}
}
Something went wrong with that request. Please try again.