2016-02-15 74 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),打破春天启动的自动配置。