微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ActiveJPA 针对JPA的活动记录模式

程序名称:ActiveJPA

授权协议: Apache

操作系统: 跨平台

开发语言: Java

ActiveJPA 介绍

ActiveJPA基于JPA,提供了Martin Fowler所提出的活动记录模式(Active Record
pattern)的Java实现。借助于ActiveJPA,模型本身会作为DAO并与数据库交互,这样就不需要额外的代码作为数据访问层了。

ActiveJPA使用到了JPA规范,因此所有JPA的ORM实现(Hibernate、EclipseLink、OpenJPA等)都可以与ActiveJPA协同使用。

示例代码

// 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", "[email protected]", "status", "shipped");

// Get all orders for the product category 'books' and paginate it
Filter filter = new Filter();
filter.setPageNo(1);
filter.setPerPage(25);
filter.addCondition(new Condition("orderItems.product.category", Operator.eq, "books");
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", "[email protected]", "status", "shipped");

// Get the unique order matching the conditions
Long count = Order.one("customer_email", "[email protected]", "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
order.setBillingAmount(1000.0);
order.persist();

// Delete order
order.delete();

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

// Find order item by id within an order
order.collections("order_items").findById(123L);

// Search order items by filter with an order
order.collections("order_items").findById(filter);

....
....

ActiveJPA 官网

https://github.com/ActiveJpa/activejpa

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