KickJava   Java API By Example, From Geeks To Geeks.

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


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.dao.support.DaoSupport;
23 import org.springframework.orm.jpa.JpaTemplate;
24
25 /**
26  * Convenient super class for JPA data access objects. Intended for
27  * JpaTemplate usage. Alternatively, JPA-based DAOs can be coded
28  * against the plain JPA EntityManagerFactory/EntityManager API.
29  *
30  * <p>Requires an EntityManagerFactory or EntityManager to be set,
31  * providing a JpaTemplate based on it to subclasses. Can alternatively
32  * be initialized directly via a JpaTemplate, to reuse the latter's
33  * settings such as the EntityManagerFactory, JpaDialect, flush mode, etc.
34  *
35  * <p>This class will create its own JpaTemplate if an EntityManagerFactory
36  * or EntityManager reference is passed in. A custom JpaTemplate instance
37  * can be used through overriding <code>createJpaTemplate</code>.
38  *
39  * @author Juergen Hoeller
40  * @since 2.0
41  * @see #setEntityManagerFactory
42  * @see #setEntityManager
43  * @see #createJpaTemplate
44  * @see #setJpaTemplate
45  * @see org.springframework.orm.jpa.JpaTemplate
46  */

47 public abstract class JpaDaoSupport extends DaoSupport {
48
49     private JpaTemplate jpaTemplate;
50
51
52     /**
53      * Set the JPA EntityManagerFactory to be used by this DAO.
54      * Will automatically create a JpaTemplate for the given EntityManagerFactory.
55      * @see #createJpaTemplate
56      * @see #setJpaTemplate
57      */

58     public final void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
59       this.jpaTemplate = createJpaTemplate(entityManagerFactory);
60     }
61
62     /**
63      * Create a JpaTemplate for the given EntityManagerFactory.
64      * Only invoked if populating the DAO with a EntityManagerFactory reference!
65      * <p>Can be overridden in subclasses to provide a JpaTemplate instance
66      * with different configuration, or a custom JpaTemplate subclass.
67      * @param entityManagerFactory the JPA EntityManagerFactory to create a JpaTemplate for
68      * @return the new JpaTemplate instance
69      * @see #setEntityManagerFactory
70      */

71     protected JpaTemplate createJpaTemplate(EntityManagerFactory entityManagerFactory) {
72         return new JpaTemplate(entityManagerFactory);
73     }
74
75     /**
76      * Set the JPA EntityManager to be used by this DAO.
77      * Will automatically create a JpaTemplate for the given EntityManager.
78      * @see #createJpaTemplate
79      * @see #setJpaTemplate
80      */

81     public final void setEntityManager(EntityManager entityManager) {
82       this.jpaTemplate = createJpaTemplate(entityManager);
83     }
84
85     /**
86      * Create a JpaTemplate for the given EntityManager.
87      * Only invoked if populating the DAO with a EntityManager reference!
88      * <p>Can be overridden in subclasses to provide a JpaTemplate instance
89      * with different configuration, or a custom JpaTemplate subclass.
90      * @param entityManager the JPA EntityManager to create a JpaTemplate for
91      * @return the new JpaTemplate instance
92      * @see #setEntityManagerFactory
93      */

94     protected JpaTemplate createJpaTemplate(EntityManager entityManager) {
95         return new JpaTemplate(entityManager);
96     }
97
98     /**
99      * Set the JpaTemplate for this DAO explicitly,
100      * as an alternative to specifying a EntityManagerFactory.
101      * @see #setEntityManagerFactory
102      */

103     public final void setJpaTemplate(JpaTemplate jpaTemplate) {
104         this.jpaTemplate = jpaTemplate;
105     }
106
107     /**
108      * Return the JpaTemplate for this DAO, pre-initialized
109      * with the EntityManagerFactory or set explicitly.
110      */

111     public final JpaTemplate getJpaTemplate() {
112       return jpaTemplate;
113     }
114
115     protected final void checkDaoConfig() {
116         if (this.jpaTemplate == null) {
117             throw new IllegalArgumentException JavaDoc("entityManagerFactory or jpaTemplate is required");
118         }
119     }
120
121 }
122
Popular Tags