Download:
EJP 2.2 (new)
Documentation (pdf)
EJP is a powerful and easy to use relational database persistence API for Java. EJP's main features include:
EJP has no need for mapping annotations or XML configuration, and there is no need to extend any classes or implement any interfaces. You truly do use your Plain Old Java Objects (POJOs). EJP is, by far, the easiest persistence API available for Java.
EJP is this easy:
public static void main(String[] args)
{
DatabaseManager dbm = DatabaseManager.getDatabaseManager(...);
dbm.saveObject(new Customer("Smith", "John"));
Customer customer;
if ((customer = dbm.loadObject(new Customer("Smith"))) != null)
{
customer.getSupport().add(new Support(...));
dbm.saveObject(customer);
}
Collection<Customer> list = new ArrayList<Customer>();
list = dbm.loadObjects(list, Customer.class);
...
}
It's used with normal class definitions like the following:
public class Customer
{
String firstName, lastName;
List<Support> support;
List<Order> orders;
...
public Customer(String lastName) { this.lastName = lastName; }
...
public getFirstName() { return firstName; }
public setFirstName(String firstName) { this.firstName = firstName; }
public getLastName() { return lastName; }
public setLastName(String lastName) { this.lastName = lastName; }
...
// Associations (automatic)
public List<Support> getSupport() { return support; }
public void setSupport(List<Support> support) { this.support = support; }
public List<Order> getOrders() { return orders; }
public void setOrders(List<Order> orders) { this.orders = orders; }
...
}
That's it! No configuration and no annotation.
All support requests, bug reports, code access, etc. can be found at SourceForge.net. Please be pro-active and report any issues and feature requests. Thank you!
If you would like to contribute code and/or fixes, please send a support request (via SourceForge.net) requesting commit access to our repository.
EJP can be found at https://sourceforge.net/projects/ejpersistence
With the exception of the imports and ellipsis (...), the following is a complete, compilable, and runable program example. For the purposes of this example, the ellipsis are being used to reduce non-essential information. The class definitions and database table creation scripts follow. There are no mapping requirements, no XML and no Annotations.
public class DatabaseExample
{
public DatabaseExample(DatabaseManager dbm) throws DatabaseException
{
// Inserting contact with associations
Contact contact = new Contact("deisenhower", ...);
contact.getSupport().add(new Support("Request", ...));
contact.getSupport().add(new Support("Response", ...));
contact.getSupport().add(new Support("Request", ...));
contact.getOrders().add(new Order("Dwight D. Eisenhower Dollar", ...));
contact.getOrders().add(new Order("Susan B. Anthony Dollar", ...));
// Saving within an automatic transaction (covers all relationships)
dbm.saveObject(contact);
// Load based on information contained in classes
contact = dbm.loadObject(Contact.class, "where :contactId like 'tjef%'");
System.out.println("\ncontactId = " + contact.getContactId());
// Load a collection of objects from the database
Collection<Contact> c = dbm.loadObjects(new ArrayList<Contact>(), Contact.class);
for (Contact contact2 : c)
System.out.println("contactId = " + contact2.getContactId());
}
public static void main(String[] args) throws DatabaseException
{
DatabaseManager dbm = null;
DatabaseManager.setLogLevel(Level.OFF);
try
{
dbm = DatabaseManager.getDatabaseManager("example");
new DatabaseExample(dbm);
}
finally
{
// Also closes any open Databases
dbm.close(); // optional
}
}
/*
* The following are normal class definitions that
* automatically map to your database
*/
public static class Contact
{
private String contactId, password, firstName, lastName, companyName, email;
private List<Support> support = new ArrayList<Support>();
private List<Order> orders = new ArrayList<Order>();
public Contact() {}
public Contact(String contactId)
{
this.contactId = contactId;
}
public Contact(String contactId, String password, String firstName,
String lastName, String companyName, String email)
{
this.contactId = contactId;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.companyName = companyName;
this.email = email;
}
public String getContactId() { return contactId; }
public void setContactId(String id) { contactId = id; }
public String getPassword() { return password; }
public void setPassword(String passwd) { password = passwd; }
public String getFirstName() { return firstName; }
public void setFirstName(String fName) { firstName = fName; }
public String getLast() { return lastName; }
public void setLast(String lName) { lastName = lName; }
public String getCompanyName() { return companyName; }
public void setCompanyName(String name) { companyName = name; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
// Associations
public List<Support> getSupport() { return support; }
public void setSupport(List<Support> support) { this.support = support; }
public List<Order> getOrders() { return orders; }
public void setOrders(List<Order> orders) { this.orders = orders; }
}
public static class Order
{
private Long orderId;
private Integer quantity;
private Double price;
private String contactId, product, status;
public Order() {}
public Order(String product, Integer quantity, Double price, String status)
{
this.product = product;
this.quantity = quantity;
this.price = price;
this.status = status;
}
public Order(String contactId, String product, Integer quantity,
Double price, String status)
{
this.contactId = contactId;
this.product = product;
this.quantity = quantity;
this.price = price;
this.status = status;
}
public Long getOrderId() { return orderId; }
public void setOrderId(Long orderId) { this.orderId = orderId; }
public String getContactId() { return contactId;}
public void setContactId(String contactId) { this.contactId = contactId; }
public String getProduct() { return product; }
public void setProduct(String product) { this.product = product; }
public Integer getQuantity() { return quantity; }
public void setQuantity(Integer quantity) { this.quantity = quantity; }
public Double getPrice() { return price; }
public void setPrice(Double price) { this.price = price; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
}
public static class Support
{
private Long supportId;
private String contactId, code, status, phone, email, request;
public Support() {}
public Support(String code, String status, String phone,
String email, String request)
{
this.code = code;
this.status = status;
this.phone = phone;
this.email = email;
this.request = request;
}
public Support(String contactId, String code, String status,
String phone, String email, String request)
{
this.contactId = contactId;
this.code = code;
this.status = status;
this.phone = phone;
this.email = email;
this.request = request;
}
public Long getSupportId() { return supportId; }
public void setSupportId(Long id) { supportId = id; }
public String getContactId() { return contactId; }
public void setContactId(String id) { contactId = id; }
public String getCode() { return code; }
public void setCode(String code) { this.code = code; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
public String getPhone() { return phone; }
public void setPhone(String phone) { this.phone = phone; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public String getRequest() { return request; }
public void setRequest(String request) { this.request = request; }
}
}
# MySQL database creation
CREATE TABLE contacts
(
CONTACT_ID varchar(40) NOT NULL PRIMARY KEY,
PASSWORD varchar(40) NOT NULL,
FIRST_NAME varchar(40) NOT NULL,
LAST_NAME varchar(60) NOT NULL,
COMPANY_NAME varchar(60),
EMAIL varchar(255),
CREATED timestamp NOT NULL default CURRENT_TIMESTAMP
);
CREATE TABLE orders
(
ORDER_ID int(11) NOT NULL auto_increment PRIMARY KEY,
CONTACT_ID varchar(40) NOT NULL,
PRODUCT varchar(40) NOT NULL,
QUANTITY int(11) NOT NULL,
PRICE double NOT NULL,
STATUS varchar(20) NOT NULL default 'unverified',
created timestamp NOT NULL default CURRENT_TIMESTAMP,
FOREIGN KEY (CONTACT_ID) REFERENCES contacts (CONTACT_ID) ON DELETE CASCADE
);
CREATE TABLE support
(
SUPPORT_ID int(11) NOT NULL auto_increment PRIMARY KEY,
CONTACT_ID varchar(40) NOT NULL,
CODE varchar(10) NOT NULL,
STATUS varchar(20) NOT NULL,
PHONE varchar(20),
EMAIL varchar(255),
REQUEST varchar(255) NOT NULL,
CREATED timestamp NOT NULL default CURRENT_TIMESTAMP,
FOREIGN KEY (CONTACT_ID) REFERENCES contacts (CONTACT_ID) ON DELETE CASCADE
);
With the exception of the imports and ellipsis (...), the following is a complete, compilable, and runable program example. For the purposes of this example, the ellipsis are being used to reduce non-essential information. The class definitions and database table creation scripts follow. There are no mapping requirements, no XML and no Annotations.
public class DatabaseExample
{
public DatabaseExample(DatabaseManager dbm) throws DatabaseException
{
// Inserting contact with associations
Contact contact = new Contact("deisenhower", ...);
contact.getSupport().add(new Support("Request", ...));
contact.getSupport().add(new Support("Response", ...));
contact.getSupport().add(new Support("Request", ...));
contact.getOrders().add(new Order("Dwight D. Eisenhower Dollar", ...));
contact.getOrders().add(new Order("Susan B. Anthony Dollar", ...));
// Saving within an automatic transaction (covers all relationships)
dbm.saveObject(contact);
// Add an association and update
contact.getSupport().add(new Support("Response", ...));
dbm.saveObject(contact);
// Saving within a transaction manager
new TransactionManager(dbm)
{
public void run() throws Exception
{
// Inserting individually using the
// TransactionManager's saveObject() and deleteObject()
saveObject(new Contact("tjefferson", ...));
saveObject(new Support("tjefferson", ...));
saveObject(new Order("tjefferson", ...));
// Insert new contact only
Contact contact = new Contact("fdroosevelt", ...);
saveObject(contact);
// Add associations and update
contact.getSupport().add(new Support("fdroosevelt", ...));
contact.getOrders().add(new Order("fdroosevelt", ...));
saveObject(contact);
// Can still freely use commit, savepoint and rollback
commit();
Savepoint savepoint = null;
if (supportsSavepoints())
savepoint = setSavepoint();
contact = new Contact("gwashington", ...);
contact.getSupport().add(new Support("gwashington", ...));
contact.getOrders().add(saveObject(new Order("gwashington", ...));
saveObject(contact);
if (supportsSavepoints())
rollback(savepoint);
// Same as saveObject()
contact = new Contact("jkennedy", ...);
contact.getSupport().add(new Support("jkennedy", ...));
contact.getSupport().add(new Support("jkennedy", ...));
contact.getOrders().add(new Order("jkennedy", ...));
getDatabase().saveObject(contact);
}
}.executeTransaction();
/*
* The ejp.DatabaseManager way to load objects
*/
// Load based on information contained in classes
contact = dbm.loadObject(Contact.class, "where :contactId like 'tjef%'");
System.out.println("\ncontactId = " + contact.getContactId());
// or Load based on information contained in objects
contact = dbm.loadObject(new Contact("tjef%"));
System.out.println("contactId = " + contact.getContactId());
// or with variable argument parameters
contact = dbm.loadObject(Contact.class, "where :contactId like ?", "tjef%");
System.out.println("contactId = " + contact.getContactId() + "\n");
// Load a collection of objects from the database
Collection<Contact> c = dbm.loadObjects(new ArrayList<Contact>(), Contact.class);
for (Contact contact2 : c)
System.out.println("contactId = " + contact2.getContactId());
System.out.println();
/*
* The ejp.Database way to load objects
*/
Database db = dbm.getDatabase();
try
{
// Query all
Result<Contact> result = db.queryObject(Contact.class, "order by :lastName");
// Result is Iterable
for (Contact contact3 : result)
{
System.out.println(contact3);
}
// ejp.Result is a cursor
// and can be closed to free the resource
result.close();
// or
result = db.queryObject(Contact.class);
// Print and delete
while (result.hasNext() && (contact = result.next()) != null)
{
System.out.println(contact);
db.deleteObject(contact);
}
result.close();
}
finally
{
// Also closes any open results
db.close();
}
}
public static void main(String[] args) throws DatabaseException
{
DatabaseManager dbm = null;
DatabaseManager.setLogLevel(Level.OFF);
try
{
dbm = DatabaseManager.getDatabaseManager("example");
new DatabaseExample(dbm);
}
finally
{
// Also closes any open Databases
dbm.close(); // optional
}
}
/*
* The following are normal class definitions that
* automatically map to your database
*/
public static class Contact
{
private String contactId, password, firstName, lastName, companyName, email;
private List<Support> support = new ArrayList<Support>();
private List<Order> orders = new ArrayList<Order>();
public Contact() {}
public Contact(String contactId)
{
this.contactId = contactId;
}
public Contact(String contactId, String password, String firstName,
String lastName, String companyName, String email)
{
this.contactId = contactId;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.companyName = companyName;
this.email = email;
}
public String getContactId() { return contactId; }
public void setContactId(String id) { contactId = id; }
public String getPassword() { return password; }
public void setPassword(String passwd) { password = passwd; }
public String getFirstName() { return firstName; }
public void setFirstName(String fName) { firstName = fName; }
public String getLast() { return lastName; }
public void setLast(String lName) { lastName = lName; }
public String getCompanyName() { return companyName; }
public void setCompanyName(String name) { companyName = name; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
// Associations
public List<Support> getSupport() { return support; }
public void setSupport(List<Support> support) { this.support = support; }
public List<Order> getOrders() { return orders; }
public void setOrders(List<Order> orders) { this.orders = orders; }
}
public static class Order
{
private Long orderId;
private Integer quantity;
private Double price;
private String contactId, product, status;
public Order() {}
public Order(String product, Integer quantity, Double price, String status)
{
this.product = product;
this.quantity = quantity;
this.price = price;
this.status = status;
}
public Order(String contactId, String product, Integer quantity,
Double price, String status)
{
this.contactId = contactId;
this.product = product;
this.quantity = quantity;
this.price = price;
this.status = status;
}
public Long getOrderId() { return orderId; }
public void setOrderId(Long orderId) { this.orderId = orderId; }
public String getContactId() { return contactId;}
public void setContactId(String contactId) { this.contactId = contactId; }
public String getProduct() { return product; }
public void setProduct(String product) { this.product = product; }
public Integer getQuantity() { return quantity; }
public void setQuantity(Integer quantity) { this.quantity = quantity; }
public Double getPrice() { return price; }
public void setPrice(Double price) { this.price = price; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
}
public static class Support
{
private Long supportId;
private String contactId, code, status, phone, email, request;
public Support() {}
public Support(String code, String status, String phone,
String email, String request)
{
this.code = code;
this.status = status;
this.phone = phone;
this.email = email;
this.request = request;
}
public Support(String contactId, String code, String status,
String phone, String email, String request)
{
this.contactId = contactId;
this.code = code;
this.status = status;
this.phone = phone;
this.email = email;
this.request = request;
}
public Long getSupportId() { return supportId; }
public void setSupportId(Long id) { supportId = id; }
public String getContactId() { return contactId; }
public void setContactId(String id) { contactId = id; }
public String getCode() { return code; }
public void setCode(String code) { this.code = code; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
public String getPhone() { return phone; }
public void setPhone(String phone) { this.phone = phone; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public String getRequest() { return request; }
public void setRequest(String request) { this.request = request; }
}
}
# MySQL database creation
CREATE TABLE contacts
(
CONTACT_ID varchar(40) NOT NULL PRIMARY KEY,
PASSWORD varchar(40) NOT NULL,
FIRST_NAME varchar(40) NOT NULL,
LAST_NAME varchar(60) NOT NULL,
COMPANY_NAME varchar(60),
EMAIL varchar(255),
CREATED timestamp NOT NULL default CURRENT_TIMESTAMP
);
CREATE TABLE orders
(
ORDER_ID int(11) NOT NULL auto_increment PRIMARY KEY,
CONTACT_ID varchar(40) NOT NULL,
PRODUCT varchar(40) NOT NULL,
QUANTITY int(11) NOT NULL,
PRICE double NOT NULL,
STATUS varchar(20) NOT NULL default 'unverified',
created timestamp NOT NULL default CURRENT_TIMESTAMP,
FOREIGN KEY (CONTACT_ID) REFERENCES contacts (CONTACT_ID) ON DELETE CASCADE
);
CREATE TABLE support
(
SUPPORT_ID int(11) NOT NULL auto_increment PRIMARY KEY,
CONTACT_ID varchar(40) NOT NULL,
CODE varchar(10) NOT NULL,
STATUS varchar(20) NOT NULL,
PHONE varchar(20),
EMAIL varchar(255),
REQUEST varchar(255) NOT NULL,
CREATED timestamp NOT NULL default CURRENT_TIMESTAMP,
FOREIGN KEY (CONTACT_ID) REFERENCES contacts (CONTACT_ID) ON DELETE CASCADE
);