Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package cn.org.citycloud.srdz.utils; import java.sql.Connection; import java.sql.SQLException; import javax.sql.DataSource; import org.apache.commons.lang3.StringUtils; import org.hibernate.Hibernate; import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.MySQL5InnoDBDialect; import org.hibernate.dialect.Oracle10gDialect; import org.hibernate.dialect.PostgreSQL82Dialect; import org.hibernate.dialect.SQLServer2008Dialect; /** * ?application.properties???? * applicationContext.xml * @author mxy * */ public class Hibernates { /** * Initialize the lazy property value. * * e.g. Hibernates.initLazyProperty(user.getGroups()); */ public static void initLazyProperty(Object proxyedPropertyValue) { Hibernate.initialize(proxyedPropertyValue); } /** * DataSoure?connection, ?connectionmetadatajdbcUrlDialect. * ?Oracle, H2, MySql, PostgreSql, SQLServer? */ public static String getDialect(DataSource dataSource) { String jdbcUrl = getJdbcUrlFromDataSource(dataSource); // ?jdbc urldialect if (StringUtils.contains(jdbcUrl, ":h2:")) { return H2Dialect.class.getName(); } else if (StringUtils.contains(jdbcUrl, ":mysql:")) { return MySQL5InnoDBDialect.class.getName(); } else if (StringUtils.contains(jdbcUrl, ":oracle:")) { return Oracle10gDialect.class.getName(); } else if (StringUtils.contains(jdbcUrl, ":postgresql:")) { return PostgreSQL82Dialect.class.getName(); } else if (StringUtils.contains(jdbcUrl, ":sqlserver:")) { return SQLServer2008Dialect.class.getName(); } else { throw new IllegalArgumentException("Unknown Database of " + jdbcUrl); } } private static String getJdbcUrlFromDataSource(DataSource dataSource) { Connection connection = null; try { connection = dataSource.getConnection(); if (connection == null) { throw new IllegalStateException("Connection returned by DataSource [" + dataSource + "] was null"); } return connection.getMetaData().getURL(); } catch (SQLException e) { throw new RuntimeException("Could not get database url", e); } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { } } } } }