【问题标题】:Spring Boot + PostgreSQL: Error creating bean with name 'entityManagerFactory' defined in class path resourceSpring Boot + PostgreSQL:创建类路径资源中定义的名称为“entityManagerFactory”的bean时出错
【发布时间】:2017-03-13 01:01:19
【问题描述】:

将 Spring Boot 与 PostgreSQL 连接时出现问题。我似乎无法让它工作。如果有什么遗漏,我可以给你更多,但现在这些信息已经足够了

完整的错误:

Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:run (default-cli) on project resorts-restful-project: An exception occurred while running. null: InvocationTargetException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 

这是我的配置:

application.properties:

spring.datasource.url= jdbc:postgresql://localhost:5433/qwerty
spring.datasource.username=postgres spring.datasource.password=postgres@qwerty
spring.jpa.hibernate.ddl-auto=create-drop

我的模特:

package com.fvthree.domain;

import javax.persistence.*;
import java.io.Serializable;

@Entity
public class Resort implements Serializable {
    @Id
    @GeneratedValue
    @Column(name="resorts_id")
    private Long id;

    @Column(name="name")
    private String name;
    @Column(name="location")
    private String location;

    @Column(name="contact_id")
    private Long contactId;

    public Resort() {
    }

    public Resort(Long id, String name, String location, Long contactId) {
        this.id = id;
        this.name = name;
        this.location = location;
        this.contactId = contactId;
    }

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public Long getContactId() {
        return contactId;
    }

    public void setContactId(Long contactId) {
        this.contactId = contactId;
    }
}

【问题讨论】:

    标签: spring postgresql spring-boot


    【解决方案1】:

    确保您已设置所有这些属性:

    spring.datasource.driverClassName=org.postgresql.Driver
    spring.datasource.url=
    spring.datasource.username=
    spring.datasource.password=
    
    spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
    spring.jpa.show-sql=false
    spring.jpa.hibernate.ddl-auto=create-drop
    

    你已经使用 main() 在类上启用了这些注释:

    @Configuration
    @EnableAutoConfiguration
    @ComponentScan
    public class Application {
    
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class, args);
        }
    
    } 
    

    【讨论】:

      【解决方案2】:

      我已经解决了这个问题。

      application.properties 文件需要完整:

      # Configure postgres
      
      spring.jpa.database=POSTGRESQL
      spring.datasource.platform=postgres
      spring.jpa.show-sql=true
      spring.jpa.hibernate.ddl-auto=create-drop
      spring.database.driverClassName=org.postgresql.Driver
      spring.datasource.url=jdbc:postgresql://localhost:5432/qweqwe
      spring.datasource.username=postgres
      spring.datasource.password=dontcopythis
      

      我还将@EntityScan 和@EnableJpaRepositories 添加到主目录:

      package com.fvthree;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.boot.autoconfigure.domain.EntityScan;
      import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
      
      @SpringBootApplication
      @EntityScan(basePackages = {"com.fvthree.domain" })
      @EnableJpaRepositories(basePackages = {"com.fvthree.repository"})
      public class ResortsRestfulProjectApplication {
      
          public static void main(String[] args) {
              SpringApplication.run(ResortsRestfulProjectApplication.class, args);
          }
      }
      

      【讨论】:

        【解决方案3】:

        (1) 在您的文件 application.properties 中,请注意 spring.datasource.username=postgres spring.datasource.password=postgres@qwerty 是 2 行,而不是 1 行。

        (2) 因为这个错误:

        访问 DialectResolutionInfo 时不能为空 'hibernate.dialect' 未设置

        你不见了 hibernate.dialect=...

        例如,如果您使用 PostgreSQL 9.5,它将是 hibernate.dialect=org.hibernate.dialect.PostgreSQL95Dialect

        参考:https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/dialect/package-summary.html

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-25
        • 2017-11-15
        • 2019-07-02
        • 2020-03-25
        • 1970-01-01
        • 2015-10-16
        相关资源
        最近更新 更多