Introducing ActiveJpa

Follow up to my previous post on building restful services at ease, I have created a library ActiveJpa that is aimed at avoiding the need to create a layer for DAL. It does so by integrating the data access piece with the models. ActiveJpa attempts to implement the active record pattern on top of JPA to allow operations like ActiveRecord library in ruby.

Visit http://github.com/ActiveJpa/activejpa for samples and usage.

Some sample operations that you can do with ActiveJpa,

    // Get order by id
    Order order = Order.findById(12345L);

    // Get all orders for a customer that are shipped
    List<Order> orders = Order.where("customer_email", "dummyemail@dummy.com", "status", "shipped");

    // Get all orders for the product category 'books' and paginate it
    Filter filter = new Filter();
    filter.setPageNo(1);
    filter.setPerPage(25);
    filter.addCondition(new Condition("orderItems.product.category", Operator.eq, "books");
    List<Order> orders = Order.where(filter);

    // Count of orders matching the filter
    Long count = Order.count(filter);

    // Get the first order matching the filter
    Long count = Order.first("customer_email", "dummyemail@dummy.com", "status", "shipped");

    // Get the unique order matching the conditions
    Long count = Order.one("customer_email", "dummyemail@dummy.com", "status", "shipped");

    // Dump everything
    List<Order> orders = Order.all();

    // Delete all orders matching the filter
    Long count = Order.deleteAll(filter);

    // Check if order exists with the given identifier
    boolean exists = Order.exists(1234L);

    // Save order
    order.setBillingAmount(1000.0);
    order.persist();

    // Delete order
    order.delete();

    // Update attributes
    Map<String, Object> attributes = new HashMap<String, Object>();
    attributes.put("billingAmount", 1000.0);
    order.updateAttributes(attributes);

    // Find order item by id within an order
    order.collections("order_items").findById(123L);

    // Search order items by filter with an order
    order.collections("order_items").findById(filter);

2 comments

  1. Nick says:

    Can you show an example using Active JPA on how do a Join – Thanks

Leave a Reply

%d bloggers like this: