001package springdao; 002 003import java.io.Serializable; 004import java.util.Collection; 005import javax.persistence.ManyToOne; 006import java.util.List; 007import java.util.Map; 008 009/** 010 * 011 * @author Kent Yeh 012 */ 013public interface DaoRepository<E> { 014 015 /** 016 * return type of handle object.<br/> 017 * 取回DAO物件真正對應所要處理的物件 018 * 019 * @return target object class. 020 */ 021 public Class<E> getClazz(); 022 023 /** 024 * build a instance of <E>. 025 * 026 * @return instance of {@link #getClazz() getClazz()} 027 * @throws java.lang.InstantiationException 028 * @throws java.lang.IllegalAccessException 029 */ 030 public E instanate() throws InstantiationException, IllegalAccessException; 031 032 /** 033 * Completely clear the session. Evict all loaded instances and cancel all 034 * pending saves, updates and deletions. Do not close open iterators or 035 * instances of ScrollableResults. 036 */ 037 public void clear(); 038 039 /** 040 * Check if this instance is associated with this Session. 041 * 042 * @param entity 043 * @return True/False 044 */ 045 boolean contains(Object entity); 046 047 /** 048 * Find entity with serializable primaryKey.<br/> 049 * 以主鍵找尋物件 050 * 051 * @param primaryKey 主鍵 052 * @return entity/物件 053 */ 054 public E findByPrimaryKey(Serializable primaryKey); 055 056 /** 057 * Find entity with serializable id and lockMode.<br/> 058 * 059 * @param primaryKey 060 * @param lockMode 061 * @return entity/物件 062 */ 063 public E findByPrimaryKey(Serializable primaryKey, String lockMode); 064 065 public E findByPrimaryKey(Serializable primaryKey, Map<String, Object> properties); 066 067 public E findByPrimaryKey(Serializable primaryKey, String lockMode, Map<String, Object> properties); 068 069 /** 070 * Save entiry 儲存物件 071 * 072 * @param entity entity(物件) 073 * @return saved entity/儲存物件 074 */ 075 public E save(E entity); 076 077 /** 078 * Save multipal entities.<br/> 079 * 一次儲存多個物件 080 * 081 * @param entities 082 * @return saved entities/儲存物件 083 */ 084 public Collection<E> save(Collection<E> entities); 085 086 /* 087 * Make an instance managed and persistent. 088 */ 089 public E persist(E entity); 090 091 /** 092 * update entity<br/> 093 * 更新物件 094 * 095 * @param entity 物件 096 * @return merged entity/更新後的物件 097 */ 098 public E update(E entity); 099 100 /** 101 * update multipal entities<br/> 102 * 一次更新多個物件 103 * 104 * @param entities 105 * @return merged entities/更新後的物件 106 */ 107 public Collection<E> update(Collection<E> entities); 108 109 public E merge(E entity); 110 111 public Collection<E> merge(Collection<E> entities); 112 113 /** 114 * Save or update entity<br/> 115 * 儲存或更新物件 116 * 117 * @param entity 物件 118 * @return merged entity/更新後的物件 119 */ 120 public E saveOrUpdate(E entity); 121 122 /** 123 * Save or update multipal entities<br/> 124 * 一次儲存或更新多個物件 125 * 126 * @param entities 127 * @return merged entities/更新後的物件 128 */ 129 public Collection<E> saveOrUpdate(Collection<E> entities); 130 131 /** 132 * Delete entity by primary key.<br/> 133 * 藉由主鍵刪除物件<br/> 134 * <div style="color:red;font-weight:bold"> Notice:</div> 135 * A child entity with a {@link ManyToOne @ManyToOne} reference to a 136 * parent entity can't be delete directed by {@link Dao @Dao} or 137 * {@link DaoManager @DaoManager}, It should be delete like<br/> 138 * <div style="color:red;font-weight:bold"> 注意:</div> 139 * 當子物件包含{@link ManyToOne @ManyToOne}參考到父物件時,無法直接由 140 * {@link Dao @Dao} or 141 * {@link DaoManager @DaoManager}刪除,必須以下方代碼進行子物件刪除 142 * <pre> 143 * Parent parent = parentDao.merge(child.getParent()); 144 * child.setParent(null); 145 * parent.getChildren().remove(child); 146 * parentDao.merge(parent); 147 * </pre> Or it can be deleted by issue a blukUpdate command instead like 148 * <br/> 149 * 或是使用以下代碼刪除<br/> 150 * <div style="color:blue"><code>childDao.blukUpdate("DELETE FROM "+getEntityName()+" WHERE id=?",child.getId())</code></div> 151 * 152 * @param primaryKey 153 */ 154 public void delete(Serializable primaryKey); 155 156 /** 157 * Delete entity with lockMode.<br/> 158 * 以指定的層級鎖定刪除物件 159 * 160 * @param primaryKey 161 * @param lockMode 162 */ 163 public void delete(Serializable primaryKey, String lockMode); 164 165 public void delete(Collection<? extends Serializable> primaryKeys); 166 167 public E remove(E entity); 168 169 public E remove(E entity, String lockMode); 170 171 public Collection<E> remove(Collection<E> entities); 172 173 /** 174 * Lock entity with lockMode.<br/> 175 * 以指定的層級鎖定物件 176 * 177 * @param entity 178 * @param lockMode 179 * @return locked entity/鎖定的物件 180 */ 181 public E lock(E entity, String lockMode); 182 183 /** 184 * Refresh the state of the instance from the database, overwriting changes 185 * made to the entity, if any.<br/> 186 * 重新讀取物件 187 * 188 * @param entity 189 * @return entity instance/物件 190 */ 191 public E refresh(E entity); 192 193 /** 194 * Refresh the state of the instance from the database, overwriting changes 195 * made to the entity, if any.<br/> 196 * 以指定的層級鎖定重新讀取物件 197 * 198 * @param entity 199 * @param lockMode 200 * @return entity instance/物件 201 */ 202 public E refresh(E entity, String lockMode); 203 204 public int sqlUpdate(String sql); 205 206 public int sqlUpdate(String sql, Object... parameters); 207 208 public int sqlUpdate(String sql, Map<String, ?> parameters); 209 210 public List<Integer> sqlUpdate(List<String> sqls); 211 212 public int bulkUpdate(String QL); 213 214 public List<Integer> bulkUpdate(List<String> QLs); 215 216 public int bulkUpdate(String QL, Object... parameters); 217 218 public int bulkUpdate(String QL, Map<String, ?> parameters); 219 220 /** 221 * returne entity name.<br/> 222 * 回傳ENTITY的名稱(類別名稱)<br/> 223 * 叫用 224 * {@link #getClazz() getClazz()}.{@link java.lang.Class#getName() getName()} 225 * 226 * @return 類別名稱 227 */ 228 public String getEntityName(); 229 230 /** 231 * alias for {@link #getEntityName() getEntityName()}. 232 * {@link #getEntityName() getEntityName()}的別名 233 * 234 * @return 類別名稱 235 */ 236 public String $e(); 237 238 /** 239 * Default alias of entity.<br/> 240 * Lowercase first character of class name.<br/> 241 * 取得預設查詢所使用的別名<br/> 242 * 實際上是等於 類別名稱的第一碼改為小寫 243 * 244 * @return alias name of target entity class 245 */ 246 public String getAliasName(); 247 248 /** 249 * alias for {@link #getAliasName() getAliasName()}. 250 * {@link #getAliasName() getAliasName()}的別名 251 * 252 * @return alias name of target entity class 253 */ 254 public String $a(); 255 256 /** 257 * return {@link #$e() $e()}+"AS"+{@link #$a() $a()}. 258 * 回傳{@link #$e() $e()}+"AS"+{@link #$a() $a()}的組合 259 * 260 * @return {@link #$e() $e()} AS {@link #$a() $a()} 261 */ 262 public String $ea(); 263 264 /** 265 * Query by criteria by invoke 266 * {@link #findByCriteria(String ,int ,int ) findByCriteria(criteria, 0, 0)}.<br/> 267 * 條件查詢<br/>會叫用{@link #findByCriteria(String ,int ,int ) findByCriteria(criteria, 0, 0)} 268 * 269 * @param qlCriteria QL 的查詢條件 270 * @return List of entities. 物件集合 271 */ 272 public List<E> findByCriteria(String qlCriteria); 273 274 /** 275 * Query by criteria.<br/> 276 * QL statement will be "from " + 277 * {@link #getEntityName() getEntityName()} + " as " + 278 * {@link #getAliasName() getAliasName()} + criteria<br/> 279 * 條件查詢所有資料<br/> 280 * 實際查詢時QL會組成 "from " + {@link #getEntityName() getEntityName()} + 281 * " as " + {@link #getAliasName() getAliasName()} + criteria 282 * 283 * @param qlCriteria QL parameter(using ? mark) 的查詢條件(參數要用 ? ) 284 * @param parameters paraemters(with ? mark). 參數(順序必須時應QL內的 ?) 285 * @return List of entities.物件集合 286 */ 287 public List<E> findByCriteria(String qlCriteria, Object... parameters); 288 289 public List<E> findByCriteria(String qlCriteria, Map<String, ?> parameters); 290 291 /** 292 * Query by criteria.<br/> 293 * QL statement will be "from " + 294 * {@link #getEntityName() getEntityName()} + " as " + 295 * {@link #getAliasName() getAliasName()} + criteria<br/> 296 * 條件查詢所有資料<br/> 297 * 實際查詢時QL會組成 "from " + {@link #getEntityName() getEntityName()} + 298 * " as " + {@link #getAliasName() getAliasName()} + criteria 299 * 300 * @param qlCriteria QL (? mark means parameter).的查詢條件(參數要用 ? ) 301 * @param startPageNo start page no.起始頁數 302 * @param pageSize page size.每頁筆數 303 * @param parameters parameters of QL(follow by sequence.) .參數(順序必須時應QL內的 ?) 304 * @return List of entities.物件集合 305 */ 306 public List<E> findByCriteria(String qlCriteria, int startPageNo, int pageSize, Object... parameters); 307 308 public List<E> findByCriteria(String qlCriteria, int startPageNo, int pageSize, Map<String, ?> parameters); 309 310 /** 311 * Query by criteria.<br/> 312 * QL statement will be "from " + 313 * {@link #getEntityName() getEntityName()} + " as " + 314 * {@link #getAliasName() getAliasName()} + criteria<br/> 315 * 條件查詢<br/> 316 * 實際查詢時QL會組成 "from " + {@link #getEntityName() getEntityName()} + 317 * " as " + {@link #getAliasName() getAliasName()} + criteria 318 * 319 * @param qlCriteria QL 的查詢條件 320 * @param startPageNo start page no.起始頁數 321 * @param pageSize page size. 每頁筆數 322 * @return List of entities.物件集合 323 */ 324 public List<E> findByCriteria(String qlCriteria, int startPageNo, int pageSize); 325 326 public List<E> findByNamedQuery(String name); 327 328 public List<E> findByNamedQuery(String name, Object... parameters); 329 330 public List<E> findByNamedQuery(String name, Map<String, ?> parameters); 331 332 /** 333 * Query by SQL.<br/> 334 * 以SQL Statement 取得查詢物件結果 335 * 336 * @param sql 337 * @return List of entities.物件集合 338 */ 339 public List<E> findBySQLQuery(String sql); 340 341 public List<E> findBySQLQuery(String sql, Object... parameters); 342 343 public List<E> findBySQLQuery(String sql, Map<String, ?> parameters); 344 345 public <T> T findUniqueByQL(String QL); 346 347 public <T> T findUniqueByQL(String QL, Object... parameters); 348 349 public <T> T findUniqueByQL(String QL, Map<String, ?> parameters); 350 351 public <T> List<T> findListByQL(String QL); 352 353 public <T> List<T> findListByQL(String QL, Object... parameters); 354 355 public <T> List<T> findListByQL(String QL, Map<String, ?> parameters); 356 357 public <T> List<T> findListByNamedQuery(String name); 358 359// public <T> List<T> findListByNamedQuery(Class<T> clazz, String name); 360 361 public <T> List<T> findListByNamedQuery(String name, Object... parameters); 362 363// public <T> List<T> findListByNamedQuery(Class<T> clazz, String name, Object... parameters); 364 365 public <T> List<T> findListByNamedQuery(String name, Map<String, ?> parameters); 366 367// public <T> List<T> findListByNamedQuery(Class<T> clazz, String name, Map<String, ?> parameters); 368 369 public E initLazyCollection(E entity, String collectionFieldName); 370}