2017-03-13 51 views
1

當Spring Boot與PostgreSQL連接時出現問題。我似乎無法使其工作。如果有遺漏我可以給它更多的給你,但現在這是足夠的信息Spring Boot + PostgreSQL:在類路徑資源中定義名稱爲'entityManagerFactory'的bean時出錯

完整的錯誤:

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 [email protected] 
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; 
    } 
} 

回答

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); 
    } 

} 
1

我已經解決了這一問題。

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); 
    } 
} 
1

(1)在您的文件application.properties,注意spring.datasource.username=postgres [email protected]是2行,而不是1行。

(2)由於此錯誤:

Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

你缺少 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

+0

有沒有辦法讓org.hibernate.dialect.PostgreSQL95Dialect與spring-boot-starter-data-jpa:1.5.3.RELEASE?它不包含可能會有的冬眠版本。 – Ville

+1

您應該使用最新的PostgreSQL版本,然後使用'org.hibernate.dialect.PostgreSQL95Dialect'而不是'org.hibernate.dialect.PostgreSQLDialect' Spring Boot 1.5.3.RELEASE使用Hibernate託管依賴版本5.0.12.Final。請參閱: https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/dialect/PostgreSQL95Dialect.html(section hibernate-orm)和 http://docs.spring.io/spring- boot/docs/current/reference/htmlsingle /#appendix-dependency-versions –

+1

您必須重寫託管依賴項版本。但它會讓你的應用程序變得不穩定。 –

相關問題