001
002 /*
003 * Copyright (C) 2011 Archie L. Cobbs. All rights reserved.
004 *
005 * $Id: DAO.java 275 2012-02-13 21:56:57Z archie.cobbs $
006 */
007
008 package org.dellroad.stuff.dao;
009
010 import java.util.List;
011
012 import javax.persistence.FlushModeType;
013
014 /**
015 * Data Access Object (DAO) generic interface.
016 */
017 public interface DAO<T> {
018
019 // Access methods
020
021 /**
022 * Get an instance by ID. This assumes object IDs are long values.
023 */
024 T getById(long id);
025
026 /**
027 * Get all instances.
028 */
029 List<T> getAll();
030
031 /**
032 * Get a reference to an instance by ID. This assumes object IDs are long values.
033 *
034 * <p>
035 * Note if the instance does not exist, then an exception may be thrown either here or later upon first access.
036 */
037 T getReference(long id);
038
039 // Lifecycle methods
040
041 /**
042 * Save a newly created instance.
043 */
044 void save(T obj);
045
046 /**
047 * Delete the given instance from the persistent store.
048 */
049 void delete(T obj);
050
051 /**
052 * Merge the given object into the current session.
053 */
054 T merge(T obj);
055
056 /**
057 * Refresh the given object from the database.
058 */
059 void refresh(T obj);
060
061 /**
062 * Evict an object from the session cache.
063 */
064 void detach(T obj);
065
066 // Session methods
067
068 /**
069 * Flush outstanding changes to the persistent store.
070 */
071 void flush();
072
073 /**
074 * Set flush mode.
075 */
076 void setFlushMode(FlushModeType flushMode);
077
078 /**
079 * Clear the session cache.
080 */
081 void clear();
082
083 /**
084 * Determine if the current transaction is read-only.
085 *
086 * @return true if the current transaction is read-only
087 * @throws IllegalStateException if no transaction is associated with the current thread
088 */
089 boolean isReadOnly();
090 }
091