-
Notifications
You must be signed in to change notification settings - Fork 0
Getting started
rnentjes edited this page Feb 26, 2013
·
18 revisions
Simple example to get started with Simple-persistence.
The following jars are required on the classpath:
- prevayler-2.x.jar]
- simple-persistence-x.x.jar
Create a model object:
- implement Persistent
- implement both methods getId() and clone()
- make sure it has a serialVersionUID
import nl.astraeus.persistence.Persistent
public class Comment implements Persistent<Long> {
public final static long serialVersionUID = -9038882251579382910L;
private long id;
private long date = System.currentTimeMillis();
private String description = "";
public Comment() { }
public Comment(String description) {
this.id = IdGenerator.nextId();
this.description = description;
}
public Long getId() {
return id;
}
public Comment clone() throws CloneNotSupportedException {
return (Comment)super.clone();
}
}Create a Dao object to work with it:
public class CommentDao extends PersistentDao<Long, Comment> {
}To create and store a new Comment object: (we need the autocommit setting or a transaction but let's ignore that for now)
public class TestCreateComment {
public static void main(String [] args) {
System.setProperty(SimpleStore.AUTOCOMMIT, String.valueOf(true));
Comment comment = new Comment("Some comment on something");
CommentDao dao = new CommentDao();
dao.store(comment);
}
}And that't it, your object is now persistent. Let's take a look at all comments:
public class TestShowComments {
public static void main(String [] args) {
CommentDao dao = new CommentDao();
Collection<Comment> comments = dao.findAll();
for (Comment comment : comments) {
System.out.println("Comment: "+comment);
}
// find by id
Comment comment = dao.find(1L);
}
}Next: References