Skip to content
This repository was archived by the owner on May 3, 2022. It is now read-only.

annotations_custom

Otávio Santana edited this page Aug 6, 2013 · 1 revision

You also can do a different way to storage an information with @org.easycassandra.CustomData annotation. By default it will serialize the object and put in Cassandra for this the object should implements the java.io.Serializable, but you may do another custom create a class whose implements org.easycassandra.persistence.cassandra.Customizable and pass itself in classCustmo, parameter of CustomData annotation.

Sample Custom default


 @Entity(name="product")
 public class Avatar{

 @Id
 private String nickName;

 @CustomData //this class should implements java.io.Serializable
 private Picture imagem;
 //getter and setter
 }

Class which implements Serializable


 public class Picture implements Serializable{

 private String nameImage;

 private byte[] contents;

 }	

Sample implementing Customizable

annotated the entity


 @Entity(name="product")
 public class ProductShopping {

 @Id
 private String name;

 @CustomData(classCustmo=ProductsCustomData.class)
 private Products products;

//getter and setter

}

Object example


 public class Products  {

private static final long serialVersionUID = 1L;

private String nome;

private Double value;

private String country;
  
   //getter and setter
   
   }

implements the Customizable


 public class ProductsCustomData implements Customizable{

public ByteBuffer read(Object object) {
	Products products=(Products)object;
	StringBuilder result=new StringBuilder();
	result.append(products.getNome()).append("|");
	result.append(products.getValue()).append("|");
	result.append(products.getCountry());
	return ByteBuffer.wrap(result.toString().getBytes());
	
}

public Object write(ByteBuffer byteBuffer) {
    byte[] result = new byte[byteBuffer.remaining()];
        byteBuffer.get(result);
    String values[]=new String(result).split("\\|");
	
    Products products= new Products();
    products.setNome(values[0]);
    products.setValue(Double.valueOf(values[1]));
    products.setCountry(values[1]);
    return products;
 }
   }

Clone this wiki locally