KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > jpa > support > SharedEntityManagerBean


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.support;
18
19 import javax.persistence.EntityManager;
20 import javax.persistence.EntityManagerFactory;
21
22 import org.springframework.beans.factory.FactoryBean;
23 import org.springframework.beans.factory.InitializingBean;
24 import org.springframework.orm.jpa.EntityManagerFactoryAccessor;
25 import org.springframework.orm.jpa.EntityManagerFactoryInfo;
26 import org.springframework.orm.jpa.EntityManagerPlus;
27 import org.springframework.orm.jpa.JpaDialect;
28 import org.springframework.orm.jpa.SharedEntityManagerCreator;
29 import org.springframework.util.Assert;
30
31 /**
32  * FactoryBeans that exposes a shared JPA EntityManager reference for a
33  * given EntityManagerFactory. Typically used for an EntityManagerFactory
34  * created by LocalEntityManagerFactoryBean, as direct alternative to a
35  * JndiObjectFactoryBean definition for a Java EE 5 server's EntityManager.
36  *
37  * <p>The shared EntityManager will behave just like an EntityManager fetched
38  * from an application server's JNDI environment, as defined by the JPA
39  * specification. It will delegate all calls to the current transactional
40  * EntityManager, if any; else, it will fall back to a newly created
41  * EntityManager per operation.
42  *
43  * <p>Can be passed to DAOs that expect a shared EntityManager reference
44  * rather than an EntityManagerFactory reference. Note that Spring's
45  * JpaTransactionManager always needs an EntityManagerFactory reference,
46  * to be able to create new transactional EntityManager instances.
47  *
48  * @author Juergen Hoeller
49  * @since 2.0
50  * @see #setEntityManagerFactory
51  * @see #setEntityManagerInterface
52  * @see org.springframework.orm.jpa.LocalEntityManagerFactoryBean
53  * @see org.springframework.orm.jpa.JpaTransactionManager
54  */

55 public class SharedEntityManagerBean extends EntityManagerFactoryAccessor implements FactoryBean, InitializingBean {
56
57     private Class JavaDoc entityManagerInterface;
58
59     private EntityManager shared;
60
61
62     /**
63      * Specify the EntityManager interface to expose.
64      * <p>Default is the the EntityManager interface as defined by the
65      * EntityManagerFactoryInfo, if available. Else, the standard
66      * <code>javax.persistence.EntityManager</code> interface will be used.
67      * @see org.springframework.orm.jpa.EntityManagerFactoryInfo#getEntityManagerInterface()
68      * @see javax.persistence.EntityManager
69      */

70     public void setEntityManagerInterface(Class JavaDoc entityManagerInterface) {
71         Assert.notNull(entityManagerInterface, "entityManagerInterface must not be null");
72         Assert.isAssignable(EntityManager.class, entityManagerInterface);
73         this.entityManagerInterface = entityManagerInterface;
74     }
75
76
77     public final void afterPropertiesSet() {
78         EntityManagerFactory emf = getEntityManagerFactory();
79         if (emf == null) {
80             throw new IllegalArgumentException JavaDoc("entityManagerFactory is required");
81         }
82         Class JavaDoc[] ifcs = null;
83         if (emf instanceof EntityManagerFactoryInfo) {
84             EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) emf;
85             if (this.entityManagerInterface == null) {
86                 this.entityManagerInterface = emfInfo.getEntityManagerInterface();
87             }
88             JpaDialect jpaDialect = emfInfo.getJpaDialect();
89             if (jpaDialect != null && jpaDialect.supportsEntityManagerPlusOperations()) {
90                 ifcs = new Class JavaDoc[] {this.entityManagerInterface, EntityManagerPlus.class};
91             }
92             else {
93                 ifcs = new Class JavaDoc[] {this.entityManagerInterface};
94             }
95         }
96         else {
97             if (this.entityManagerInterface == null) {
98                 this.entityManagerInterface = EntityManager.class;
99             }
100             ifcs = new Class JavaDoc[] {this.entityManagerInterface};
101         }
102         this.shared = SharedEntityManagerCreator.createSharedEntityManager(emf, getJpaPropertyMap(), ifcs);
103     }
104
105
106     public EntityManager getObject() {
107         return this.shared;
108     }
109
110     public Class JavaDoc getObjectType() {
111         return this.entityManagerInterface;
112     }
113
114     public boolean isSingleton() {
115         return true;
116     }
117
118 }
119
Popular Tags