Reputation: 901
I'm trying to connect spring boot application to Postgres database, but I cant setup, I follow the spring getting started tutorials and bealdum too, but I cannot fix the problem:
this is my pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>example</name>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
my properties file:
spring.datasource.url=jdbc:postgresql://localhost:5432/database
spring.datasource.username=user
spring.datasource.password=pass
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto = update
my spring boot application class:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EnableJpaRepositories
public class ExampleApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
}
}
controller class:
import com.example.example.entities.Thing;
import com.example.example.repositories.ThingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CreateThingController {
private ThingRepository thingRepository;
@Autowired
public CreateThingController(ThingRepository thingRepository){
this.thingRepository = thingRepository;
}
@PostMapping("/things")
public void createThing(@RequestBody Thing thing) {
this.thingRepository.save(thing);
}
}
My entity class:
import org.springframework.data.annotation.Id;
import javax.persistence.*;
@Entity
public class Thing {
@Id
@Column(name = "id", nullable = false, updatable = false)
private String id;
@Column(name = "name", nullable = false, updatable = false)
private String name;
public Thing(
String id,
String name
) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
}
and this is my repository interface:
import com.example.example.entities.Thing;
import org.springframework.stereotype.Repository;
import org.springframework.data.jpa.repository.JpaRepository;
@Repository
public interface ThingRepository extends JpaRepository<Thing, String>
{
}
The error is this:
Parameter 0 of constructor in com.example.example.controllers.CreateThingController required a bean named 'entityManagerFactory' that could not be found.
i trying to fix the error following other response and tutorials, but i fail. What could be the problem here? I need to configure something else?
Upvotes: 0
Views: 593
Reputation: 26572
You have all the dependencies but you are explicitly removing the database autoconfiguration during the startup:
spring.autoconfigure.exclude=
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Comment or remove that property.
Upvotes: 1
Reputation: 281
What is your Thing
class file name? change that file name like Thing
and restart the program.
Upvotes: 0