View Javadoc
1   /*
2    * AutoDAO - Generic DAO on steroids implementation for Java.
3    *
4    * Copyright 2008-2012  Marat Radchenko <slonopotamus@users.sourceforge.net>
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package net.sf.autodao;
19  
20  import java.io.Serializable;
21  
22  import org.jetbrains.annotations.NotNull;
23  import org.jetbrains.annotations.Nullable;
24  import org.springframework.dao.DataAccessException;
25  
26  /**
27   * Typesafe CRUD interface.
28   *
29   * <p>In order to use AutoDAO CRUD operations just inherit your DAO interface from this one.
30   *
31   * <p>See <a href="http://autodao.sourceforge.net/guide.html#CRUD_operations">CRUD operations</a> for usage.
32   *
33   * @param <E> entity type.
34   * @param <K> primary key.
35   * @since AutoDAO 0.1
36   */
37  @SuppressWarnings({ "UnusedDeclaration" })
38  public interface Dao<E extends PersistentEntity<K>, K extends Serializable> {
39  
40    /**
41     * Persist the new instance object into database. [create]
42     *
43     * @param o transient object.
44     * @see org.hibernate.Session#persist(Object)
45     */
46    void create(@NotNull E o) throws DataAccessException;
47  
48    /**
49     * Retrieve an object that was previously persisted to the database using the indicated id as primary key. [read]
50     *
51     * @param id primary key
52     * @return instance of object or <code>null</code> if object is not found.
53     * @see org.hibernate.Session#get(Class, java.io.Serializable)
54     */
55    @Nullable
56    E get(@NotNull K id) throws DataAccessException;
57  
58    /**
59     * Update the given object. [update]
60     *
61     * @param o instance of object to update.
62     * @see org.hibernate.Session#update(Object)
63     */
64    void update(@NotNull E o) throws DataAccessException;
65  
66    /**
67     * Remove an object from persistent storage in the database. [delete]
68     *
69     * @param o object.
70     * @see org.hibernate.Session#delete(Object)
71     */
72    void delete(@NotNull E o) throws DataAccessException;
73  
74    /**
75     * Save or update object.
76     *
77     * @param o object.
78     * @see org.hibernate.Session#saveOrUpdate(Object)
79     */
80    void saveOrUpdate(@NotNull E o) throws DataAccessException;
81  
82    /**
83     * Merge object with persistent object.
84     *
85     * @param o object to merge.
86     * @return merged object
87     * @see org.hibernate.Session#merge(Object)
88     */
89    @NotNull
90    E merge(@NotNull E o) throws DataAccessException;
91  
92    /**
93     * Refresh the state of the instance from the database, overwriting changes made to the entity, if any.
94     *
95     * @param o object.
96     * @see org.hibernate.Session#refresh(Object)
97     */
98    void refresh(@NotNull final E o) throws DataAccessException;
99  }