KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > petstore > dao > ItemDAOImpl


1 /*
2  * JFox - The most lightweight Java EE Application Server!
3  * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
4  *
5  * JFox is licenced and re-distributable under GNU LGPL.
6  */

7 package org.jfox.petstore.dao;
8
9 import java.sql.SQLException JavaDoc;
10 import java.util.List JavaDoc;
11 import javax.ejb.Stateless JavaDoc;
12 import javax.ejb.Local JavaDoc;
13 import javax.persistence.NamedNativeQueries;
14 import javax.persistence.NamedNativeQuery;
15 import javax.persistence.PersistenceContext;
16 import javax.persistence.EntityManager;
17
18 import org.jfox.petstore.entity.Item;
19 import org.jfox.petstore.entity.Order;
20 import org.jfox.entity.EntityManagerExt;
21 import org.jfox.entity.dao.DAOSupport;
22
23 /**
24  * @author <a HREF="mailto:jfox.young@gmail.com">Young Yang</a>
25  */

26 @NamedNativeQueries(
27         {
28         @NamedNativeQuery(
29                 name = ItemDAOImpl.GET_ITEM_LIST_BY_PRODUCT,
30                 query = "select itemid, listprice, unitcost, supplier, i.productid, name, descn, category, status, attr1, attr2, attr3, attr4, attr5 from item i, product p where p.productid = i.productid and i.productid = $productId",
31                 resultClass = Item.class
32         ),
33         @NamedNativeQuery(
34                 name = ItemDAOImpl.GET_ITEM,
35                 query = "select i.itemid, listprice, unitcost, supplier, i.productid, name, descn, category, status, attr1, attr2, attr3, attr4, attr5, qty from item i, inventory v, product p where p.productid = i.productid and i.itemid = v.itemid and i.itemid = $id",
36                 resultClass = Item.class
37         ),
38         @NamedNativeQuery(
39                 name = ItemDAOImpl.GET_INVENTORY_QUANTITY,
40                 query = "select qty from inventory where itemid = $itemId",
41                 resultClass = Integer JavaDoc.class
42         ),
43         @NamedNativeQuery(
44                 name = "updateInventoryQuantity",
45                 query = "update inventory set qty = qty - increment where itemid = $itemid"
46         )
47                 }
48 )
49 @Stateless JavaDoc
50 @Local JavaDoc
51 public class ItemDAOImpl extends DAOSupport implements ItemDAO {
52
53     public static final String JavaDoc GET_ITEM_LIST_BY_PRODUCT = "getItemListByProduct";
54     public static final String JavaDoc GET_ITEM = "getItem";
55     public static final String JavaDoc GET_INVENTORY_QUANTITY = "getInventoryQuantity";
56
57     @PersistenceContext(unitName = "JPetstoreMysqlDS")
58     EntityManagerExt em;
59
60     protected EntityManager getEntityManager() {
61         return em;
62     }
63
64     public Item getItem(String JavaDoc itemId) throws SQLException JavaDoc {
65         return (Item)createNamedNativeQuery(GET_ITEM).setParameter("id",itemId).getSingleResult();
66     }
67
68     public List JavaDoc<Item> getItemListByProduct(String JavaDoc productId) throws SQLException JavaDoc {
69         return (List JavaDoc<Item>)createNamedNativeQuery(GET_ITEM_LIST_BY_PRODUCT).setParameter("productId", productId).getResultList();
70     }
71
72     public int getInventoryQuantity(String JavaDoc itemId) throws SQLException JavaDoc {
73         return (Integer JavaDoc)createNamedNativeQuery(GET_INVENTORY_QUANTITY).setParameter("itemId",itemId).getSingleResult();
74     }
75
76     public void updateQuantity(Order order) throws SQLException JavaDoc {
77     }
78 }
79
Popular Tags