KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > jpa > vendor > HibernateJpaVendorAdapter


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.vendor;
18
19 import java.util.Map JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import javax.persistence.EntityManager;
23 import javax.persistence.spi.PersistenceProvider;
24
25 import org.hibernate.cfg.Environment;
26 import org.hibernate.dialect.DB2Dialect;
27 import org.hibernate.dialect.HSQLDialect;
28 import org.hibernate.dialect.InformixDialect;
29 import org.hibernate.dialect.MySQLDialect;
30 import org.hibernate.dialect.Oracle9Dialect;
31 import org.hibernate.dialect.PostgreSQLDialect;
32 import org.hibernate.dialect.SQLServerDialect;
33 import org.hibernate.dialect.SybaseDialect;
34 import org.hibernate.ejb.HibernateEntityManager;
35 import org.hibernate.ejb.HibernatePersistence;
36
37 import org.springframework.orm.jpa.JpaDialect;
38
39 /**
40  * {@link org.springframework.orm.jpa.JpaVendorAdapter} implementation for
41  * Hibernate EntityManager. Developed and tested against Hibernate 3.2.
42  *
43  * <p>Exposes Hibernate's persistence provider and EntityManager extension interface,
44  * and supports AbstractJpaVendorAdapter's common configuration settings.
45  *
46  * @author Juergen Hoeller
47  * @author Rod Johnson
48  * @since 2.0
49  * @see org.hibernate.ejb.HibernatePersistence
50  * @see org.hibernate.ejb.HibernateEntityManager
51  */

52 public class HibernateJpaVendorAdapter extends AbstractJpaVendorAdapter {
53
54     private final PersistenceProvider persistenceProvider = new HibernatePersistence();
55
56     private final JpaDialect jpaDialect = new HibernateJpaDialect();
57
58
59     public PersistenceProvider getPersistenceProvider() {
60         return this.persistenceProvider;
61     }
62
63     public Map JavaDoc getJpaPropertyMap() {
64         Properties JavaDoc jpaProperties = new Properties JavaDoc();
65
66         if (getDatabasePlatform() != null) {
67             jpaProperties.setProperty(Environment.DIALECT, getDatabasePlatform());
68         }
69         else if (getDatabase() != null) {
70             Class JavaDoc databaseDialectClass = determineDatabaseDialectClass(getDatabase());
71             if (databaseDialectClass != null) {
72                 jpaProperties.setProperty(Environment.DIALECT, databaseDialectClass.getName());
73             }
74         }
75
76         if (isGenerateDdl()) {
77             jpaProperties.setProperty(Environment.HBM2DDL_AUTO, "update");
78         }
79         if (isShowSql()) {
80             jpaProperties.setProperty(Environment.SHOW_SQL, "true");
81         }
82
83         return jpaProperties;
84     }
85
86     /**
87      * Determine the Hibernate database dialect class for the given target database.
88      * @param database the target database
89      * @return the Hibernate database dialect class, or <code>null<code> if none found
90      */

91     protected Class JavaDoc determineDatabaseDialectClass(Database database) {
92         switch (database) {
93             case DB2: return DB2Dialect.class;
94             case HSQL: return HSQLDialect.class;
95             case INFORMIX: return InformixDialect.class;
96             case MYSQL: return MySQLDialect.class;
97             case ORACLE: return Oracle9Dialect.class;
98             case POSTGRESQL: return PostgreSQLDialect.class;
99             case SQL_SERVER: return SQLServerDialect.class;
100             case SYBASE: return SybaseDialect.class;
101             default: return null;
102         }
103     }
104
105     public Class JavaDoc<? extends EntityManager> getEntityManagerInterface() {
106         return HibernateEntityManager.class;
107     }
108
109     public JpaDialect getJpaDialect() {
110         return this.jpaDialect;
111     }
112
113 }
114
Popular Tags