All Projects → lazaronixon → active-persistence

lazaronixon / active-persistence

Licence: MIT license
Active Persistence is a implementation of Active Record Query Interface for JPA that makes it easy and fun.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to active-persistence

apex-query-builder
Convenient query builder for dynamic SOQL queries
Stars: ✭ 37 (+164.29%)
Mutual labels:  query, query-builder
SimpleCurd
2个类,实现类ActiveRecord,无需写Mapper, mybatis增强
Stars: ✭ 14 (+0%)
Mutual labels:  activerecord, jpa
Activejpa
A simple active record pattern library in java that makes programming DAL easier
Stars: ✭ 172 (+1128.57%)
Mutual labels:  activerecord, jpa
Jsonb accessor
Adds typed jsonb backed fields to your ActiveRecord models.
Stars: ✭ 558 (+3885.71%)
Mutual labels:  activerecord, query
uri-query-parser
a parser and a builder to work with URI query string the right way in PHP
Stars: ✭ 38 (+171.43%)
Mutual labels:  query, query-builder
Querybuilder
SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird
Stars: ✭ 2,111 (+14978.57%)
Mutual labels:  activerecord, query-builder
php-orm-benchmark
The benchmark to compare performance of PHP ORM solutions.
Stars: ✭ 82 (+485.71%)
Mutual labels:  activerecord, orm-framework
Laravel Eloquent Query Cache
Adding cache on your Laravel Eloquent queries' results is now a breeze.
Stars: ✭ 529 (+3678.57%)
Mutual labels:  query, query-builder
activerecord-setops
Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll).
Stars: ✭ 21 (+50%)
Mutual labels:  activerecord, query
Gridify
Easy and optimized way to apply Filtering, Sorting, and Pagination using text-based data.
Stars: ✭ 372 (+2557.14%)
Mutual labels:  query, query-builder
Baby squeel
🐷 An expressive query DSL for Active Record 4 and 5
Stars: ✭ 362 (+2485.71%)
Mutual labels:  activerecord, query
AdvancedSQL
The best Java query builder/SQL connector.
Stars: ✭ 23 (+64.29%)
Mutual labels:  query, query-builder
Sqliterally
Lightweight SQL query builder
Stars: ✭ 231 (+1550%)
Mutual labels:  query, query-builder
vaultaire
Query DSL and data access utilities for Corda developers.
Stars: ✭ 14 (+0%)
Mutual labels:  query, query-builder
Pecee Pixie
Lightweight, easy-to-use querybuilder for PHP inspired by Laravel Eloquent - but with less overhead.
Stars: ✭ 19 (+35.71%)
Mutual labels:  query, query-builder
querqy-elasticsearch
Querqy for Elasticsearch
Stars: ✭ 37 (+164.29%)
Mutual labels:  query, query-builder
Loukoum
A simple SQL Query Builder
Stars: ✭ 305 (+2078.57%)
Mutual labels:  query, query-builder
React Querybuilder
A QueryBuilder component for React
Stars: ✭ 315 (+2150%)
Mutual labels:  query, query-builder
Neo
Orm框架:基于ActiveRecord思想开发的至简化的java的Orm框架
Stars: ✭ 35 (+150%)
Mutual labels:  activerecord, orm-framework
spring-filter
Painless filtering library for JPA entities and MongoDB collections. Smoothly integrates with Spring APIs.
Stars: ✭ 123 (+778.57%)
Mutual labels:  query, jpa

Getting Started

pom.xml

<dependency>
  <groupId>com.github.lazaronixon</groupId>
  <artifactId>active-persistence</artifactId>
  <version>0.0.31</version>
</dependency>

models/User.java

@Entity
public class User extends BaseIdentity {

    private String name;

    private String occupation;

    private LocalDateTime createdAt;

    private LocalDateTime updatedAt;

    // Get/Set omitted by brevity
}

repositories/UserRepository.java

@RequestScoped
public class UserRepository extends Base<User> {

}

CRUD: Reading and Writing Data

Create

User user = new User();
user.name = "David";
user.occupation = "Code Artist";

userRepository.save(user);

Read

// return a collection with all users
List<User> users = userRepository.all();

// return the first user
User user = userRepository.first();

// return the first user named David
User david = userRepository.findBy("user.name = ?", "David");

// find all users named David who are Code Artists and sort by createdAt in reverse chronological order
List<User> users = userRepository.where("user.name = 'David' AND user.occupation = 'Code Artist'").order("user.createdAt DESC");

Update

User user = userRepository.findBy("user.name = ?", "David");
user.name = "Dave";
userRepository.save(user);
//OR
userRepository.updateAll("user.maxLoginAttempts = 3, user.mustChangePassword = 'true'");

Delete

User user = userRepository.findBy("user.name = ?", "David");
userRepository.destroy(user);
//OR
userRepository.destroyBy("user.name = ?", "David");
userRepository.destroyAll();

Callbacks

public class ClientRepository extends Base<Client> {

    @Override
    public void beforeSave(Client client) {
        // implementation here
    }

    @Override
    public void afterSave(Client client) {
        // implementation here
    }

    @Override
    public void beforeCreate(Client client) {
        // implementation here
    }

    @Override
    public void afterCreate(Client client) {
        // implementation here
    }

    @Override
    public void beforeUpdate(Client client) {
        // implementation here
    }

    @Override
    public void afterUpdate(Client client) {
        // implementation here
    }

    @Override
    public void beforeDestroy(Client client) {
        // implementation here
    }

    @Override
    public void afterDestroy(Client client) {
        // implementation here
    }

}

TimeStamps

public class Post extends BaseIdentity {

    private LocalDateTime createdAt;

    private LocalDateTime updatedAt;

    @Override
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    @Override
    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
}

Retrieving Objects from the Database

