2016-07-11 17 views
0

私はPostgreSQlとSpring 4で作業していて、実行中に私のアプリケーションが自動的にデータベースを作成したいと思っています。PostgreSQL + Hibernate + Spring自動作成データベース

私のエンティティクラスがある:

@Entity 
@Table(name = "user", schema = "public") 
public class User extends BaseEntity{ 
private Integer id; 
private String name; 
private Integer contractId; 

public User() { 
} 

public User(Integer id) { 
    super(id); 
} 

@Id 
@Column(name = "usr_id", nullable = false) 
@GeneratedValue(strategy= GenerationType.IDENTITY) 
public Integer getId() { 
    return id; 
} 

public void setId(Integer id) { 
    this.id = id; 
} 

@Basic 
@Column(name = "usr_name", nullable = true, length = -1) 
public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

@Basic 
@Column(name = "usr_contract_id", nullable = true) 
public Integer getContractId() { 
    return contractId; 
} 

public void setContractId(Integer contractId) { 
    this.contractId = contractId; 
} 

} 

HibernateConfig.java

@Configuration 
@EnableTransactionManagement(proxyTargetClass = true) 
@PropertySources({ 
    @PropertySource(value = "classpath:application.properties")}) 
@ConfigurationProperties(prefix = "spring.datasource") 
public class HibernateConfig { 

@Autowired 
private Environment environment; 

@Autowired 
private DataSource dataSource; 

@Autowired 
private MultiTenantConnectionProvider multiTenantConnectionProvider; 

@Autowired 
private CurrentTenantIdentifierResolver currentTenantIdentifierResolver; 

public HibernateConfig() { 

} 

@Bean 
public LocalSessionFactoryBean sessionFactory() throws Exception { 

    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); 
    sessionFactory.setDataSource(dataSource); 
    sessionFactory.setHibernateProperties(hibernateProperties()); 

    sessionFactory.setPackagesToScan(new String[] { 
      "com.xxx.xxx.model", 
    }); 

    return sessionFactory; 
} 

private Properties hibernateProperties() { 
    Properties properties = new Properties(); 
    properties.put(DIALECT, environment.getRequiredProperty(DIALECT)); 
    properties.put(SHOW_SQL, environment.getRequiredProperty(SHOW_SQL)); 
    properties.put(FORMAT_SQL, environment.getRequiredProperty(FORMAT_SQL)); 
    properties.put(HBM2DDL_AUTO, environment.getRequiredProperty(HBM2DDL_AUTO)); 

    return properties; 
} 

@Bean 
@Primary 
@Autowired 
public HibernateTransactionManager transactionManager(SessionFactory s) { 
    HibernateTransactionManager txManager = new HibernateTransactionManager(); 
    txManager.setSessionFactory(s); 
    return txManager; 
} 

@Bean 
@Autowired 
public HibernateTemplate hibernateTemplate(SessionFactory s) { 
    HibernateTemplate hibernateTemplate = new HibernateTemplate(s); 
    return hibernateTemplate; 
} 
} 

application.properties

# Database connection settings: 
jdbc.driverClassName=org.postgresql.Driver 
jdbc.url=jdbc:postgresql://localhost:5432/database 
jdbc.username=postgres 
jdbc.password=111111 

hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect 
hibernate.show_sql=false 
hibernate.format_sql=false 
hibernate.hbm2ddl.auto=update 

spring.datasource.initialSize=50 
spring.datasource.maxActive=200 
spring.datasource.maxIdle=200 
spring.datasource.minIdle=50 

しかし、私はテーブルのユーザーにアクセスするためのSQLを実行している場合、このエラーが表示されます。 :テーブル 'User'は存在しません。

Hibernateでデータベースを自動作成するにはどうすればよいですか?私に助言を与えてください ありがとう

+0

hibernate.hbm2ddl.auto = createに疲れましたか? – sAm

答えて

0

プロパティhibernate.hbm2ddl.autoあなたのためのトリックを行います。 SessionFactoryが作成されると、スキーマDDLがデータベースに自動的に検証またはエクスポートされます。 create-dropを使用すると、SessionFactoryが明示的に閉じられると、データベーススキーマが削除されます。

Hibernateは上記のプロパティに対してこれらのオプションを受け入れることができます。

validate:スキーマを検証し、データベースを変更しません。

update:スキーマを更新します。

create:以前のデータを破棄してスキーマを作成します。

create-drop:セッションの最後にスキーマを削除します。

0

mysqlとは異なり、PostgresはCreate Database If not existをサポートしていません。

したがって、hibernate.hbm2ddl.auto=createを変更し、URLを変更すると、jdbc.url=jdbc:postgresql://localhost/database?createDatabaseIfNotExist=true は動作しません。

しかし、あなたは、以下の質問のような挙動をシミュレーションしてみてください:

Create Postgres database on the fly, if it doesn't exists using Hibernate

Simulate CREATE DATABASE IF NOT EXISTS for PostgreSQL?

1

は、これは私のための作業である

spring.jpa.hibernate.ddl-auto=update 
spring.jpa.generate-ddl=true 
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL94Dialect 

spring.datasource.driverClassName=org.postgresql.Driver 
spring.datasource.url= jdbc:postgresql://localhost:5432/postgres 
spring.datasource.username=postgres 
spring.datasource.password=123 

spring.jpa.show-sql=true 
spring.session.store-type=none 

この方法を試してみてください。

SessionFactoryライフサイクルの一環としてSchemaManagementToolアクションを自動的に実行する設定。 - いいえ、アクションは実行されません

  • none:有効なオプションは、アクションの列挙型のexternalJpaName値によって定義されています。

  • create - データベース作成が生成されます。

  • drop - データベースの削除が生成されます。

  • drop-and-create - データベースの作成後にデータベースの削除が生成されます。そこspring.jpa.hibernate.ddl-auto=update ==>update

は、あなたのシナリオに応じて変更することができます。

関連する問題