SpingREST: Could not open JPA EntityManager for transaction; nested exception is org.hiberna

夙愿已清 提交于 2019-12-08 11:46:08

问题


When I start Postman to see my SpringREST service running I get following error:

{
    "timestamp": 1506965117328,
    "status": 500,
    "error": "Internal Server Error",
    "exception": "org.springframework.transaction.CannotCreateTransactionException",
    "message": "Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection",
    "path": "/api/matchs"
}

I don't know where the error is, here are the classes of the project:

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>

    <groupId>com.kaluzny</groupId>
    <artifactId>spring-boot-rest-api-postgresql</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/>
    </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>
        <!-- Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!-- PostgreSQL-->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1201-jdbc41</version>
            <scope>runtime</scope>
        </dependency>
        <!-- Java EE -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Aplication.java

package com.kaluzny;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

MatchRestController.java

package com.kaluzny.web;

import com.kaluzny.domain.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.util.Collection;

@RestController
@RequestMapping("/api/matchs")
public class MatchRestController {

    private MatchRepository repository;

    @Inject
    public void setRepository(MatchRepository repository) {
        this.repository = repository;
    }

    @RequestMapping(
            method = RequestMethod.POST)
    public ResponseEntity<?> addMatch(@RequestBody Match match) {
        return new ResponseEntity<>(repository.save(match), HttpStatus.CREATED);
    }

    @RequestMapping(
            method = RequestMethod.GET)
    public ResponseEntity<Collection<Match>> getAllMatch() {
        return new ResponseEntity<>(repository.findAll(), HttpStatus.OK);
    }

    @RequestMapping(
            value = "/{idMatch}",
            method = RequestMethod.GET)
    public ResponseEntity<Match> getMatchWithIdMatch(@PathVariable Integer idMatch) {
        return new ResponseEntity<>(repository.findOne(idMatch), HttpStatus.OK);
    }

    @RequestMapping(
            value = "/{idMatch}",
            method = RequestMethod.PUT)
    public ResponseEntity<Match> updateMatchFromDB(@PathVariable("idMatch") Integer idMatch, @RequestBody Match match) {

        Match currentMatch = repository.findOne(idMatch);
        currentMatch.setIdMatch(match.getIdMatch());
        currentMatch.setNumberPlayers(match.getNumberPlayers());
        currentMatch.setWinner(match.getWinner());
        currentMatch.setScore(match.getScore());
        currentMatch.setNumberSpike(match.getNumberSpike());
        currentMatch.setNumberFireball(match.getNumberFireball());
        currentMatch.setNumberNuke(match.getNumberNuke());

        Match currentMatch1 = repository.findOne(idMatch);



        return new ResponseEntity<>(repository.save(currentMatch1), HttpStatus.OK);
    }

    @RequestMapping(
            value = "/{idMatch}",
            method = RequestMethod.DELETE)
    public void deleteMatchWithId(@PathVariable Integer idMatch) {
        repository.delete(idMatch);
    }

    @RequestMapping(
            method = RequestMethod.DELETE)
    public void deleteAllMatchs() {
        repository.deleteAll();
    }
}

MatchRepository.java

package com.kaluzny.domain;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.util.List;

@RepositoryRestResource
public interface MatchRepository extends JpaRepository<Match, Integer> {
    List<Match> findByIdMatch(Integer idMatch);
}

Match.java

package com.kaluzny.domain;

import javax.persistence.*;

@Entity
public class Match {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)

    private Integer idMatch;
    private Integer numberPlayers;
    private String winner;
    private Integer score;
    private Integer numberSpike;
    private Integer numberFireball;
    private Integer numberNuke;


    public Match(Integer idMatch, Integer numberPlayers, String winner, Integer score, Integer numberSpike, Integer numberFireball, Integer numberNuke) {
        this.idMatch = idMatch;
        this.numberPlayers = numberPlayers;
        this.winner = winner;
        this.score = score;
        this.numberSpike = numberSpike;
        this.numberFireball = numberFireball;
        this.numberNuke = numberNuke;
    }

    public Match() {
    }

    public void setIdMatch(Integer idMatch){
        this.idMatch = idMatch;
    }

    public Integer getIdMatch() {
        return idMatch;
    }

    public void setNumberPlayers(Integer numberPlayers){
        this.numberPlayers = numberPlayers;
    }

    public Integer getNumberPlayers(){
        return numberPlayers;
    }

    public void setWinner(String winner){
        this.winner = winner;
    }

    public String getWinner(){
        return winner;
    }

    public void setScore(Integer score){
        this.score = score;
    }

    public Integer getScore(){
        return score;
    }

    public void setNumberSpike(Integer numberSpike){
        this.numberSpike = numberSpike;
    }

    public Integer getNumberSpike(){
        return numberSpike;
    }

    public void setNumberFireball(Integer numberFireball){
        this.numberFireball = numberFireball;
    }

    public Integer getNumberFireball(){
        return numberFireball;
    }

    public void setNumberNuke(Integer numberNuke){
        this.numberNuke = numberNuke;
    }

    public Integer getNumberNuke(){
        return numberNuke;
    }


    @Override
    public String toString() {
        return "Partida{" +
            "Indentificador Partida: " + numberPlayers + '\'' +
            "Numero Jogadores: " + numberPlayers + '\'' +
            "Vencedor: "+ winner  + '\'' +
            "Pontuação: " + score + '\'' +
            "Numero de Espinhos: " + numberSpike + '\'' +
            "Numero de Fireball" + numberFireball + '\'' +
            "Numero de Nukes" + numberNuke + '}';
    }
}

As I said I don't know where the problem is, I have fixed a few of them but this one has been difficult.

Thanks everyone.


回答1:


It clear seems that there is a connection issue. please check your application.properties file and see if the database configuration is correct.



来源:https://stackoverflow.com/questions/46530896/spingrest-could-not-open-jpa-entitymanager-for-transaction-nested-exception-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!