All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javax.persistence.EntityManager Maven / Gradle / Ivy

The newest version!
/*
 * The contents of this file are subject to the terms 
 * of the Common Development and Distribution License 
 * (the License).  You may not use this file except in
 * compliance with the License.
 * 
 * You can obtain a copy of the license at 
 * https://glassfish.dev.java.net/public/CDDLv1.0.html or
 * glassfish/bootstrap/legal/CDDLv1.0.txt.
 * See the License for the specific language governing 
 * permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL 
 * Header Notice in each file and include the License file 
 * at glassfish/bootstrap/legal/CDDLv1.0.txt.  
 * If applicable, add the following below the CDDL Header, 
 * with the fields enclosed by brackets [] replaced by
 * you own identifying information: 
 * "Portions Copyrighted [year] [name of copyright owner]"
 * 
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 */
package javax.persistence;

/**
 * Interface used to interact with the persistence context.
 */
public interface EntityManager {

    /**
     * Make an instance managed and persistent.
     * @param entity
     * @throws IllegalArgumentException if not an entity
     *    or entity is detached
     * @throws TransactionRequiredException if there is
     *    no transaction
     */
    public void persist(Object entity);
    
    /**
     * Merge the state of the given entity into the
     * current persistence context.
     * @param entity
     * @return the instance that the state was merged to
     * @throws IllegalArgumentException if instance is not an
     *    entity or is a removed entity
     * @throws TransactionRequiredException if there is
     *    no transaction
     */
    public  T merge(T entity);
    
    /**
     * Remove the instance.
     * @param entity
     * @throws IllegalArgumentException if not an entity
     *    or if a detached entity
     * @throws TransactionRequiredException if there is
     *    no transaction
     */
    public void remove(Object entity);
    
    /**
     * Find by primary key.
     * @param entityClass
     * @param primaryKey
     * @return the found entity instance or null
     *    if the entity does not exist
     * @throws IllegalArgumentException if the first argument does
     *    not denote an entity type or the second
     *    argument is not a valid type for that
     *    entity's primary key
     */
    public  T find(Class entityClass, Object primaryKey);

    /**
     * Get an instance, whose state may be lazily fetched.
     * If the requested instance does not exist in the database,
     * throws EntityNotFoundException when the instance state is
     * first accessed. (The container is permitted to throw
     * EntityNotFoundException when get is called.)
     * The application should not expect that the instance state will
     * be available upon detachment, unless it was accessed by the
     * application while the entity manager was open.
     * @param entityClass
     * @param primaryKey
     * @return the found entity instance
     * @throws IllegalArgumentException if the first argument does
     *    not denote an entity type or the second
     *    argument is not a valid type for that
     *    entity's primary key
     * @throws EntityNotFoundException if the entity state
     *    cannot be accessed
     */
    public  T getReference(Class entityClass, Object primaryKey);
    
    /**
     * Synchronize the persistence context to the
     * underlying database.
     * @throws TransactionRequiredException if there is
     *    no transaction
     * @throws PersistenceException if the flush fails
     */
    public void flush();
  
    /** 
     * Refresh the state of the instance from the database,
     * overwriting changes made to the entity, if any.
     * @param entity
     * @throws IllegalArgumentException if not an entity
     *    or entity is not managed
     * @throws TransactionRequiredException if there is
     *    no transaction
     * @throws EntityNotFoundException if the entity no longer
     *    exists in the database
     */
    public void refresh(Object entity);
    
    /**
     * Check if the instance belongs to the current persistence
     * context.
     * @param entity
     * @return
     * @throws IllegalArgumentException if not an entity
     */
    public boolean contains(Object entity);
    
    /**
     * Create an instance of Query for executing an
     * EJB QL statement.
     * @param ejbqlString an EJB QL query string
     * @return the new query instance
     * @throws IllegalArgumentException if query string is not valid
     */
    public Query createQuery(String ejbqlString);
    
    /**
     * Create an instance of Query for executing a
     * named query (in EJB QL or native SQL).
     * @param name the name of a query defined in metadata
     * @return the new query instance
     * @throws IllegalArgumentException if query string is not valid
     */
    public Query createNamedQuery(String name);
    
    /**
     * Create an instance of Query for executing
     * a native SQL statement.
     * @param sqlString a native SQL query string
     * @return the new query instance
     * @throws IllegalArgumentException if query string is not valid
     */
    public Query createNativeQuery(String sqlString);
    
    /**
     * Create an instance of Query for executing
     * a native SQL query.
     * @param sqlString a native SQL query string
     * @param resultClass the class of the resulting instances
     * @return the new query instance
     * @throws IllegalArgumentException if query string is not valid
     */
    public Query createNativeQuery(String sqlString, Class resultClass);
    
    /**
     * Create an instance of Query for executing
     * a native SQL query.
     * @param sqlString a native SQL query string
     * @param resultSetMapping the name of the result set mapping
     * @return the new query instance
     * @throws IllegalArgumentException if query string is not valid
     */
    public Query createNativeQuery(String sqlString, String resultSetMapping);
    
    /**
     * Closes an application-managed EntityManager.
     * This method can only be called when the EntityManager
     * is not associated with an active transaction.
     * After an EntityManager has been closed, all methods on the
     * EntityManager instance will throw the IllegalStateException
     * except for isOpen, which will return false.
     * @throws IllegalStateException if the EntityManager is
     *    associated with an active transaction or if the
     *    EntityManager is container-managed.
     */
    public void close();
    
    /**
     * Indicates whether the EntityManager is open.
     * @return true until the EntityManager has been closed.
     */
    public boolean isOpen();
    
    /**
    * Set the lock mode for an entity object contained
    * in the persistence context.
    * @param entity
    * @param lockMode
    * @throws PersistenceException if an unsupported lock call
    * is made
    * @throws IllegalArgumentException if the instance is not
    * an entity or is a detached entity
    * @throws TransactionRequiredException if there is no
    * transaction
    */
    public void lock(Object entity, LockModeType lockMode);

    /**
    * Clear the persistence context, causing all managed
    * entities to become detached. Changes made to entities that
    * have not been flushed to the database will not be
    * persisted.
    */
    public void clear();

    /**
    * Return the underlying provider object for the EntityManager,
    * if available. The result of this method is implementation
    * specific.
    */
    public Object getDelegate();

    /**
     * Returns the resource-level transaction object.
     * The EntityTransaction instance may be used serially to
     * begin and commit multiple transactions.
     * @return EntityTransaction instance
     * @throws IllegalStateException if invoked on a JTA
     *    EntityManager or an EntityManager that has been closed.
     */
    public EntityTransaction getTransaction();
    
    /**
    * Get the flush mode that applies to all objects contained
    * in the persistence context.
    * @return flushMode
    */
    public FlushModeType getFlushMode();

    /**
    * Set the flush mode that applies to all objects contained
    * in the persistence context.
    * @param flushMode
    */
    public void setFlushMode(FlushModeType flushMode);    

    /**
     * Indicate to the EntityManager that a JTA transaction is
     * active. This method should be called on a JTA application
     * managed EntityManager that was created outside the scope
     * of the active transaction to associate it with the current
     * JTA transaction.
     * @throws TransactionRequiredException if there is
     * no transaction.
     */
    public void joinTransaction();

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy