Skip to content

Achilles Annotations

DuyHai DOAN edited this page Oct 14, 2013 · 25 revisions

For bean mapping, only field-based access type is supported by Achilles. Furthermore, there is no default mapping for fields. If you want a field to be mapped, you must annotate it with @Column/@Id/@EmbeddedId. All fields that are not annotated by either annotations are considered transient by Achilles

Below is a list of all Achilles annotations.


#### @Entity

Indicates that an entity is candidate for persistence. By default Achilles creates a table whose name is the short class name of the entity. If you want to specify a specific table name, set the table attribute.

Example:

@Entity(table="users")
public class User 
{
	...
	...
}

> **Please note that Cassandra limits the column family name to 48 characters max.**
#### @Id

Indicates a field to be mapped as the primary key for the entity. The primary key can be of any type, even a plain POJO. When the name attribute of @Id is given, the field will be mapped to this name. Otherwise the field name will be used.

For non Clustered Entity, the primary key also corresponds to partition key.


#### @EmbeddedId

Indicates a field to be mapped as a compound primary key for an clustered entity. A valid compound primary key should be a POJO having at least 2 fields representing primary key components. When the name attribute of @EmbeddedId is given, the field will be mapped to this name. Otherwise the field name will be used.

The first component is the partition key, the remaining components are the clustering keys.

For more details, see Clustered Entity


#### @Column

Indicates a field to be mapped by Achilles. When the name attribute of @Column is given, the field will be mapped to this name. Otherwise the field name will be used.

Example:

@Column(name = "age_in_years")
private Long age; 

#### @Lazy

When put on a @Column field, this annotation makes it lazy. When an entity is loaded by Achilles, only eager fields are loaded. Lazy fields will be loaded at runtime when the getter is invoked.

A lazy Collection or Map field will be loaded entirely when the getter is invoked.


#### @Order

This annotation is used to define components for compound primary keys. It should be used in a class which is an @EmbeddedId for an entity.

The annotation requires the value attribute to indicates the order of the current component in the key. Example:

@Entity
public class MyEntity 
{
	@EmbeddedId
	private CompoundKey id;
	
	...
		
	private static class CompoundKey 
	{
		@Order(1)
		private Long userId;

		@Order(2)
		private UUID tweetId;
		...
	}
	...
}

> Unlike Java indexes, the ordering starts at 1. It is a design choice since it is more natural for human being to start counting at 1

For more detail on this annotation and its usage, please refer to Clustered Entity


#### @PartitionKey

This annotation indicates which component is part of the partition key for a compound primary key.

If you have a simple partition key, this annotation is optional. By default the component having @Order(1) is considered by Achilles as default partition key. If you have a composite partition key with many components, add this annotation on each of them.

@Entity
public class MyEntity 
{
	@EmbeddedId
	private CompoundKey id;
	
	...
		
	private static class CompoundKey 
	{
		@PartitionKey
		@Order(1)
		private Long id;

		@PartitionKey
		@Order(2)
		private String type;

		@Order(3)
		private UUID date;
		...
	}
	...
}

In the above example, id and type are part of the composite partition key. date is the clustering key.

Remark: all fields annotated with @PartitionKey should be consecutive with respect to their ordering. Failing this condition will raise an exception during Achilles bootstrap


#### @Consistency

This annotation can be used on an entity or a Counter field

You need to specify the read and write attribute to define the corresponding consistency level.

Example:

@Entity
@Consistency(read=ConsistencyLevel.ONE,write=ConsistencyLevel.QUORUM)
public class MyBean 
{
	@Id
	private Long id;
	
	...
		
	@ConsistencyLevel(read=ConsistencyLevel.ONE,write=ConsistencyLevel.ONE)
	@Counter
	@Column
	private Long counter;

}

#### @TimeUUID

This annotation tells Achilles to map a Java UUID field to Cassandra timeuuid type.

Example:

@Entity
@Consistency(read=ConsistencyLevel.ONE,write=ConsistencyLevel.QUORUM)
public class MyBean 
{
	@Id
	private Long id;
	
  @TimeUUID
  @Column 
  private UUID date;
	...

This is especially useful in CQL3 to map to timeuuid type so you can use Timeuuid functions like dateOf()/now()/minTimeuuid()/maxTimeuuid() or unixTimestampOf() on native queries

Home

Clone this wiki locally