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