SeabedOHM is an Object-Hash Mapping
Java persistence library inspired by HibernateORM implemented for Redis
.
java-json.jar
Java JSON libraryjedis-2.0.0.jar
Java Redis Client
package com.seabed.sample;
import com.seabed.ohm.SeabedObject;
import com.seabed.ohm.annotations.AutoIncrement;
import com.seabed.ohm.annotations.ID;
import com.seabed.ohm.annotations.Persist;
import com.seabed.ohm.annotations.SBObject;
import com.seabed.ohm.exceptions.SeabedOHMException;
@SBObject(namespace = "sample")
public class SampleObject extends SeabedObject {
public SampleObject() throws SeabedOHMException {
super();
}
public SampleObject(Object id) throws SeabedOHMException {
super(id);
}
@ID
@AutoIncrement
public int id;
@Persist
public String firstName;
@Persist
public String lastName;
}
@SBObject(namespace = "sample")
- Declare an
@SBObject
annotation on top of the Class. - Set the value of the
namespace
as the namespace that would correspond in Redis. - For example, in SQL, it's more like the
name of the table
.
public class SampleObject extends SeabedObject
- Extend your Object to
SeabedObject
- This would enable you to use core functionalities of the object (i.e., CRUD).
public SampleObject() throws SeabedOHMException {
super();
}
public SampleObject(Object id) throws SeabedOHMException {
super(id);
}
- Add a
constructor
which throwsSeabedOHMException
- Don't worry, if you're using
Eclipse
, this would just auto-suggest.
@ID
@AutoIncrement
public int id;
- Declare an
ID field
that would become yourunique identifier
for every entry of this object. - On top of your ID field, declare an
@ID annotation
. - The ID field can be any
Number
(e.g., int, long, short, etc.) or aString
.
@Persist
public String firstName;
@Persist
public String lastName;
- To flag a field as
Persistent
, add the annotation@Persist
on top of the field. - Currently
supported data types
are the following:
- Number (e.g., int, long, short, double, etc.)
- String
- List
- JSONObject
public static void create() throws SeabedOHMException {
SampleObject obj = new SampleObject(); // Construct the object.
obj.setFirstName("John"); // Set obj first name.
obj.setLastName("Doe"); // Set obj last name.
obj.create(); // To add a new entry.
}
public static void update() throws SeabedOHMException {
SampleObject obj = new SampleObject(1); // Construct the object.
obj.setFirstName("Jane"); // Change the name of John to Jane.
obj.update(); // Update the entry.
}
public static void delete() throws SeabedOHMException {
SampleObject obj = new SampleObject(1); // Construct the object.
obj.delete(); // Delete the entry.
}