001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.orm.jpa.support;
018:
019: import javax.persistence.EntityManager;
020: import javax.persistence.EntityManagerFactory;
021:
022: import org.springframework.dao.support.DaoSupport;
023: import org.springframework.orm.jpa.JpaTemplate;
024:
025: /**
026: * Convenient super class for JPA data access objects. Intended for
027: * JpaTemplate usage. Alternatively, JPA-based DAOs can be coded
028: * against the plain JPA EntityManagerFactory/EntityManager API.
029: *
030: * <p>Requires an EntityManagerFactory or EntityManager to be set,
031: * providing a JpaTemplate based on it to subclasses. Can alternatively
032: * be initialized directly via a JpaTemplate, to reuse the latter's
033: * settings such as the EntityManagerFactory, JpaDialect, flush mode, etc.
034: *
035: * <p>This class will create its own JpaTemplate if an EntityManagerFactory
036: * or EntityManager reference is passed in. A custom JpaTemplate instance
037: * can be used through overriding <code>createJpaTemplate</code>.
038: *
039: * @author Juergen Hoeller
040: * @since 2.0
041: * @see #setEntityManagerFactory
042: * @see #setEntityManager
043: * @see #createJpaTemplate
044: * @see #setJpaTemplate
045: * @see org.springframework.orm.jpa.JpaTemplate
046: */
047: public abstract class JpaDaoSupport extends DaoSupport {
048:
049: private JpaTemplate jpaTemplate;
050:
051: /**
052: * Set the JPA EntityManagerFactory to be used by this DAO.
053: * Will automatically create a JpaTemplate for the given EntityManagerFactory.
054: * @see #createJpaTemplate
055: * @see #setJpaTemplate
056: */
057: public final void setEntityManagerFactory(
058: EntityManagerFactory entityManagerFactory) {
059: this .jpaTemplate = createJpaTemplate(entityManagerFactory);
060: }
061:
062: /**
063: * Create a JpaTemplate for the given EntityManagerFactory.
064: * Only invoked if populating the DAO with a EntityManagerFactory reference!
065: * <p>Can be overridden in subclasses to provide a JpaTemplate instance
066: * with different configuration, or a custom JpaTemplate subclass.
067: * @param entityManagerFactory the JPA EntityManagerFactory to create a JpaTemplate for
068: * @return the new JpaTemplate instance
069: * @see #setEntityManagerFactory
070: */
071: protected JpaTemplate createJpaTemplate(
072: EntityManagerFactory entityManagerFactory) {
073: return new JpaTemplate(entityManagerFactory);
074: }
075:
076: /**
077: * Set the JPA EntityManager to be used by this DAO.
078: * Will automatically create a JpaTemplate for the given EntityManager.
079: * @see #createJpaTemplate
080: * @see #setJpaTemplate
081: */
082: public final void setEntityManager(EntityManager entityManager) {
083: this .jpaTemplate = createJpaTemplate(entityManager);
084: }
085:
086: /**
087: * Create a JpaTemplate for the given EntityManager.
088: * Only invoked if populating the DAO with a EntityManager reference!
089: * <p>Can be overridden in subclasses to provide a JpaTemplate instance
090: * with different configuration, or a custom JpaTemplate subclass.
091: * @param entityManager the JPA EntityManager to create a JpaTemplate for
092: * @return the new JpaTemplate instance
093: * @see #setEntityManagerFactory
094: */
095: protected JpaTemplate createJpaTemplate(EntityManager entityManager) {
096: return new JpaTemplate(entityManager);
097: }
098:
099: /**
100: * Set the JpaTemplate for this DAO explicitly,
101: * as an alternative to specifying a EntityManagerFactory.
102: * @see #setEntityManagerFactory
103: */
104: public final void setJpaTemplate(JpaTemplate jpaTemplate) {
105: this .jpaTemplate = jpaTemplate;
106: }
107:
108: /**
109: * Return the JpaTemplate for this DAO, pre-initialized
110: * with the EntityManagerFactory or set explicitly.
111: */
112: public final JpaTemplate getJpaTemplate() {
113: return jpaTemplate;
114: }
115:
116: protected final void checkDaoConfig() {
117: if (this .jpaTemplate == null) {
118: throw new IllegalArgumentException(
119: "entityManagerFactory or jpaTemplate is required");
120: }
121: }
122:
123: }
|