ActiveJPA基于JPA,提供了MartiFowler所提出的活动记录模式(ActiveRecordpatter)的Java实现。借助于ActiveJPA,模型本身会作为DAO并与数据库交互,这样就不需要额外的代码作为数据访问层了。
ActiveJPA使用到了JPA规范,因此所有JPA的ORM实现(Hiberate、EclipseLik、OpeJPA等)都可以与ActiveJPA协同使用。
示例代码:
// Get order by idOrder order = Order.fidById(12345L);// Get all orders for a customer that are shippedList<Order> orders = Order.where("customer_email", "dummyemail@dummy.com", "status", "shipped");// Get all orders for the product category 'books' ad pagiate itFilter filter = ew Filter();filter.setPageNo(1);filter.setPerPage(25);filter.addCoditio(ew Coditio("orderItems.product.category", Operator.eq, "books");List<Order> orders = Order.where(filter);// Cout of orders matchig the filterLog cout = Order.cout(filter);// Get the first order matchig the filterLog cout = Order.first("customer_email", "dummyemail@dummy.com", "status", "shipped");// Get the uique order matchig the coditiosLog cout = Order.oe("customer_email", "dummyemail@dummy.com", "status", "shipped");// Dump everythigList<Order> orders = Order.all();// Delete all orders matchig the filterLog cout = Order.deleteAll(filter);// Check if order exists with the give idetifierboolea exists = Order.exists(1234L);// Save orderorder.setBilligAmout(1000.0);order.persist();// Delete orderorder.delete();// Update attributesMap<Strig, Object> attributes = ew HashMap<Strig, Object>();attributes.put("billigAmout", 1000.0);order.updateAttributes(attributes);// Fid order item by id withi a orderorder.collectios("order_items").fidById(123L);// Search order items by filter with a orderorder.collectios("order_items").fidById(filter);........






评论