Skip to content


Subversion checkout URL

You can clone with
Download ZIP
A simple active record pattern library in java that makes programming DAL easier
Java Other

ActiveJpa Build Status Coverage Status

ActiveJpa is a java library that attempts to implement the active record pattern on top of JPA. The goal of this library is to eliminate the need to create DAO or Repository classes and make programming DAL a lot more simpler.

What can you do with ActiveJpa?

AcitveJpa abstracts out some of the most common functionalities you might need in your DAL. You should be able to do,

    // 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", "", "status", "shipped");

    // Get all orders for the product category 'books' and paginate it
    Filter filter = new Filter();
    filter.addCondition(new Condition("orderItems.product.category", Operator.eq, "books");
    filter.addSortField("status", true);
    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", "", "status", "shipped");

    // Get the unique order matching the conditions
    Long count ="customer_email", "", "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

    // Delete order

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

    // Find order item by id within an order

    // Search order items by filter with an order


Getting Started

Setting Up Maven

ActiveJpa is available as a Maven artifact and should be fairly simpler to integrate with your application. Just add the below maven dependency to your pom.xml file,



If you are on a non-maven project, you will have to include these additional dependencies in addition to activejpa-core,

  • Javassist-3.17.1-GA.jar
  • hibernate-jpa-2.1-api.1.0.0-Final.jar
  • commons-beanutils-1.8.3.jar
  • commons-lang3-3.1.jar
  • guava-14.0.1.jar
  • slf4j-api-1.7.5.jar

Hooking into your application

ActiveJPA does some runtime bytecode enhancement to simplify development and madates you to hook it before your models are loaded by the classloader. There are two ways you can hook it to your application,

  • Run your applicaiton with the activejpa java agent by the below jvm option "-javaagent:activejpa-core.jar"
  • At the bootstrap of your application, before your classes are loaded, manually load the java agent using the following code. This will require you to add tools.jar to your dependency list.

Setup EntityManagerFactory

You have to feed in the persistence unit to ActiveJpa to do the magic. There are multiple ways you can do this,

    // Add the persistence unit defined by persistence.xml identified by the name 'order'. The persistence.xml should be available in the classpath

    // If you have entity manager factory already created, you can attach the same to ActiveJpa
    JPA.addPersistenceUnit("order", entityManagerFactory);

Enhancing your Entities

ActiveJpa enhances all the classes that is a subclass of org.activejpa.entity.Model and has java.persistence.Entity annotation. So ensure all your JPA entities extend org.activejpa.entity.Model class

    public class Order extends org.activejpa.entity.Model {

        private Long id;

        public Long getId() {
            return id;

Managing transactions

All the update operations in the model class will open up a transaction if one is not found in the current context. Below code demonstrates wrapping a unit of work under a transaction,

    JPAContext context = JPA.instance.getDefaultConfig().getContext();
    boolean failed = true;
    try {
        // Your unit of work here
        failed = false;
    } finally {
        // Commit or rollback the transaction

Testing your models

The setup done for taking care of byte code instrumentation applies for your test cases. But most of the IDE's support running individual test cases and adding the -javaagent option to every such run is a pain.

ActiveJpa provides an abstract model test class for tesng that enables instrumentation for all your modles without specifying -javaagent option to your test runs. To use this you will have to extend org.activejpa.entity.testng.BaseModelTest class,

    public class OrderTest extends BaseModelTest {

        public void testCreateOrder() {
            Order order = new Order();
            Assert.assertEquals(Order.where("customer_email", "").get(0), order);


Spring integration

There's a sample application that demonstrates the Spring-ActiveJpa integration

How to ask for help?

Create an issue explaining your issue as detail as possible.


ActiveJPA is offered under Apache License, Version 2.0

Something went wrong with that request. Please try again.