2016-02-15 109 views
1

我想將數據保存到postgresql中的表中。我正在使用 + postgresql以及hibernate。我的應用程序沒有任何錯誤,但它不是在數據庫中創建表。休眠不使用postgresql在spring啓動時自動創建表

這是我的控制器類

package com.ge.health.poc.controlleer; 

    import java.io.IOException; 

    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.web.bind.annotation.RequestBody; 
    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.bind.annotation.RequestMethod; 
    import org.springframework.web.bind.annotation.ResponseBody; 
    import org.springframework.web.bind.annotation.RestController; 

    import com.fasterxml.jackson.core.JsonParseException; 
    import com.fasterxml.jackson.databind.JsonMappingException; 
    import com.fasterxml.jackson.databind.ObjectMapper; 
    import com.ge.health.poc.model.Bookmodel; 
    import com.ge.health.poc.service.BookServiceImplementation; 

    @RestController 
    public class HttpController { 

     @Autowired 
     BookServiceImplementation bookserviceimpl; 

     @RequestMapping(value = "/httpmethod", method = RequestMethod.POST) 
     @ResponseBody 
     public void helloService(@RequestBody String input) throws JsonParseException, JsonMappingException, IOException { 
      System.out.println(input); 
      ObjectMapper mapper = new ObjectMapper(); 
      Bookmodel pojodata = mapper.readValue(input, Bookmodel.class); 

      System.out.println(pojodata); 

     } 
    } 

AppConfig.java

package com.ge.health.poc.configuration; 

    import java.util.Properties; 

    import javax.annotation.Resource; 
    import javax.jms.ConnectionFactory; 
    import javax.sql.DataSource; 

    import org.springframework.context.annotation.Bean; 
    import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; 
    import org.springframework.jdbc.datasource.DriverManagerDataSource; 
    import org.springframework.jms.core.JmsTemplate; 
    import org.springframework.orm.jpa.JpaTransactionManager; 
    import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 

    public class AppConfig { 

     @Resource 
     private SettingConfig settings; 

     @Bean 
     JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) { 
      return new JmsTemplate(connectionFactory); 
     } 

     @Bean 
     public DataSource dataSource() { 
      DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
      dataSource.setDriverClassName(settings.getDriverClassName()); 
      dataSource.setUrl(settings.getDatasource()); 
      dataSource.setUsername(settings.getUsername()); 
      dataSource.setPassword(settings.getPassword()); 
      return dataSource; 
     } 

     /** 
     * Declare the JPA entity manager factory. 
     */ 
     @Bean 
     public LocalContainerEntityManagerFactoryBean entityManagerFactory() { 
      LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean(); 

      // Hibernate properties 
      Properties additionalProperties = new Properties(); 
      additionalProperties.put("hibernate.dialect", settings.getDialect()); 
      additionalProperties.put("hibernate.show_sql", settings.getShowsql()); 
      additionalProperties.put("hibernate.hbm2ddl.auto", settings.getDdlauto()); 
      entityManagerFactory.setJpaProperties(additionalProperties); 

      return entityManagerFactory; 
     } 

     /** 
     * Declare the transaction manager. 
     */ 
     @Bean 
     public JpaTransactionManager transactionManager() { 
      JpaTransactionManager transactionManager = new JpaTransactionManager(); 
      transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); 
      return transactionManager; 
     } 

     /** 
     * PersistenceExceptionTranslationPostProcessor is a bean post processor 
     * which adds an advisor to any bean annotated with Repository so that any 
     * platform-specific exceptions are caught and then rethrown as one Spring's 
     * unchecked data access exceptions (i.e. a subclass of 
     * DataAccessException). 
     */ 
     @Bean 
     public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { 
      return new PersistenceExceptionTranslationPostProcessor(); 
     } 
    } 

SettingConfig.java

​​

application.properties

# Database 
    spring.datasource.driver-class-name=org.postgresql.Driver 
    spring.datasource.url=jdbc:postgresql://localhost:5432/bookdetails 
    spring.datasource.username=postgres 
    spring.datasource.password=admin 
    # Hibernate 
    hibernate.dialect=org.hibernate.dialect.MySQLDialect 
    hibernate.show_sql=true 
    hibernate.hbm2ddl.auto=create 

POJO類

package com.ge.health.poc.model; 

import javax.persistence.Column; 
import javax.persistence.Table; 

import org.springframework.data.annotation.Id; 
import org.springframework.stereotype.Component; 

@Component 
@Table 
public class Bookmodel { 

    @Id 
    private String id; 
    @Column 
    private String name; 
    @Column 
    private String isbn; 
    @Column 
    private String author; 
    @Column 
    private String pages; 

    public String getId() { 
     return id; 
    } 

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

    public String getPages() { 
     return pages; 
    } 

    public void setPages(String pages) { 
     this.pages = pages; 
    } 

    @Override 
    public String toString() { 
     return "Bookmodel [id=" + id + ", name=" + name + ", isbn=" + isbn + ", author=" + author + ", pages=" + pages 
       + "]"; 
    } 

    public String getName() { 
     return name; 
    } 

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

    public String getIsbn() { 
     return isbn; 
    } 

    public void setIsbn(String isbn) { 
     this.isbn = isbn; 
    } 

    public String getAuthor() { 
     return author; 
    } 

    public void setAuthor(String author) { 
     this.author = author; 
    } 

} 

我想將數據保存在postgresql.I表中正在使用的彈簧引導+ PostgreSQL的使用Hibernate一起。我的應用程序沒有任何錯誤,但它不是在數據庫中創建表。

+0

在你的hibernate.hbm2ddl.auto中,將它設置爲'update'。另外,如果你有Postgresql,你爲什麼使用MySQl方言,去PostgreSQL9Dialect。 –

+0

我試過了,它不工作 –

+0

org.hibernate.dialect.PostgreSQLDialect我現在使用這個方言,但仍然沒有創建表 –

回答

0

你是「使用」Spring Boot,你做的第一件事就是儘量不要使用Spring Boot。

而不是你自己做所有的配置讓Spring Boot爲你付出沉重的代價。

在您的application.properties中設置正確的方言並使用正確的屬性(請參閱Spring Boot Reference Guide的this section以獲取完整屬性列表)。

# Database 
spring.datasource.driver-class-name=org.postgresql.Driver 
spring.datasource.url=jdbc:postgresql://localhost:5432/bookdetails 
spring.datasource.username=postgres 
spring.datasource.password=admin 

# Hibernate 
spring.jpa.database=org.hibernate.dialect.PostgreSQL94Dialect 
spring.jpa.show-sql=true 
spring.jpa.hibernate.ddl-auto=create 

,並刪除您的自定義配置(即AppConfigSettingConfig),打破春天啓動的自動配置。