1 7 package org.jfox.petstore.dao; 8 9 import java.sql.SQLException ; 10 import java.util.List ; 11 import javax.ejb.Stateless ; 12 import javax.ejb.Local ; 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 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 .class 42 ), 43 @NamedNativeQuery( 44 name = "updateInventoryQuantity", 45 query = "update inventory set qty = qty - increment where itemid = $itemid" 46 ) 47 } 48 ) 49 @Stateless 50 @Local 51 public class ItemDAOImpl extends DAOSupport implements ItemDAO { 52 53 public static final String GET_ITEM_LIST_BY_PRODUCT = "getItemListByProduct"; 54 public static final String GET_ITEM = "getItem"; 55 public static final String 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 itemId) throws SQLException { 65 return (Item)createNamedNativeQuery(GET_ITEM).setParameter("id",itemId).getSingleResult(); 66 } 67 68 public List <Item> getItemListByProduct(String productId) throws SQLException { 69 return (List <Item>)createNamedNativeQuery(GET_ITEM_LIST_BY_PRODUCT).setParameter("productId", productId).getResultList(); 70 } 71 72 public int getInventoryQuantity(String itemId) throws SQLException { 73 return (Integer )createNamedNativeQuery(GET_INVENTORY_QUANTITY).setParameter("itemId",itemId).getSingleResult(); 74 } 75 76 public void updateQuantity(Order order) throws SQLException { 77 } 78 } 79 | Popular Tags |