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   * Wrapper on a bean to also indicate if a bean has been deleted.
066   * <p>
067   * If a bean has been deleted then for the same persistence context is should
068   * not be able to be fetched from persistence context or L2 cache.
069   * </p>
070   */
071  class WithOption {
072
073    /**
074     * The bean was previously deleted from this persistence context (can't hit
075     * L2 cache).
076     */
077    public static final WithOption DELETED = new WithOption();
078
079    private final boolean deleted;
080    private final Object bean;
081
082    private WithOption() {
083      this.deleted = true;
084      this.bean = null;
085    }
086
087    /**
088     * The bean exists in the persistence context (and not been previously deleted).
089     */
090    public WithOption(Object bean) {
091      this.deleted = false;
092      this.bean = bean;
093    }
094
095    /**
096     * Return true if the bean was deleted. This means you can't hit the L2
097     * cache.
098     */
099    public boolean isDeleted() {
100      return deleted;
101    }
102
103    /**
104     * Return the bean (from the persistence context).
105     */
106    public Object getBean() {
107      return bean;
108    }
109  }
110}