DEV Community

Braisdom
Braisdom

Posted on

A new ORM framework in Java

ObjectiveSQL is an ORM framework in Java based on ActiveRecord pattern, which encourages rapid development and clean, codes with the least, and convention over configuration.

github: https://github.com/braisdom/ObjectiveSql

Features

  • Dynamic code generation with JSR 269 for Java API of database access
  • Automate your methods of CRUD operations in database, and much more
  • Easy to relation query, transaction and paged query
  • Dynamically SQL programming with Java syntax, and similar to program with SQL syntax extremely

Installation

IntelliJ IDEA plugin installation

Preferences/Settings -> Plugins -> Search with "ObjectiveSql" in market -> Install

Maven dependencies

<!-- In standalone -->
<dependency>
    <groupId>com.github.braisdom</groupId>
    <artifactId>objective-sql</artifactId>
    <version>1.4.0</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode
<!-- In Spring Boot -->
<dependency>
  <groupId>com.github.braisdom</groupId>
  <artifactId>objsql-springboot</artifactId>
  <version>1.1.0</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Refer to the pom.xml for more configurations

Examples

ObjectiveSQL provides full example for various databases below, You can open it directly with IntelliJ IDEA as a standalone project. In fact, they are not just examples, but also unit tests of ObjectiveSQL in various databases.

MySQL, Oracle, MS SQL Server, SQLite, PostgreSQL, Spring Boot

Simple SQL programming without coding

You just define a JavaBean with one annotation your class has a fully featured database access capabilities

@DomainModel
public class Member {
    private String no;

    @Queryable
    private String name;
    private Integer gender;
    private String mobile;
    private String otherInfo;

    @Relation(relationType = RelationType.HAS_MANY)
    private List<Order> orders;
}
Enter fullscreen mode Exit fullscreen mode

Persistence methods

Member.create(newMember);
Member.create(newMember, true); // Create a member without validating
Member.create(Member.newInstanceFrom(memberHash));
Member.create(new Member[]{newMember1, newMember2, newMember3}, false);

Member.update(1L, newMember, true); // Update a member with primary key
Member.update("name = 'Smith => Jackson'", "name = 'Alice'");

Member.destroy(1L); // Delete a member with primary key
Member.destroy("name = 'Mary'");

// Execute SQL
Member.execute(String.format("DELETE FROM %s WHERE name = 'Mary'", Member.TABLE_NAME));
Enter fullscreen mode Exit fullscreen mode

Transaction

@Transactional
public static void makeOrder(Order order, OrderLine... orderLines) throws SQLException {
  Order.create(order, false);
  OrderLine.create(orderLines, false);
}
Enter fullscreen mode Exit fullscreen mode

Counting and querying simply

Member.countAll();
Member.count("id > ?", 1);
Member.queryByPrimaryKey(1);
Member.queryFirst("id = ?", 1);
Member.query("id > ?", 1);
Member.queryAll();
Enter fullscreen mode Exit fullscreen mode

Paged query

Page page = Page.create(0, 10);// Create a Page instance with current page and page size
PagedList<Member> members = Member.pagedQueryAll(page, Member.HAS_MANY_ORDERS);
Enter fullscreen mode Exit fullscreen mode

Relation query

// Querying objects with convenient methods, and it will carry the related objects
Member.queryAll(Member.HAS_MANY_ORDERS);
Member.queryByPrimary(1, Member.HAS_MANY_ORDERS);
Member.queryByName("demo", Member.HAS_MANY_ORDERS);
...
Enter fullscreen mode Exit fullscreen mode

Complex SQL programming

// SQL programming with Java syntax without losing the features of SQL syntax
Order.Table orderTable = Order.asTable();
Select select = new Select();

select.project(sum(orderTable.amount) / sum(orderTable.quantity) * 100)
        .from(orderTable)
        .where(orderTable.quantity > 30 &&
            orderTable.salesAt.between($("2020-10-10 00:00:00"), $("2020-10-30 23:59:59")))
        .groupBy(orderTable.productId);
Enter fullscreen mode Exit fullscreen mode
-- SQL syntax is the same as Java syntax
SELECT ((((SUM(`T0`.`amount` ) / SUM(`T0`.`quantity` ) )) * 100))
FROM `orders` AS `T0`
WHERE ((`T0`.`quantity` > 30) AND 
       `T0`.`sales_at` BETWEEN '2020-10-10 00:00:00' AND '2020-10-30 23:59:59')
GROUP BY `T0`.`product_id`
Enter fullscreen mode Exit fullscreen mode

See more:

Top comments (8)

Collapse
 
braisdom profile image
Braisdom • Edited

Hi, because operator overloading is implemented in ObjectiveSQL, it means that Java operator will be mapped as SQL operator, for example

orderTable.quantity > 30 &&
            orderTable.salesAt.between($("2020-10-10 00:00:00"), $("2020-10-30 23:59:59"))
Enter fullscreen mode Exit fullscreen mode

it will mapped as:

(`T0`.`quantity` > 30) AND 
       `T0`.`sales_at` BETWEEN '2020-10-10 00:00:00' AND '2020-10-30 23:59:59'
Enter fullscreen mode Exit fullscreen mode
Collapse
 
braisdom profile image
Braisdom

Refer to the github.com/braisdom/ObjectiveSql for more information

Collapse
 
braisdom profile image
Braisdom

And code generation based on Javac AST is a dynamic codding capability in Java

Collapse
 
braisdom profile image
Braisdom

thx, operator overloading makes Java look like SQL

Collapse
 
braisdom profile image
Braisdom

The complex SQL is part of ObjectiveSQL, and the most importantly, ObjectiveSQL is about automatic code generation.

It will make SQL programming very easy in Java

Collapse
 
braisdom profile image
Braisdom

Why is no one attention to such a project

Collapse
 
braisdom profile image
Braisdom • Edited

Refer to the github.com/braisdom/ObjectiveSql for more information

Collapse
 
braisdom profile image
Braisdom

I think you will like it