-
Notifications
You must be signed in to change notification settings - Fork 31
Using the EntityManager
##Using the EntityManager
The easiest way to use the library is to instantiate an EntityManager and call its save and load methods. The EntityManager will scan the package provided looking for POJOs annotated with @Entity. Any POJO with @Entity will be usable by the EntityManager for saving and loading objects. This method of persistence is very much in line with the JPA standard.
As an example consider the following POJO:
package com.mycompany;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.Map.Entry;
import com.real.hom.ColorConverter;
import com.real.hom.Colors;
import com.real.hom.annotations.AnonymousPropertyAddHandler;
import com.real.hom.annotations.AnonymousPropertyCollectionGetter;
import com.real.hom.annotations.Column;
import com.real.hom.annotations.Entity;
import com.real.hom.annotations.Id;
import com.real.hom.annotations.Table;
@Entity
@Table("TestColumnFamily")
class MyPojo {
@Id
private UUID id;
@Column("lp1")
private long longProp1;
@Column(value = "color", converter = ColorConverter.class)
private Colors color;
private Map<String, String> anonymousProps = new HashMap<String, String>();
@AnonymousPropertyAddHandler
public void addAnonymousProp(String name, String value) {
anonymousProps.put(name, value);
}
@AnonymousPropertyCollectionGetter
public Collection<Entry<String, String>> getAnonymousProps() {
return anonymousProps.entrySet();
}
public String getAnonymousProp(String name) {
return anonymousProps.get(name);
}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public long getLongProp1() {
return longProp1;
}
public void setLongProp1(long longProp1) {
this.longProp1 = longProp1;
}
public Colors getColor() {
return color;
}
public void setColor(Colors color) {
this.color = color;
}
}
Notice the @Entity and @Table annotating MyPojo. @Entity merely signals the EntityManager to make the bean available for persistence management. @Table defines what ColumnFamily to use when loading and saving. If @Table is omitted the name of the class is used as the ColumnFamily. Properties are not automatically mapped to a ColumnFamily column. To persist a property it must be annotated with @Column, and the name of the column is required.
So with an annotated POJO it is very easy to save and load an object:
package com.mycompany;
import java.util.UUID;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.factory.HFactory;
import com.real.hom.Colors;
import com.real.hom.EntityManager;
public class MainProg {
public static void main(String[] args) {
Cluster cluster = HFactory.getOrCreateCluster("TestPool", "localhost:9160");
Keyspace keyspace = HFactory.createKeyspace("TestKeyspace", cluster);
EntityManager em = new EntityManager(keyspace, "com.mycompany");
MyPojo pojo1 = new MyPojo();
pojo1.setId(UUID.randomUUID());
pojo1.setLongProp1(123L);
pojo1.setColor(Colors.RED);
em.save(pojo1);
// do some stuff
MyPojo pojo2 = em.load(MyPojo.class, pojo1.getId());
// do some more stuff
System.out.println( "Color = " + pojo2.getColor() );
}
}
That’s the basics! Not much to it. See Custom Property Converters and Anonymous Properties for advanced configuration.