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

org.springframework.orm.jpa.EntityManagerFactoryAccessor Maven / Gradle / Ivy

There is a newer version: 5.3.34
Show newest version
/*
 * Copyright 2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.orm.jpa;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

/**
 * Base class for any class that needs to access an EntityManagerFactory,
 * usually in order to obtain an EntityManager. Defines common properties.
 *
 * 

Not intended to be used directly. See JpaAccessor. * * @author Juergen Hoeller * @since 2.0 * @see JpaAccessor * @see EntityManagerFactoryUtils */ public abstract class EntityManagerFactoryAccessor { /** Logger available to subclasses */ protected final Log logger = LogFactory.getLog(getClass()); private EntityManagerFactory entityManagerFactory; private final Map jpaPropertyMap = new HashMap(); /** * Set the JPA EntityManagerFactory that should be used to create * EntityManagers. * @see javax.persistence.EntityManagerFactory#createEntityManager() * @see javax.persistence.EntityManagerFactory#createEntityManager(java.util.Map) */ public void setEntityManagerFactory(EntityManagerFactory emf) { this.entityManagerFactory = emf; } /** * Return the JPA EntityManagerFactory that should be used to create * EntityManagers. */ public EntityManagerFactory getEntityManagerFactory() { return entityManagerFactory; } /** * Specify JPA properties, to be passed into * EntityManagerFactory.createEntityManager(Map) (if any). *

Can be populated with a String "value" (parsed via PropertiesEditor) * or a "props" element in XML bean definitions. * @see javax.persistence.EntityManagerFactory#createEntityManager(java.util.Map) */ public void setJpaProperties(Properties jpaProperties) { CollectionUtils.mergePropertiesIntoMap(jpaProperties, this.jpaPropertyMap); } /** * Specify JPA properties as a Map, to be passed into * EntityManagerFactory.createEntityManager(Map) (if any). *

Can be populated with a "map" or "props" element in XML bean definitions. * @see javax.persistence.EntityManagerFactory#createEntityManager(java.util.Map) */ public void setJpaPropertyMap(Map jpaProperties) { if (jpaProperties != null) { this.jpaPropertyMap.putAll(jpaProperties); } } /** * Allow Map access to the JPA properties to be passed to the persistence * provider, with the option to add or override specific entries. *

Useful for specifying entries directly, for example via "jpaPropertyMap[myKey]". */ public Map getJpaPropertyMap() { return jpaPropertyMap; } /** * Obtain a new EntityManager from this accessor's EntityManagerFactory. *

Can be overridden in subclasses to create specific EntityManager variants. * @return a new EntityManager * @throws IllegalStateException if this accessor is not configured with an EntityManagerFactory * @see javax.persistence.EntityManagerFactory#createEntityManager() * @see javax.persistence.EntityManagerFactory#createEntityManager(java.util.Map) */ protected EntityManager createEntityManager() throws IllegalStateException { EntityManagerFactory emf = getEntityManagerFactory(); Assert.state(emf != null, "No EntityManagerFactory specified"); Map properties = getJpaPropertyMap(); return (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager()); } /** * Obtain the transactional EntityManager for this accessor's EntityManagerFactory, if any. * @return the transactional EntityManager, or null if none * @throws IllegalStateException if this accessor is not configured with an EntityManagerFactory * @see EntityManagerFactoryUtils#getTransactionalEntityManager(javax.persistence.EntityManagerFactory) * @see EntityManagerFactoryUtils#getTransactionalEntityManager(javax.persistence.EntityManagerFactory, java.util.Map) */ protected EntityManager getTransactionalEntityManager() throws IllegalStateException{ EntityManagerFactory emf = getEntityManagerFactory(); Assert.state(emf != null, "No EntityManagerFactory specified"); return EntityManagerFactoryUtils.getTransactionalEntityManager(emf, getJpaPropertyMap()); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy