Saturday, November 21, 2020

Show HN: A new way of ORM in Java

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

Features

  • Dynamic code generation with JSR 269 for Java API of database access
  • Full Java API of database access without coding
  • Dynamically SQL programming with Java syntax, and very close to SQL syntax

Installation

If you are using Maven just add the following dependency to your pom.xml:

<dependency>
    <groupId>com.github.braisdom</groupId>
    <artifactId>objective-sql</artifactId>
    <version>1.3.8</version>
</dependency>

Installing IntelliJ Plugin: Preferences/Settings -> Plugins -> Search with "ObjectiveSql" in market -> Install

Simple SQL programming without coding

// You only define a domain model and fill related properties
@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;
}
// You will get behaviors of query, update and delete without coding.
Member.countAll();
Member.count("id > ?", 1);
Member.queryByPrimaryKey(1);
Member.queryFirst("id = ?", 1);
Member.query("id > ?", 1);
Member.queryAll();

Member newMember = new Member()
        .setNo("100000")
        .setName("Pamela")
        .setGender(1)
        .setMobile("15011112222");
Member.create(newMember);

Member[] members = new Member[]{newMember1, newMember2, newMember3};
        Member.create(members);

Member.update(1L, newMember);

Member.destroy(1L);

Member.execute(String.format("DELETE FROM %s WHERE name = 'Mary'", Member.TABLE_NAME));

...
Member.queryAll(Member.HAS_MANY_ORDERS);
Member.queryPrimary(1, Member.HAS_MANY_ORDERS);
Member.queryByName("demo", Member.HAS_MANY_ORDERS);
...

Complex SQL programming

Order.Table orderTable = Order.asTable();
Select select = new Select();

select.project(sum(orderTable.amount) / sum(orderTable.quantity) * 100)
    .from(orderTable)
    .groupBy(orderTable.productId);
SELECT SUM(order.amount) / SUM(order.quantity)  * 100
      FROM orders AS order GROUP BY order.product_id
Member.Table member = Member.asTable();
Order.Table order = Order.asTable();

Select select = new Select();

select.from(order, member)
        .where(order.memberId.eq(member.id));
select.project(member.no,
        member.name,
        member.mobile,
        countDistinct(order.no).as("order_count"),
        sum(order.quantity).as("total_quantity"),
        sum(order.amount).as("total_amount"),
        min(order.salesAt).as("first_shopping"),
        max(order.salesAt).as("last_shopping"));
select.groupBy(member.no, member.name, member.mobile);
SELECT
        `T0`.`no` ,
        `T0`.`name` ,
        `T0`.`mobile` ,
        COUNT(DISTINCT `T1`.`no` ) AS `order_count`,
        SUM(`T1`.`quantity` ) AS `total_quantity`,
        SUM(`T1`.`amount` ) AS `total_amount`,
        MIN(`T1`.`sales_at` ) AS `first_shopping`,
        MAX(`T1`.`sales_at` ) AS `last_shopping`
FROM `orders` AS `T1`, `members` AS `T0`
WHERE (`T1`.`member_id` = `T0`.`id` )
GROUP BY `T0`.`no` , `T0`.`name` , `T0`.`mobile`
  1. Java syntax very close to SQL syntax

  2. SQL program will be changed to logical program, resuable and procedural



from Hacker News https://ift.tt/32UnU7c

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.