KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > jpa > EntityManagerFactoryAccessor


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.orm.jpa;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import javax.persistence.EntityManager;
24 import javax.persistence.EntityManagerFactory;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.springframework.util.Assert;
30 import org.springframework.util.CollectionUtils;
31
32 /**
33  * Base class for any class that needs to access an EntityManagerFactory,
34  * usually in order to obtain an EntityManager. Defines common properties.
35  *
36  * <p>Not intended to be used directly. See JpaAccessor.
37  *
38  * @author Juergen Hoeller
39  * @since 2.0
40  * @see JpaAccessor
41  * @see EntityManagerFactoryUtils
42  */

43 public abstract class EntityManagerFactoryAccessor {
44
45     /** Logger available to subclasses */
46     protected final Log logger = LogFactory.getLog(getClass());
47
48     private EntityManagerFactory entityManagerFactory;
49
50     private final Map JavaDoc jpaPropertyMap = new HashMap JavaDoc();
51
52
53     /**
54      * Set the JPA EntityManagerFactory that should be used to create
55      * EntityManagers.
56      * @see javax.persistence.EntityManagerFactory#createEntityManager()
57      * @see javax.persistence.EntityManagerFactory#createEntityManager(java.util.Map)
58      */

59     public void setEntityManagerFactory(EntityManagerFactory emf) {
60         this.entityManagerFactory = emf;
61     }
62
63     /**
64      * Return the JPA EntityManagerFactory that should be used to create
65      * EntityManagers.
66      */

67     public EntityManagerFactory getEntityManagerFactory() {
68         return entityManagerFactory;
69     }
70
71     /**
72      * Specify JPA properties, to be passed into
73      * <code>EntityManagerFactory.createEntityManager(Map)</code> (if any).
74      * <p>Can be populated with a String "value" (parsed via PropertiesEditor)
75      * or a "props" element in XML bean definitions.
76      * @see javax.persistence.EntityManagerFactory#createEntityManager(java.util.Map)
77      */

78     public void setJpaProperties(Properties JavaDoc jpaProperties) {
79         CollectionUtils.mergePropertiesIntoMap(jpaProperties, this.jpaPropertyMap);
80     }
81
82     /**
83      * Specify JPA properties as a Map, to be passed into
84      * <code>EntityManagerFactory.createEntityManager(Map)</code> (if any).
85      * <p>Can be populated with a "map" or "props" element in XML bean definitions.
86      * @see javax.persistence.EntityManagerFactory#createEntityManager(java.util.Map)
87      */

88     public void setJpaPropertyMap(Map JavaDoc jpaProperties) {
89         if (jpaProperties != null) {
90             this.jpaPropertyMap.putAll(jpaProperties);
91         }
92     }
93
94     /**
95      * Allow Map access to the JPA properties to be passed to the persistence
96      * provider, with the option to add or override specific entries.
97      * <p>Useful for specifying entries directly, for example via "jpaPropertyMap[myKey]".
98      */

99     public Map JavaDoc getJpaPropertyMap() {
100         return jpaPropertyMap;
101     }
102
103
104     /**
105      * Obtain a new EntityManager from this accessor's EntityManagerFactory.
106      * <p>Can be overridden in subclasses to create specific EntityManager variants.
107      * @return a new EntityManager
108      * @throws IllegalStateException if this accessor is not configured with an EntityManagerFactory
109      * @see javax.persistence.EntityManagerFactory#createEntityManager()
110      * @see javax.persistence.EntityManagerFactory#createEntityManager(java.util.Map)
111      */

112     protected EntityManager createEntityManager() throws IllegalStateException JavaDoc {
113         EntityManagerFactory emf = getEntityManagerFactory();
114         Assert.state(emf != null, "No EntityManagerFactory specified");
115         Map JavaDoc properties = getJpaPropertyMap();
116         return (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager());
117     }
118
119     /**
120      * Obtain the transactional EntityManager for this accessor's EntityManagerFactory, if any.
121      * @return the transactional EntityManager, or <code>null</code> if none
122      * @throws IllegalStateException if this accessor is not configured with an EntityManagerFactory
123      * @see EntityManagerFactoryUtils#getTransactionalEntityManager(javax.persistence.EntityManagerFactory)
124      * @see EntityManagerFactoryUtils#getTransactionalEntityManager(javax.persistence.EntityManagerFactory, java.util.Map)
125      */

126     protected EntityManager getTransactionalEntityManager() throws IllegalStateException JavaDoc{
127         EntityManagerFactory emf = getEntityManagerFactory();
128         Assert.state(emf != null, "No EntityManagerFactory specified");
129         return EntityManagerFactoryUtils.getTransactionalEntityManager(emf, getJpaPropertyMap());
130     }
131
132 }
133
Popular Tags