1

Sou iniciante em Spring Boot e Hibernate, fiz um projeto no Spring Initializr apenas com as dependencias Web, DevTools e PostgreSQL, mas depois eu adicionei a dependencia spring-boot-starter-data-jpa, fui seguindo uns tutoriais e fiz uma classe de configuração, segue o código:

package com.projetospringboot.meuprojeto.dao;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import javax.sql.DataSource;

@Configuration
public class DataConfiguration {

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.postgresql.Driver");     
        dataSource.setUrl(""+
        "jdbc:postgresql://localhost:5432/BancoProjetoEventos");
        dataSource.setUsername("postgres");
        dataSource.setPassword("postgres");
        return dataSource;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setDatabase(Database.POSTGRESQL);
        adapter.setShowSql(true);
        adapter.setGenerateDdl(true);
        adapter.setDatabasePlatform(""+
        "org.hibernate.dialect.PostgreSQLDialect");
        adapter.setPrepareConnection(true);
        return adapter;
    }
}

ae quando eu tento executar o projeto ele da o seguinte erro:

Error starting ApplicationContext. To display the conditions report re-run 
your application with 'debug' enabled.
2018-09-18 21:55:53.559 ERROR 2808 --- [  restartedMain] 
o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no 
embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on 
the classpath.
    If you have database settings to be loaded from a particular profile you 
may need to activate it (no profiles are currently active).


Process finished with exit code 0

pelo o que eu entendi ele deu erro na url que eu coloquei porém eu testei essa url e esta certa, esta conectando, alguem sabe como posso resolver este erro?


O pom.xml do meu projeto:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.projeto-spring-boot</groupId>
    <artifactId>meu-projeto</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>meu-projeto</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>
            UTF-8
        </project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20090211</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2 Respostas 2

0

Remova a seguinte dependência no seu pom.xml.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
1
  • Eu consegui resolver o problema, eu criaei um projeto novo no Spring Initializr selecionando as dependencias Web, DevTools, PostgreSQL e JPA e coloquei as mesmas classes que eu fiz nesse projeto e deu certo, e nesse projeto que funciona tem essa dependencia que vc falou pra mim tirar.
    – Bruno
    19/09/2018 às 21:22
0

Consegui resolver o meu problema int vou só atualizar aqui, eu apenas fui la no Spring Initializr e criei um novo projeto selecionando as dependencias Web, DevTools, PostgreSQL e JPA, colei o mesmo código que fiz antes nesse novo projeto e ele rodou certinho. Vlw flw!

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .