2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.persistence
.dao
.common
;
12 import java
.util
.Collection
;
13 import java
.util
.List
;
16 import java
.util
.UUID
;
18 import org
.hibernate
.LockOptions
;
19 import org
.hibernate
.Session
;
20 import org
.hibernate
.criterion
.Criterion
;
21 import org
.springframework
.dao
.DataAccessException
;
23 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
24 import eu
.etaxonomy
.cdm
.persistence
.dao
.initializer
.IBeanInitializer
;
25 import eu
.etaxonomy
.cdm
.persistence
.dto
.MergeResult
;
26 import eu
.etaxonomy
.cdm
.persistence
.query
.Grouping
;
27 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
28 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
29 import javassist
.tools
.rmi
.ObjectNotFoundException
;
32 * An data access interface that all data access classes implement
34 * @since 02-Nov-2007 19:36:10
36 public interface ICdmEntityDao
<T
extends CdmBase
> {
38 public UUID
saveOrUpdate(T transientObject
) throws DataAccessException
;
40 public <S
extends T
> S
save(S newInstance
) throws DataAccessException
;
42 public T
merge(T transientObject
) throws DataAccessException
;
45 * This method allows for the possibility of returning the input transient
46 * entity instead of the merged persistent entity
48 * WARNING : This method should never be used when the objective of the merge
49 * is to attach to an existing session which is the standard use case.
50 * This method should only be used in the case of an external call which does
51 * not use hibernate sessions and is only interested in the entity as a POJO.
52 * This method returns the root merged transient entity as well as all newly merged
53 * persistent entities within the return object.
55 * @param transientObject
56 * @param returnTransientEntity
57 * @return transient or persistent object depending on the value of returnTransientEntity
58 * @throws DataAccessException
60 public MergeResult
<T
> merge(T transientObject
, boolean returnTransientEntity
) throws DataAccessException
;
63 * Obtains the specified LockMode on the supplied object
67 * @throws DataAccessException
69 public void lock(T t
, LockOptions lockOptions
) throws DataAccessException
;
72 * Globally replace all references to instance t1 with t2 (including
74 * NOTE: This replaces all non-bidirectional relationships where type T is on the
75 * "owning" side of the relationship (since the "owned" objects are, in theory,
76 * sub-components of the entity and this kind of global replace doesn't really make sense
78 * Consequently it is a good idea to either map such owned relationships with cascading
79 * semantics (i.e. CascadeType.DELETE, @OneToMany(orphanRemoval=true)) allowing them to be saved,
80 * updated, and deleted along with the owning entity automatically.
82 * @param x the object to replace, must not be null
83 * @param y the object that will replace. If y is null, then x will be removed from all collections
84 * and all properties that refer to x will be replaced with null
85 * @return T the replaced object
87 public T
replace(T x
, T y
);
90 * Refreshes the state of the supplied object using the given LockMode (e.g. use LockMode.READ
91 * to bypass the second-level cache and session cache and query the database directly)
93 * All bean properties given in the <code>propertyPaths</code> parameter are recursively initialized.
95 * For detailed description and examples <b>please refer to:</b>
96 * {@link IBeanInitializer#initialize(Object, List)}
100 * @param propertyPaths
101 * @throws DataAccessException
103 public void refresh(T t
, LockOptions lockOptions
, List
<String
> propertyPaths
) throws DataAccessException
;
105 public void clear() throws DataAccessException
;
107 public Session
getSession() throws DataAccessException
;
109 public Map
<UUID
, T
> saveAll(Collection
<?
extends T
> cdmObjCollection
) throws DataAccessException
;
111 public Map
<UUID
, T
> saveOrUpdateAll(Collection
<T
> cdmObjCollection
);
114 * @param transientObject
116 * @throws DataAccessException
118 public UUID
update(T transientObject
) throws DataAccessException
;
121 * @param persistentObject
123 * @throws DataAccessException
125 public UUID
refresh(T persistentObject
) throws DataAccessException
;
128 * @param persistentObject
130 * @throws DataAccessException
132 public UUID
delete(T persistentObject
) throws DataAccessException
;
135 * Returns a sublist of CdmBase instances stored in the database.
136 * A maximum of 'limit' objects are returned, starting at object with index 'start'.
137 * @param limit the maximum number of entities returned (can be null to return all entities)
140 * @throws DataAccessException
142 public List
<T
> list(Integer limit
, Integer start
) throws DataAccessException
;
145 * Returns a sublist of CdmBase instances stored in the database. A maximum
146 * of 'limit' objects are returned, starting at object with index 'start'.
149 * the maximum number of entities returned (can be null to return
153 * Supports path like <code>orderHints.propertyNames</code> which
154 * include *-to-one properties like createdBy.username or
155 * authorTeam.persistentTitleCache
157 * @throws DataAccessException
159 public List
<T
> list(Integer limit
, Integer start
, List
<OrderHint
> orderHints
);
163 * Returns a sublist of CdmBase instances stored in the database. A maximum
164 * of 'limit' objects are returned, starting at object with index 'start'.
168 * the maximum number of entities returned (can be null to return
172 * Supports path like <code>orderHints.propertyNames</code> which
173 * include *-to-one properties like createdBy.username or
174 * authorTeam.persistentTitleCache
176 * @throws DataAccessException
178 public <S
extends T
> List
<S
> list(Class
<S
> type
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
);
181 * Returns a sublist of CdmBase instances stored in the database. A maximum
182 * of 'limit' objects are returned, starting at object with index 'start'.
183 * The bean properties specified by the parameter <code>propertyPaths</code>
184 * and recursively initialized for each of the entities in the resultset
186 * For detailed description and examples regarding
187 * <code>propertyPaths</code> <b>please refer to:</b>
188 * {@link IBeanInitializer#initialize(Object, List)}
191 * the maximum number of entities returned (can be null to return
195 * Supports path like <code>orderHints.propertyNames</code> which
196 * include *-to-one properties like createdBy.username or
197 * authorTeam.persistentTitleCache
198 * @param propertyPaths
200 * @throws DataAccessException
202 public List
<T
> list(Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
);
205 * Returns a list of Cdm entities stored in the database filtered by the restrictions defined by
206 * the <code>parameters</code> <code>propertyName</code>, value and <code>matchMode</code>
208 * of 'limit' objects are returned, starting at object with index 'start'.
209 * The bean properties specified by the parameter <code>propertyPaths</code>
210 * and recursively initialized for each of the entities in the resultset
212 * For detailed description and examples regarding
213 * <code>propertyPaths</code> <b>please refer to:</b>
214 * {@link IBeanInitializer#initialize(Object, List)}
217 * Restrict the query to objects of a certain class, or null for
218 * all objects of type T or subclasses
219 * @param restrictions
220 * This defines a filter for multiple properties represented by the map keys. Sine the keys are of the type
221 * {@link Restriction} for each property a single MatchMode is defined. Multiple alternative values
222 * can be supplied per property, that is the values per property are combined with OR. The per property
223 * restrictions are combined with AND. </br>
224 * <b>NOTE:</b> For non string type properties you must use
225 * {@link MatchMode#EXACT}. If set <code>null</code> {@link MatchMode#EXACT} will be used
228 * the maximum number of entities returned (can be null to return
231 * The list of criterion objects representing the restriction to be applied.
233 * Supports path like <code>orderHints.propertyNames</code> which
234 * include *-to-one properties like createdBy.username or
235 * authorTeam.persistentTitleCache
236 * @param propertyPaths
238 * @throws DataAccessException
240 public <S
extends T
> List
<S
> list(Class
<S
> type
, List
<Restriction
<?
>> restrictions
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
);
243 * Counts the Cdm entities matching the restrictions defined by
244 * the <code>parameters</code> <code>propertyName</code>, value and <code>matchMode</code>.
247 * Restrict the query to objects of a certain class, or null for
248 * all objects of type T or subclasses
249 * @param restrictions
250 * This defines a filter for multiple properties represented by the map keys. Sine the keys are of the type
251 * {@link Restriction} for each property a single MatchMode is defined. Multiple alternative values
252 * can be supplied per property, that is the values per property are combined with OR. The per property
253 * restrictions are combined with AND. </br>
254 * <b>NOTE:</b> For non string type properties you must use
255 * {@link MatchMode#EXACT}. If set <code>null</code> {@link MatchMode#EXACT} will be used
258 * The list of criterion objects representing the restriction to be applied.
262 public long count(Class
<?
extends T
> type
, List
<Restriction
<?
>> restrictions
);
265 * Returns a sublist of CdmBase instances of type <TYPE> stored in the database.
266 * A maximum of 'limit' objects are returned, starting at object with index 'start'.
267 * @param limit the maximum number of entities returned (can be null to return all entities)
270 * @throws DataAccessException
272 public <S
extends T
> List
<S
> list(Class
<S
> type
, Integer limit
, Integer start
) throws DataAccessException
;
275 * Returns a sublist of objects matching the grouping projections supplied using the groups parameter
277 * It would be nice to have an equivalent countGroups method, but for the moment hibernate doesn't
278 * seem to support this (HHH-3238 - impossible to get the rowcount for a criteria that has projections)
280 * @param clazz Restrict the query to objects of a certain class, or null for all objects of type T or subclasses
281 * @param limit the maximum number of entities returned (can be null to return
283 * @param start The (0-based) offset from the start of the recordset
284 * @param groups The grouping objects representing a projection, plus an optional ordering on that projected property
285 * @param propertyPaths paths initialized on the returned objects - only applied to the objects returned from the first grouping
286 * @return a list of arrays of objects, each matching the grouping objects supplied in the parameters.
288 public List
<Object
[]> group(Class
<?
extends T
> clazz
,Integer limit
, Integer start
, List
<Grouping
> groups
, List
<String
> propertyPaths
);
293 * @throws DataAccessException
295 public T
findById(int id
) throws DataAccessException
;
298 * Finds the cdm entity specified by the id parameter and
299 * recursively initializes all bean properties given in the
300 * <code>propertyPaths</code> parameter.
302 * For detailed description and examples <b>please refer to:</b>
303 * {@link IBeanInitializer#initialize(Object, List)}
306 * @param propertyPaths properties to be initialized
309 public T
load(int id
, List
<String
> propertyPaths
);
312 * Returns the object for the given id without initializing it. So the returned
313 * object usually is a proxy object except for the case when it was already initialized
314 * before in the same session.<BR>
315 * This methods wraps {@link Session#load(Class, java.io.Serializable)}.<BR>
316 * It does not check, if the object really exists but throws an {@link ObjectNotFoundException}
317 * exception when no record with the given id exists in the database.
319 * the (uninitialized proxy) object
321 public T
loadWithoutInitializing(int id
);
323 public List
<T
> loadList(Collection
<Integer
> ids
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) throws DataAccessException
;
325 public T
findByUuid(UUID Uuid
) throws DataAccessException
;
328 * Method to find CDM Entity by Uuid, by making sure that the underlying
329 * hibernate session is not flushed (Session.FLUSH_MODE set to MANUAL temporarily)
330 * when performing the read query.
334 * @throws DataAccessException
336 public T
findByUuidWithoutFlush(UUID uuid
) throws DataAccessException
;
338 public List
<T
> list(Collection
<UUID
> uuids
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) throws DataAccessException
;
340 public <S
extends T
> List
<S
> list(Class
<S
> clazz
, Collection
<UUID
> uuids
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) throws DataAccessException
;
343 * Finds the cdm entity specified by the <code>uuid</code> parameter and
344 * initializes all its *ToOne relations.
346 public T
load(UUID uuid
);
349 * Finds the cdm entity specified by the <code>uuid</code> parameter and
350 * recursively initializes all bean properties given in the
351 * <code>propertyPaths</code> parameter.
353 * For detailed description and examples <b>please refer to:</b>
354 * {@link IBeanInitializer#initialize(Object, List)}
357 * @param propertyPaths properties to be initialized
360 public T
load(UUID uuid
, List
<String
> propertyPaths
);
365 * @throws DataAccessException
367 public Boolean
exists(UUID uuid
) throws DataAccessException
;
372 * Returns the number of objects of type <T> - which must extend T
377 public long count(Class
<?
extends T
> clazz
);
380 * FIXME Should this method exist : I would expect flushing of a session to be
381 * something that a DAO should hide?
386 * Convenience method which makes it easy to discover what type of object this DAO returns at runtime
390 public Class
<T
> getType();
393 * Method that counts the number of objects matching the example provided.
394 * The includeProperties property is used to specify which properties of the example are used.
396 * If includeProperties is null or empty, then all literal properties are used (restrictions are
397 * applied as in the Hibernate Query-By-Example API call Example.create(object)).
399 * If includeProperties is not empty then only literal properties that are named in the set are used to
400 * create restrictions, *PLUS* any *ToOne related entities. Related entities are matched on ID, not by
401 * their internal literal values (e.g. the call is criteria.add(Restrictions.eq(property,relatedObject)), not
402 * criteria.createCriteria(property).add(Example.create(relatedObject)))
405 * @param includeProperties
406 * @return a count of matching objects
408 public long count(T example
, Set
<String
> includeProperties
);
411 * Method that lists the objects matching the example provided.
412 * The includeProperties property is used to specify which properties of the example are used.
414 * If includeProperties is null or empty, then all literal properties are used (restrictions are
415 * applied as in the Hibernate Query-By-Example API call Example.create(object)).
417 * If includeProperties is not empty then only literal properties that are named in the set are used to
418 * create restrictions, *PLUS* any *ToOne related entities. Related entities are matched on ID, not by
419 * their internal literal values (e.g. the call is criteria.add(Restrictions.eq(property,relatedObject)), not
420 * criteria.createCriteria(property).add(Example.create(relatedObject)))
423 * @param includeProperties
424 * @param limit the maximum number of entities returned (can be null to return
426 * @param start The (0-based) offset from the start of the recordset
428 * Supports path like <code>orderHints.propertyNames</code> which
429 * include *-to-one properties like createdBy.username or
430 * @param propertyPaths paths initialized on the returned objects - only applied to the objects returned from the first grouping
431 * @return a list of matching objects
433 public <S
extends T
> List
<S
> list(S example
, Set
<String
> includeProperties
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
);
435 public <S
extends T
> List
<S
> findByParamWithRestrictions(Class
<S
> clazz
, String param
, String queryString
, MatchMode matchmode
, List
<Restriction
<?
>> restrictions
, Integer pageSize
,
436 Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
);
438 public long countByParamWithRestrictions(Class
<?
extends T
> clazz
, String param
, String queryString
, MatchMode matchmode
, List
<Restriction
<?
>> restrictions
);
440 public long countByParam(Class
<?
extends T
> clazz
, String param
, String queryString
, MatchMode matchmode
, List
<Criterion
> criterion
);
442 public <S
extends T
> List
<S
> findByParam(Class
<S
> clazz
, String param
, String queryString
, MatchMode matchmode
, List
<Criterion
> criterion
, Integer pageSize
, Integer pageNumber
,
443 List
<OrderHint
> orderHints
, List
<String
> propertyPaths
);
445 public <S
extends T
> List
<S
> findByParam(Class
<S
> clazz
, Set
<String
> params
, String queryString
, MatchMode matchmode
,
446 List
<Criterion
> criterion
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
,
447 List
<String
> propertyPaths
);