針對JPA的活動記錄模式: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", "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<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", "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<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);
....
....
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!