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