1 /*******************************************************************************
2 * Gisgraphy Project
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17 *
18 * Copyright 2008 Gisgraphy project
19 * David Masclet <davidmasclet@gisgraphy.com>
20 *
21 *
22 *******************************************************************************/
23 /**
24 *
25 */
26 package com.gisgraphy.domain.repository;
27
28 import java.io.Serializable;
29 import java.sql.SQLException;
30 import java.util.List;
31
32 import javax.persistence.EntityManager;
33 import javax.persistence.FlushModeType;
34
35 import org.hibernate.FlushMode;
36 import org.hibernate.HibernateException;
37 import org.hibernate.Session;
38 import org.springframework.dao.DataAccessException;
39
40 /**
41 * Interface of a Generic (java-5 meaning) data access object
42 *
43 * @see IGisDao
44 * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
45 */
46 public interface IDao<T, PK extends Serializable> {
47
48 /**
49 * @return The class, this DAO 'process'
50 */
51 public Class<T> getPersistenceClass();
52
53 /**
54 * Generic method used to get all objects of a particular type. This is the
55 * same as lookup up all rows in a table.
56 *
57 * @return List of populated objects (never return null, but an empty list)
58 */
59 public List<T> getAll();
60
61 /**
62 * Returns all object of a particular type (i.e getAllPaginate(2,5) will
63 * return the [2,3,4,5,6] object
64 *
65 * @param from
66 * The first result to return numbered from 1
67 * @param maxResults
68 * The maximum list size to return
69 * @return List of populated objects, (never return null, but an empty list)
70 * @throws HibernateException
71 * if thrown by the Hibernate API
72 * @throws SQLException
73 * if thrown by JDBC API
74 */
75 public List<T> getAllPaginate(int from, int maxResults);
76
77 /**
78 * Flush all Memory Objects to the database, and clear the L1 cache, of the
79 * current thread-bound Hibernate Session.
80 *
81 * @throws HibernateException
82 * that Indicates problems flushing the session or talking
83 * to the database
84 */
85 public void flushAndClear();
86
87 /**
88 * Saves the passed object, and returns an attached entity. It is very very
89 * very important to use the returned entity, because of the way the
90 * underlying mechanism possibly works. For instance, when using Db4o, this
91 * is completly useless, but when using JPA and the merge method of the
92 * EntityManager, things are going to be buggy (Duplicate Key exceptions) if
93 * you don't use the returned object. Please consult EJB3 (or the concrete
94 * persistence framework doc) Spec for more information about the way the
95 * merge method works.
96 *
97 * @param o
98 * The object to save
99 * @return The saved instance
100 * @throws DataAccessException
101 * in case of errors
102 */
103 public T save(final T o);
104
105 /**
106 * Sets the flush mode (i.e. when objects are flushed to the database) of
107 * the current thread-bound session. By default, it is equivalent (in the
108 * case of JPA persistence) to {@link FlushModeType#AUTO}, which lets the
109 * persistence framework handle that issue. However, for performance
110 * reasons, it might be necessary to set it to {@link FlushModeType#COMMIT}.
111 * Warning : this sets the default flush mode of the session (either
112 * hibernate {@link Session}, JPA {@link EntityManager}, or similar) that
113 * is currently bound to the current thread. This means that it has
114 * absolutely no effect if no transaction is currently opened.
115 *
116 * @param flushMode
117 * The flush mode To set for this dao
118 * @throws HibernateException
119 * if thrown by the Hibernate API
120 * @throws SQLException
121 * if thrown by JDBC API
122 */
123 public void setFlushMode(FlushMode flushMode);
124
125 /**
126 * remove the object from the datastore
127 *
128 * @param o
129 * The object to remove
130 * @throws DataAccessException
131 * In case of errors
132 */
133 public void remove(T o);
134
135 /**
136 * Checks for existence of an object of type T using the id arg.
137 *
138 * @param id
139 * the id of the entity
140 * @return - true if it exists, false if it doesn't
141 * @throws DataAccessException
142 * in case of errors
143 */
144 boolean exists(PK id);
145
146 /**
147 * Retrieve the Object whith the specified primary key
148 *
149 * @param id
150 * the primarey key
151 * @return The object
152 * @throws DataAccessException
153 * in case of errors
154 */
155 public T get(PK id);
156
157 /**
158 * @return the number of element in the Datastore
159 * @throws HibernateException
160 * if thrown by the Hibernate API
161 * @throws SQLException
162 * if thrown by JDBC API
163 */
164 public long count();
165
166 /**
167 * Delete all the specified object from the datastore
168 *
169 * @param list
170 * the list of element to delete
171 * @throws DataAccessException
172 * in case of errors
173 */
174 public void deleteAll(List<T> list);
175
176 /**
177 * Delete all the object from the datastore
178 *
179 * @return the number of deleted objects
180 *
181 * @throws DataAccessException
182 * in case of errors
183 */
184 public int deleteAll();
185
186 }