大佬教程收集整理的这篇文章主要介绍了即使数据库不存在,也可以使用Hibernate动态创建PostgreSQL数据库,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
该hbmddl
工具只能为现有架构创建表,而不能为您创建架构。在运行该工具之前,数据库必须存在。这是因为数据库必须由管理员创建,并且应该为其分配所有者。
因为在大多数应用程序中,该应用程序只能访问具有限制性特权的数据库角色,所以不需要这种功能。
POSTGResql不支持通过连接URL即时创建数据库。您可以InitializingBean
使用管理帐户和默认的PostgreSql数据库在应用程序启动时添加一个连接到数据库服务器的应用程序,如果应用程序数据库不存在,则可以发出CREATE
DATABASE。或者,只需使用Flyway。
使用H2,
Environment.HBM2DDL_AUTO,"create"
如果数据库尚不存在,则创建数据库。
但是,在Postgres中,不会创建不存在的数据库,因此会引发异常,例如“ DB不存在”。有没有一种方法可以配置POSTGRes按需创建不存在的数据库?
以下配置文件可用于重现该问题:
使用H2可以正常工作:
package test.POSTGRessql;
import java.util.Properties;
import javax.sql.Datasource;
import org.hibernate.cfg.Environment;
import org.springframework.beans.factory.Annotation.Autowired;
import org.springframework.context.Annotation.bean;
import org.springframework.context.Annotation.ComponentScan;
import org.springframework.context.Annotation.Configuration;
import org.springframework.context.Annotation.Propertysource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriveRMANagerDatasource;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpatransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntitymanagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformtransactionManager;
import org.springframework.transaction.Annotation.EnabletransactionManagement;
@Configuration
@Propertysource("file:C:/springconfig/qpmlib.properties")
@ComponentScan(basePackages = {"test.POSTGRessql"})
@EnableJpaRepositories(basePackages = { "test.POSTGRessql" })
@EnabletransactionManagement
public abstract class H2DBConfig {
@Autowired
org.springframework.core.env.Environment env;
public static final String db_name = getNewDBName();
@Bean
public Datasource datasource() {
DriveRMANagerDatasource dmds = new DriveRMANagerDatasource();
dmds.setDriverClassName("org.h2.Driver");
dmds.setUrl("jdbc:h2:tcp://localhost/~/" + db_name );
dmds.setUsername(env.getProperty("h2user"));
dmds.setpassword(env.getProperty("h2pw"));
return dmds;
}
private static String getNewDBName() {
return "H2DBTest";
}
@Bean
public LocalContainerEntitymanagerFactoryBean entitymanagerFactory() {
LocalContainerEntitymanagerFactoryBean factory = new LocalContainerEntitymanagerFactoryBean();
factory.setDatasource(datasource());
factory.setPersistenceUnitName(db_name);
factory.setPackagesToScan("test.POSTGRessql");
factory.setJpaVendorAdapter(jpaAdapter());
factory.setJpaProperties(jpaProperties());
factory.afterPropertiesSet();
return factory;
}
@Bean
public PlatformtransactionManager transactionManager() {
JpatransactionManager txm = new JpatransactionManager(
entitymanagerFactory().getObject());
return txm;
}
@Bean
public JpaVendorAdapter jpaAdapter() {
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabase(Database.H2);
adapter.setGenerateDdl(true);
adapter.setShowSql(true);
return adapter;
}
@Bean
public HibernateExceptionTranslator exceptionTranslator() {
return new HibernateExceptionTranslator();
}
public Properties jpaProperties() {
Properties properties = new Properties();
properties.put(Environment.SHOW_SQL,"true");
properties.put(Environment.HBM2DDL_AUTO,"create");
properties.put(Environment.DIALECT,"org.hibernate.dialect.H2Dialect");
return properties;
}
}
package test.POSTGRessql;
import java.util.Properties;
import javax.sql.Datasource;
import org.hibernate.cfg.Environment;
import org.springframework.beans.factory.Annotation.Autowired;
import org.springframework.context.Annotation.bean;
import org.springframework.context.Annotation.ComponentScan;
import org.springframework.context.Annotation.Configuration;
import org.springframework.context.Annotation.Propertysource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriveRMANagerDatasource;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpatransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntitymanagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformtransactionManager;
import org.springframework.transaction.Annotation.EnabletransactionManagement;
@Configuration
@Propertysource("file:C:/springconfig/qpmlib.properties")
@ComponentScan(basePackages = {"test.POSTGRessql"})
@EnableJpaRepositories(basePackages = { "test.POSTGRessql" })
@EnabletransactionManagement
public abstract class PGDBConfig {
@Autowired
org.springframework.core.env.Environment env;
public static final String db_name = getNewDBName();
@Bean
public Datasource datasource() {
DriveRMANagerDatasource dmds = new DriveRMANagerDatasource();
dmds.setDriverClassName("org.POSTGResql.Driver");
dmds.setUrl("jdbc:POSTGResql://localhost:5432/" + db_name);
dmds.setUsername(env.getProperty("POSTGResuser"));
dmds.setpassword(env.getProperty("POSTGRespw"));
return dmds;
}
private static String getNewDBName() {
return "POSTGResDBTest";
}
@Bean
public LocalContainerEntitymanagerFactoryBean entitymanagerFactory() {
LocalContainerEntitymanagerFactoryBean factory = new LocalContainerEntitymanagerFactoryBean();
factory.setDatasource(datasource());
factory.setPersistenceUnitName(db_name);
factory.setPackagesToScan("test.POSTGRessql");
factory.setJpaVendorAdapter(jpaAdapter());
factory.setJpaProperties(jpaProperties());
factory.afterPropertiesSet();
return factory;
}
@Bean
public PlatformtransactionManager transactionManager() {
JpatransactionManager txm = new JpatransactionManager(
entitymanagerFactory().getObject());
return txm;
}
@Bean
public JpaVendorAdapter jpaAdapter() {
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabase(Database.POSTGRESQL);
adapter.setGenerateDdl(true);
adapter.setShowSql(true);
return adapter;
}
@Bean
public HibernateExceptionTranslator exceptionTranslator() {
return new HibernateExceptionTranslator();
}
public Properties jpaProperties() {
Properties properties = new Properties();
properties.put(Environment.SHOW_SQL,"org.hibernate.dialect.POSTGReSQL9Dialect");
return properties;
}
}
以上是大佬教程为你收集整理的即使数据库不存在,也可以使用Hibernate动态创建PostgreSQL数据库全部内容,希望文章能够帮你解决即使数据库不存在,也可以使用Hibernate动态创建PostgreSQL数据库所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。