-
Notifications
You must be signed in to change notification settings - Fork 20
Quick Start
Sai Pullabhotla edited this page Sep 29, 2017
·
6 revisions
Add Catatumbo as a dependency to your project.
<dependency>
<groupId>com.jmethods</groupId>
<artifactId>catatumbo</artifactId>
<version>2.3.0</version>
</dependency>
dependencies {
compile 'com.jmethods:catatumbo:2.3.0'
}
Model classes can either use classic JavaBeans Pattern or Builder Pattern. In either case, note that the model class is annotated with @Entity
. Also, the id field is annotated as @Identifier
.
import java.time.LocalDate;
import com.jmethods.catatumbo.Entity;
import com.jmethods.catatumbo.Identifier;
@Entity
public class Person {
@Identifier
private long id;
private String name;
private LocalDate birthDate;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDate getBirthDate() {
return birthDate;
}
public void setBirthDate(LocalDate birthDate) {
this.birthDate = birthDate;
}
}
@Entity
public class Person {
@Identifier
private final long id;
private final String name;
private final LocalDate birthDate;
private Person(Builder builder) {
this.id = builder.id;
this.name = builder.name;
this.birthDate = builder.birthDate;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public LocalDate getBirthDate() {
return birthDate;
}
public static Builder newBuilder() {
return new Builder();
}
public static class Builder {
private long id;
private String name;
private LocalDate birthDate;
public Builder setId(long id) {
this.id = id;
return this;
}
public Builder setName(String name) {
this.name = name;
return this;
}
public Builder setBirthDate(LocalDate birthDate) {
this.birthDate = birthDate;
return this;
}
public Person build() {
return new Person(this);
}
}
}
Now let's create a simple DAO to create, read, update and delete Person
entities.
import com.jmethods.catatumbo.EntityManager;
import com.jmethods.catatumbo.EntityManagerFactory;
public class PersonDAO {
public static PersonDAO instance = new PersonDAO();
private EntityManager em;
private PersonDAO() {
em = EntityManagerFactory.getInstance().createDefaultEntityManager();
}
public Person create(Person person) {
return em.insert(person);
}
public Person update(Person person) {
return em.update(person);
}
public void delete(Person person) {
em.delete(person);
}
public Person read(long id) {
return em.load(Person.class, id);
}
}
import java.time.LocalDate;
public class CreatePerson {
public static void main(String[] args) {
Person person = new Person();
person.setName("John Doe");
person.setBirthDate(LocalDate.of(1990, 2, 15));
PersonDAO dao = PersonDAO.getInstance();
person = dao.create(person);
System.out.printf("Person entity with ID %d was created successfully", person.getId());
}
}
import java.time.LocalDate;
public class CreatePerson {
public static void main(String[] args) {
Person person = Person.newBuilder().setName("John Doe").setBirthDate(LocalDate.of(1990, 2, 15))
.build();
PersonDAO dao = PersonDAO.getInstance();
person = dao.create(person);
System.out.printf("Person entity with ID %d was created successfully", person.getId());
}
}
When you run either of the CreatePerson programs, a new entity will be created in Google Cloud Datastore. The console should have something like this:
Person entity with ID 5754272234864640 was created successfully
Log into Google Cloud Console and make sure the new Person entity is created as shown below: