Using Easy-Query ORM for Strongly Typed OLTP and OLAP Queries in Java
This article introduces Easy-Query, a Java ORM that offers strong‑typed OLTP and OLAP query capabilities, demonstrates how to define entity classes with many‑to‑many and one‑to‑one relationships, and provides multiple code examples for complex queries, DTO generation, and automatic inclusion of related data.
Easy-Query is a Java ORM that provides strong‑typed OLTP and OLAP query capabilities, allowing developers to write type‑safe queries without writing raw SQL.
It supports many‑to‑many and one‑to‑one relationships, automatic joins, and can generate SQL for complex queries such as filtering users by city, selecting specific fields, counting related entities, and applying aggregate functions.
Entity definitions (excerpt):
@Table("t_user") @Data @EntityProxy public class SysUser implements ProxyEntityAvailable<SysUser, SysUserProxy> { @Column(primaryKey = true) private String id; private String name; private LocalDateTime createTime; @Navigate(value = RelationTypeEnum.ManyToMany, mappingClass = UserRole.class, selfMappingProperty = "userId", targetMappingProperty = "roleId") private List<SysRole> roles; @Navigate(value = RelationTypeEnum.OneToOne, targetProperty = "userId") private SysUserAddress address; @Override public Class<SysUserProxy> proxyTableClass() { return SysUserProxy.class; } } @Table("t_role") @Data @EntityProxy public class SysRole implements ProxyEntityAvailable<SysRole, SysRoleProxy> { @Column(primaryKey = true) private String id; private String name; private LocalDateTime createTime; @Navigate(value = RelationTypeEnum.ManyToMany, mappingClass = UserRole.class, selfMappingProperty = "roleId", targetMappingProperty = "userId") private List<SysUser> users; @Navigate(value = RelationTypeEnum.ManyToMany, mappingClass = RoleMenu.class, selfMappingProperty = "roleId", targetMappingProperty = "menuId") private List<SysMenu> menus; @Override public Class<SysRoleProxy> proxyTableClass() { return SysRoleProxy.class; } }Query examples:
Query users in Hangzhou or Shaoxing:
List<SysUser> userInHz = easyEntityQuery.queryable(SysUser.class) .where(s -> { s.or(() -> { s.address().city().eq("杭州市"); s.address().city().eq("绍兴市"); }); }).toList();Corresponding SQL:
SELECT `id`, `name`, `create_time` FROM `t_user` t LEFT JOIN `t_user_address` t1 ON t1.`user_id` = t.`id` WHERE (t1.`city` = '杭州市' OR t1.`city` = '绍兴市');Query a user named "小明" and return his name and address:
List<Draft2<String, String>> userNameAndAddr = easyEntityQuery.queryable(SysUser.class) .where(s -> s.name().eq("小明")) .select(s -> Select.DRAFT.of(s.name(), s.address().addr())) .toList();Query a user named "小明" and also return the count of his roles:
List<Draft3<String, String, Long>> userNameAndAddrAndRoleCount = easyEntityQuery.queryable(SysUser.class) .where(s -> s.name().eq("小明")) .select(s -> Select.DRAFT.of(s.name(), s.address().addr(), s.roles().count())) .toList();Other cases demonstrate filtering by role name, counting roles, using aggregate functions like MAX, and pagination of related collections.
DTO generation with EasyQueryAssistant:
The IDE plugin can generate structured DTO classes that map the entity hierarchy automatically.
@Data public class UserRoleMenuDTO { private String id; private String name; @Navigate(value = RelationTypeEnum.ManyToMany) private List<InternalRoles> roles; public static class InternalRoles { private String id; private String name; @Navigate(value = RelationTypeEnum.ManyToMany) private List<InternalMenus> menus; } public static class InternalMenus { private String id; private String name; private String route; private String icon; } }Using selectAutoInclude the generated DTO can be populated directly:
List<UserRoleMenuDTO> result = easyEntityQuery.queryable(SysUser.class) .where(u -> { u.name().like("小明"); u.createTime().rangeClosed(LocalDateTime.now().minusDays(100), LocalDateTime.now()); }) .selectAutoInclude(UserRoleMenuDTO.class) .toList();Overall, Easy-Query demonstrates a powerful, zero‑dependency, fully open‑source solution for both OLTP and OLAP scenarios in Java, supporting complex joins, aggregates, and automatic DTO mapping.
Promotional note: Scanning the QR code in the article allows free acquisition of a programmer’s book and source code for a book‑management system.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
