001package io.ebean.bean;
002
003import java.util.List;
004
005/**
006 * Holds entity beans by there type and id.
007 * <p>
008 * This is used to ensure only one instance for a given entity type and id is
009 * used to build object graphs from queries and lazy loading.
010 * </p>
011 */
012public interface PersistenceContext {
013
014  /**
015   * Put the entity bean into the PersistenceContext.
016   */
017  void put(Class<?> rootType, Object id, Object bean);
018
019  /**
020   * Put the entity bean into the PersistenceContext if one is not already
021   * present (for this id).
022   * <p>
023   * Returns an existing entity bean (if one is already there) and otherwise
024   * returns null.
025   * </p>
026   */
027  Object putIfAbsent(Class<?> rootType, Object id, Object bean);
028
029  /**
030   * Return an object given its type and unique id.
031   */
032  Object get(Class<?> rootType, Object uid);
033
034  /**
035   * Get the bean from the persistence context also checked to see if it had
036   * been previously deleted (if so then you also can't hit the L2 cache to
037   * fetch the bean for this particular persistence context).
038   */
039  WithOption getWithOption(Class<?> rootType, Object uid);
040
041  /**
042   * Clear all the references.
043   */
044  void clear();
045
046  /**
047   * Clear all the references for a given type of entity bean.
048   */
049  void clear(Class<?> rootType);
050
051  /**
052   * Clear the reference to a specific entity bean.
053   */
054  void clear(Class<?> rootType, Object uid);
055
056  /**
057   * Clear the reference as a result of an entity being deleted.
058   */
059  void deleted(Class<?> rootType, Object id);
060
061  /**
062   * Return the number of beans of the given type in the persistence context.
063   */
064  int size(Class<?> rootType);
065
066  /**
067   * Return a copy of the Persistence context to use for large query iteration.
068   */
069  PersistenceContext forIterate();
070
071  /**
072   * Return a new Persistence context during iteration of large query result.
073   */
074  PersistenceContext forIterateReset();
075
076  /**
077   * Return true if the persistence context has grown and hit the 'reset limit'
078   * during large query iteration.
079   */
080  boolean resetLimit();
081
082  /**
083   * Return the list of dirty beans held by this persistence context.
084   */
085  List<Object> dirtyBeans();
086
087  /**
088   * Wrapper on a bean to also indicate if a bean has been deleted.
089   * <p>
090   * If a bean has been deleted then for the same persistence context is should
091   * not be able to be fetched from persistence context or L2 cache.
092   * </p>
093   */
094  class WithOption {
095
096    /**
097     * The bean was previously deleted from this persistence context (can't hit
098     * L2 cache).
099     */
100    public static final WithOption DELETED = new WithOption();
101
102    private final boolean deleted;
103    private final Object bean;
104
105    private WithOption() {
106      this.deleted = true;
107      this.bean = null;
108    }
109
110    /**
111     * The bean exists in the persistence context (and not been previously deleted).
112     */
113    public WithOption(Object bean) {
114      this.deleted = false;
115      this.bean = bean;
116    }
117
118    /**
119     * Return true if the bean was deleted. This means you can't hit the L2
120     * cache.
121     */
122    public boolean isDeleted() {
123      return deleted;
124    }
125
126    /**
127     * Return the bean (from the persistence context).
128     */
129    public Object getBean() {
130      return bean;
131    }
132  }
133}