Skip to content

Using the EntityManager

FreeWhere edited this page Jun 20, 2012 · 7 revisions

The easiest way to use the library is to instantiate an EntityManagerImpl and call its persist and find methods. The EntityManager will scan the package provided to the constructor 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;

// imports omitted

@Entity
@Table(name="TestColumnFamily")
public class MyPojo {
  @Id
  private UUID id;

  @Column(name="lp1")
  private long longProp1;

  @me.prettyprint.hom.annotations.Column(name = "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. Unlike JPA, properties are not mapped to a ColumnFamily column by default. To persist a property it must be annotated with @Column, and the name of the column is required. Also notice I have used two @Column annotations, one a part of JPA and a custom one that uses converters (See Custom Property Converters) to provide custom type conversion to/from byte[], which is required by Cassandra.

So with an annotated POJO it is very easy to save and load an object:

package com.mycompany;

// imports omitted

public class MainProg {

  public static void main(String[] args) {
    Cluster cluster = HFactory.getOrCreateCluster("TestPool", "localhost:9160");
    Keyspace keyspace = HFactory.createKeyspace("TestKeyspace", cluster);

    try {
      EntityManagerImpl em = new EntityManagerImpl(keyspace, "com.mycompany");

      MyPojo pojo1 = new MyPojo();
      pojo1.setId(UUID.randomUUID());
      pojo1.setLongProp1(123L);
      pojo1.setColor(Colors.RED);

      em.persist(pojo1);

      // do some stuff

      MyPojo pojo2 = em.find(MyPojo.class, pojo1.getId());

      // do some more stuff

      System.out.println("Color = " + pojo2.getColor());
    } finally {
      cluster.getConnectionManager().shutdown();
    }
  }
}

That’s the basics! Not much to it. See Custom Property Converters and Anonymous Properties for advanced configuration.


Up to Hector Object Mapper (HOM)

Clone this wiki locally