01: /*
02: * JFox - The most lightweight Java EE Application Server!
03: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
04: *
05: * JFox is licenced and re-distributable under GNU LGPL.
06: */
07: package org.jfox.entity.dao;
08:
09: import java.util.HashMap;
10: import java.util.Map;
11: import javax.persistence.EntityManager;
12:
13: import org.jfox.entity.EntityFactory;
14: import org.jfox.entity.MappedEntity;
15: import org.jfox.entity.QueryExt;
16:
17: /**
18: * DAOSupport,�装了 DAO 的基本�作
19: * 需è¦?ç”±å?ç±»æ??ä¾›EntityManager,在 getEntityManager方法ä¸è¿”回
20: *
21: * DAO 应该都从 DAOSupport 继承,并且实现为 Stateless Local SessionBean
22: *
23: * DAO å?¯ä»¥è„±ç¦»å®¹å™¨è¿?行,æ¤æ—¶ä¸?能使用@PersistenceContext注入,
24: * 而å?ªèƒ½é€šè¿‡javax.persistence.Persistenceçš„é?™æ€?方法æ?¥æž„é€
25: *
26: * @author <a href="mailto:jfox.young@gmail.com">Yang Yong</a>
27: */
28: public abstract class DAOSupport implements DataAccessObject {
29:
30: /**
31: * 返回 EntityManager,由å?类使用 @PersistenceContext 注入
32: */
33: protected abstract EntityManager getEntityManager();
34:
35: /**
36: * æ ¹æ?® Entity Class生æˆ? Entity对象
37: *
38: * @param entityClass entity class
39: */
40: public static <T> T newEntityObject(Class<T> entityClass) {
41: return newEntityObject(entityClass,
42: new HashMap<String, Object>());
43: }
44:
45: /**
46: * 使用数� Map,生� Entity对象,Map的key和Entity的Column对应
47: *
48: * @param entityClass entity class
49: * @param dataMap data dataMap
50: */
51: public static <T> T newEntityObject(final Class<T> entityClass,
52: final Map<String, Object> dataMap) {
53: return EntityFactory.newEntityObject(entityClass, dataMap);
54: }
55:
56: /**
57: * 给定query name 创建 NamedNativeQeury
58: * @param queryName query name
59: */
60: public QueryExt createNamedNativeQuery(String queryName) {
61: return (QueryExt) getEntityManager()
62: .createNamedQuery(queryName);
63: }
64:
65: /**
66: * 给定 sql æž„é€ Qeury,Result类型为MappedEntity
67: * @param sql native sql template
68: */
69: public QueryExt createNativeQuery(String sql) {
70: return createNativeQuery(sql, MappedEntity.class);
71: }
72:
73: /**
74: * 给定 sql æž„é€ Qeury,Result类型为resultClass
75: * @param sql native sql template
76: * @param resultClass 返回的结果对象类型
77: */
78: public QueryExt createNativeQuery(String sql, Class<?> resultClass) {
79: return (QueryExt) getEntityManager().createNativeQuery(sql,
80: resultClass);
81: }
82:
83: /**
84: * 生�19的PK,比如:2006080816404856650
85: * PK å?ªä¿?è¯?唯一,ä¸?包å?«ä»»ä½•ä¸šåŠ¡æ„?义,比如:对于 ID è¿žç»æ€§çš„è¦?求
86: */
87: public long nextPK() {
88: return PKGenerator.getInstance().nextPK();
89: }
90:
91: }
|