在Jhipster

时间:2016-02-11 08:19:23

标签: java database jdbc datasource jhipster

我需要在Jhipster项目中建立一个新的数据库连接,我正在做下一个

在application-dev.yml中添加:

 datasource:
    driver-class-name: org.postgresql.ds.PGSimpleDataSource
    url: jdbc:postgresql://localhost:5432/analytics
    name: analytics
    username: elser
    password: ******
 datasources:    
    elser:
        driver-class-name: org.hibernate.dialect.PostgreSQLDialect
        url: jdbc:postgresql://localhost:5432/elser
        name: elser
        username: elser
        password: ******

在DatabaseConfiguration.java中:

@Bean
@ConfigurationProperties(prefix="spring.datasources.elser")
public DataSource dataSourceElser() {
    return DataSourceBuilder.create().build();
}

我添加了一个新类来测试它:

@Inject
@Qualifier("dataSourceElser")
private DataSource dataSourceElser;

private JdbcTemplate jdbcTemplate;

@PostConstruct
public void init() {
    jdbcTemplate = new JdbcTemplate(dataSourceElser);

    int rowCount = this.jdbcTemplate.queryForObject("select count(*)       from commons.usuario", Integer.class);
    System.out.println(rowCount);
}

但它给了我下一个错误:

 java.sql.SQLException: org.hibernate.dialect.PostgreSQLDialect cannot be cast to java.sql.Driver

1 个答案:

答案 0 :(得分:1)

您当前正在指定hibernate方言,而不是JDBC驱动程序名称。你需要使用:

driver-class-name: org.postgresql.Driver

而不是driver-class-name: org.postgresql.ds.PGSimpleDataSource driver-class-name: org.hibernate.dialect.PostgreSQLDialect