KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2007 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 javax.persistence.EntityManagerFactory;
20 import javax.persistence.Persistence;
21 import javax.persistence.PersistenceException;
22 import javax.persistence.spi.PersistenceProvider;
23
24 /**
25  * {@link org.springframework.beans.factory.FactoryBean} that creates a JPA
26  * {@link javax.persistence.EntityManagerFactory} according to JPA's standard
27  * <i>standalone</i> bootstrap contract. This is the simplest way to set up a
28  * shared JPA EntityManagerFactory in a Spring application context; the
29  * EntityManagerFactory can then be passed to JPA-based DAOs via
30  * dependency injection. Note that switching to a JNDI lookup or to a
31  * {@link org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean}
32  * definition is just a matter of configuration!
33  *
34  * <p>Configuration settings are usually read from a <code>META-INF/persistence.xml</code>
35  * config file, residing in the class path, according to the JPA standalone bootstrap
36  * contract. Additionally, most JPA providers will require a special VM agent
37  * (specified on JVM startup) that allows them to instrument application classes.
38  * See the Java Persistence API specification and your provider documentation
39  * for setup details.
40  *
41  * <p>This EntityManagerFactory bootstrap is appropriate for standalone applications
42  * which solely use JPA for data access. If you want to set up your persistence
43  * provider for an external DataSource and/or for global transactions which span
44  * multiple resources, you will need to either deploy it into a full Java EE 5
45  * application server and access the deployed EntityManagerFactory via JNDI,
46  * or use Spring's {@link LocalContainerEntityManagerFactoryBean} with appropriate
47  * configuration for local setup according to JPA's container contract.
48  *
49  * <p><b>Note:</b> This FactoryBean has limited configuration power in terms of
50  * what configuration it is able to pass to the JPA provider. If you need more
51  * flexible configuration, for example passing a Spring-managed JDBC DataSource
52  * to the JPA provider, consider using Spring's more powerful
53  * {@link LocalContainerEntityManagerFactoryBean} instead.
54  *
55  * @author Juergen Hoeller
56  * @author Rod Johnson
57  * @since 2.0
58  * @see #setJpaProperties
59  * @see #setJpaVendorAdapter
60  * @see JpaTemplate#setEntityManagerFactory
61  * @see JpaTransactionManager#setEntityManagerFactory
62  * @see LocalContainerEntityManagerFactoryBean
63  * @see org.springframework.jndi.JndiObjectFactoryBean
64  * @see org.springframework.orm.jpa.support.SharedEntityManagerBean
65  * @see javax.persistence.Persistence#createEntityManagerFactory
66  * @see javax.persistence.spi.PersistenceProvider#createEntityManagerFactory
67  */

68 public class LocalEntityManagerFactoryBean extends AbstractEntityManagerFactoryBean {
69
70     /**
71      * Initialize the EntityManagerFactory for the given configuration.
72      * @throws javax.persistence.PersistenceException in case of JPA initialization errors
73      */

74     protected EntityManagerFactory createNativeEntityManagerFactory() throws PersistenceException {
75         PersistenceProvider provider = getPersistenceProvider();
76         if (provider != null) {
77             // Create EntityManagerFactory directly through PersistenceProvider.
78
EntityManagerFactory emf = provider.createEntityManagerFactory(getPersistenceUnitName(), getJpaPropertyMap());
79             if (emf == null) {
80                 throw new IllegalStateException JavaDoc(
81                         "PersistenceProvider [" + provider + "] did not return an EntityManagerFactory for name '" +
82                         getPersistenceUnitName() + "'");
83             }
84             return emf;
85         }
86         else {
87             // Let JPA perform its standard PersistenceProvider autodetection.
88
return Persistence.createEntityManagerFactory(getPersistenceUnitName(), getJpaPropertyMap());
89         }
90     }
91
92 }
93
Popular Tags