01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 info.jtrac.hibernate;
18:
19: import org.hibernate.cfg.Configuration;
20: import org.hibernate.tool.hbm2ddl.SchemaUpdate;
21: import org.slf4j.Logger;
22: import org.slf4j.LoggerFactory;
23: import org.springframework.util.StringUtils;
24:
25: /**
26: * Utilities to create the database schema, drop and create tables
27: * Uses Hibernate Schema tools
28: * Used normally at application first start
29: */
30: public class SchemaHelper {
31:
32: private final Logger logger = LoggerFactory.getLogger(getClass());
33:
34: private String driverClassName;
35: private String url;
36: private String username;
37: private String password;
38: private String hibernateDialect;
39: private String dataSourceJndiName;
40: private String[] mappingResources;
41:
42: public void setDriverClassName(String driverClassName) {
43: this .driverClassName = driverClassName;
44: }
45:
46: public void setHibernateDialect(String hibernateDialect) {
47: this .hibernateDialect = hibernateDialect;
48: }
49:
50: public void setMappingResources(String[] mappingResources) {
51: this .mappingResources = mappingResources;
52: }
53:
54: public void setUrl(String url) {
55: this .url = url;
56: }
57:
58: public void setUsername(String username) {
59: this .username = username;
60: }
61:
62: public void setPassword(String password) {
63: this .password = password;
64: }
65:
66: public void setDataSourceJndiName(String dataSourceJndiName) {
67: this .dataSourceJndiName = dataSourceJndiName;
68: }
69:
70: /**
71: * create tables using the given Hibernate configuration
72: */
73: public void createSchema() {
74: Configuration cfg = new Configuration();
75: if (StringUtils.hasText(dataSourceJndiName)) {
76: cfg.setProperty("hibernate.connection.datasource",
77: dataSourceJndiName);
78: } else {
79: cfg.setProperty("hibernate.connection.driver_class",
80: driverClassName);
81: cfg.setProperty("hibernate.connection.url", url);
82: cfg.setProperty("hibernate.connection.username", username);
83: cfg.setProperty("hibernate.connection.password", password);
84: }
85: cfg.setProperty("hibernate.dialect", hibernateDialect);
86: for (String resource : mappingResources) {
87: cfg.addResource(resource);
88: }
89: logger
90: .info("begin database schema creation =========================");
91: new SchemaUpdate(cfg).execute(true, true);
92: logger
93: .info("end database schema creation ===========================");
94: }
95:
96: }
|