Retrieving a Single Object

// Find the client with primary key (id) 10.
Client client = clientRepository.find(10);

// The take method retrieves a record without any implicit ordering
Client client = clientRepository.take();
List<Client> clients = clientRepository.take(2);

// The first method finds the first record ordered by primary key (default)
Client client = clientRepository.first();
Client client = clientRepository.order("client.firstName").first();
List<Client> clients = clientRepository.first(3);

// The last method finds the last record ordered by primary key (default)
Client client = clientRepository.last();
Client client = clientRepository.order("client.firstName").last();
List<Client> clients = clientRepository.last(3);

// The findBy method finds the first record matching some conditions
Client client = clientRepository.findBy("client.firstName = ?", "Lifo"); // #<Client id: 1, firstName: "Lifo">
Client client = clientRepository.findBy("client.firstName = ?", "Jon");  // null

Client client = clientRepository.findBy$("client.firstName = ?", "does not exist"); // EntityNotFoundException

Conditions

//Ordinal Conditions
clientRepository.where("client.ordersCount = ?", 10);
clientRepository.where("client.ordersCount = ? AND client.locked = ?", 10, false);

//Placeholder Conditions
clientRepository.where("client.ordersCount = :count", Map.of("count", 10));
clientRepository.where("client.ordersCount = :count AND client.locked = :locked", Map.of("count", 10, "locked", false));

//SubQuery Conditions
var subquery = clientRepository.select("order.client.id");
clientRepository.where("client.id IN (?), subquery);

Ordering

clientRepository.order("client.createdAt");
clientRepository.order("client.createdAt DESC");
clientRepository.order("client.createdAt ASC");

Selecting Specific Fields

List<Client> client = clientRepository.select("client.viewableBy", "client.locked");
List<Client> client = clientRepository.select("client.name").distinct();

Limit and Offset

clientRepository.limit(5);
clientRepository.limit(5).offset(30);

Group

List<Order> orders = orderRepository.select("date(order.createdAt)", "sum(order.price)").group("date(order.createdAt)");

Total of grouped items

Map<String, Long> result = (Map) orderRepository.group("order.status").count(); // => { 'awaiting_approval' => 7, 'paid' => 12 }

Having

List<Order> orders = orderRepository.select("date(order.createdAt)", "sum(order.price)").group("date(order.createdAt)").having("sum(order.price) > 100");

Overriding Conditions

Unscope

orderRepository.where("order.id > 10").limit(20).order("order.id asc").unscope(ORDER);

Only

orderRepository.where("order.id > 10").limit(20).order("order.id asc").only(ORDER);

Reselect

postRepository.select("post.title", "post.body").reselect("post.createdAt");

Reorder

postRepository.order("post.title").reorder("post.createdAt");

Rewhere

articleRepository.where("article.trashed = true").rewhere("article.trashed = false");

Null Relation

studentRepository.none(); // returns an empty Relation and fires where 1=0.

Locking Records for Update

Client client = clientRepository.lock().first();
// OR
Client client = clientRepository.lock(true).first();
// OR
Client client = clientRepository.lock(PESSIMISTIC_WRITE).first();

Readonly Objects

Client client = clientRepository.readonly().first();
client.visits = 1;

clientRepository.save(); // ReadOnlyRecord Exception

Joining Tables

authorRepository.joins("JOIN author.post post");

Eager Loading Associations (EclipseLink only)

clientRepository.includes("client.address").limit(10);
clientRepository.eagerLoads("client.address").limit(10);

Scopes

Applying a default scope

public class ClientRepository extends Base<Client> {

    @Override
    public Relation<Client> defaultScope() {
        return where("client.name = 'nixon'");
    }

}

clientRepository.all(); // SELECT client FROM Client client WHERE client.name = 'nixon'
clientRepository.unscoped().all(); // SELECT client FROM Client client

Merging of scopes

userRepository.merge(active());

Removing All Scoping

clientRepository.unscoped();
clientRepository.where("client.published = false").unscoped();

Dynamic Finders

Client client = clientRepository.findByExpression("Name", "Nixon");
Client client = clientRepository.findByExpression("NameAndLocked", "Nixon", true);
// OR
Client client = clientRepository.findByExpression$("Name", "not found"); // EntityNotFoundException

Finding by SQL/JPQL

List<Post> posts = postRepository.findBySql("SELECT id, title FROM Post WHERE id = 5").getResultList();
List<Post> posts = postRepository.findByJpql("SELECT p FROM Post p WHERE p.id = 5").getResultList();
// OR
List posts = postRepository.getConnection().selectAll("SELECT id, title FROM Post WHERE id = 5", QueryType.SQL).getResultList();
List posts = postRepository.getConnection().selectAll("SELECT p FROM Post p WHERE p.id = 5", QueryType.JPQL).getResultList();

Existence of Objects

boolean exists = studentRepository.exists("student.name = 'Lifo'");
boolean exists = studentRepository.where("student.name = 'Lifo'").exists();

Pluck

List<Integer> ids = clientRepository.where("client.active = true").pluck("client.id"); //[1, 2, 3]
List<Integer> ids = clientRepository.where("client.active = true").ids; //[1, 2, 3]

Calculations

long   count   = (long)   clientRepository.count();
long   count   = (long)   clientRepository.count("client.age");
int    minimum = (int)    clientRepository.minimum("client.age");
int    maximum = (int)    clientRepository.maximum("client.age");
long   total   = (long)   clientRepository.sum("client.ordersCount");
double average = (double) clientRepository.average("client.ordersCount");

Recommended Environment

  • Java 9
  • JakartaEE 8
  • Payara Server

Testing

  • Install Payara Server >= 5.201
  • Add Resources - ./asadmin add-resources path_to/payara-resources.xml

More info

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].