How does annotation work with Java? And how can I create custom annotations like this:
@Entity(keyspace=':')
class Student
{
@Id
@Attribute(value="uid")
Long Id;
@Attribute(value="fname")
String firstname;
@Attribute(value="sname")
String surname;
// Getters and setters
}
Basically, what I need to have is this POJO be serialized like this when persisted:
dao.persist(new Student(0, "john", "smith"));
dao.persist(new Student(1, "katy", "perry"));
Such that, the actual generated/persisted object is a Map<String,String> like this:
uid:0:fname -> john
uid:0:sname -> smith
uid:1:fname -> katy
uid:1:sname -> perry
Any ideas how to implement this?
If you create custom annotations you will have to use
ReflectionAPI Example Here to process them.You can refere How to declare annotation.
Here is how example annotation declaration in java looks like.
RetentionandTargetare known asmeta-annotations.RetentionPolicy.RUNTIMEindicates that you want to retain the annotation at runtime and you can access it at runtime.ElementType.METHODindicates that you can declare annotation only on methods similarly you can configure your annotation for class level, member variable level etc.Each Reflection class has methods to get annotations which are declared.
You will find these methods present for
Field,Method,Classclasses.e.g.To retrieve annotations present on specified class at run time