本文介绍了SpingREST:无法打开JPA EntityManager进行事务;嵌套的异常是org.hiberna的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我启动Postman来查看我的SpringREST服务正在运行时,出现以下错误:

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

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

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

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

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.

谢谢大家.

推荐答案

很明显似乎存在连接问题.请检查您的application.properties文件,并查看数据库配置是否正确.

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

这篇关于SpingREST:无法打开JPA EntityManager进行事务;嵌套的异常是org.hiberna的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:41