当前位置: 软件>java软件
针对JPA的活动记录模式 ActiveJPA
本文导语: ActiveJPA基于JPA,提供了Martin Fowler所提出的活动记录模式(Active Record pattern)的Java实现。借助于ActiveJPA,模型本身会作为DAO并与数据库交互,这样就不需要额外的代码作为数据访问层了。 ActiveJPA使用到了JPA规范,因此所有JPA的O...
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 orders = Order.where("customer_email", "dummyemail@dummy.com", "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 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", "dummyemail@dummy.com", "status", "shipped");
// Get the unique order matching the conditions
Long count = Order.one("customer_email", "dummyemail@dummy.com", "status", "shipped");
// Dump everything
List 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 attributes = new HashMap();
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);
....
....
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。