插入新数据时出现javax.persistence.EntityNotFoundException

问题描述 投票:0回答:1

构建一个处理足球等惩罚的应用程序。比如训练太晚了等等。

首先:它是一个带有 Vaadin 前端的 Spring Boot 应用程序和带有 Spring Data JPA 的 H2 数据库

问题在于向 H2 数据库添加新实体。插入玩家和惩罚本身没有问题,但是当我尝试将这些 Id 与另一个表(PunishmentToPlayer)映射时,它给了我一个 EntityNotFoundException。

数据库中的玩家和惩罚实体也是正确的。

实体播放器

@Setter
@Getter
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Player extends AbstractEntity{

    private String firstName;
    private String lastName;
}

实体惩罚

@Setter
@Getter
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Punishment extends AbstractEntity{

    private String description;
    private Double amount;
}

对玩家的实体惩罚

@Setter
@Getter
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class PunishmentToPlayer extends AbstractEntity{

    @OneToOne
    private Player player;

    @OneToOne
    private Punishment punishment;
}

尝试在此处添加我的示例数据

@Component
public class DataHelper {

    public void addPlayer(PlayerService playerService){
        playerService.saveOrUpdate(buildPlayer("Player1_Name", "Player1_Lastname"));
        playerService.saveOrUpdate(buildPlayer("Player2_Name", "Player2_Lastname"));
        playerService.saveOrUpdate(buildPlayer("Player3_Name", "Player3_Lastname"));
        playerService.saveOrUpdate(buildPlayer("Player4_Name", "Player4_Lastname"));
    }

    public void addPunishments(PunishmentService punishmentService){
        punishmentService.saveOrUpdate(buildPunishment("Description 1", 20.00));
        punishmentService.saveOrUpdate(buildPunishment("Description 2", 15.00));
        punishmentService.saveOrUpdate(buildPunishment("Description 3", 10.00));
        punishmentService.saveOrUpdate(buildPunishment("Description 4", 5.00));
    }

    public void addConnections(PunishmentToPlayerService punishmentToPlayerService, PlayerService playerService, PunishmentService punishmentService){
        List<Player> players =  playerService.getAllPlayer();
        List<Punishment> punishments = punishmentService.getAllPunishments();

        punishmentToPlayerService.saveOrUpdate(buildPunishmentToPlayer(players.get(0), punishments.get(0)));
        punishmentToPlayerService.saveOrUpdate(buildPunishmentToPlayer(players.get(0), punishments.get(2)));
        punishmentToPlayerService.saveOrUpdate(buildPunishmentToPlayer(players.get(1), punishments.get(1)));
        punishmentToPlayerService.saveOrUpdate(buildPunishmentToPlayer(players.get(2), punishments.get(0)));
        punishmentToPlayerService.saveOrUpdate(buildPunishmentToPlayer(players.get(2), punishments.get(3)));
        punishmentToPlayerService.saveOrUpdate(buildPunishmentToPlayer(players.get(3), punishments.get(0)));
        punishmentToPlayerService.saveOrUpdate(buildPunishmentToPlayer(players.get(3), punishments.get(3)));
        punishmentToPlayerService.saveOrUpdate(buildPunishmentToPlayer(players.get(3), punishments.get(3)));
    }

    private Player buildPlayer(String firstname, String lastname){
        return Player.builder()
                .firstName(firstname)
                .lastName(lastname)
                .build();
    }

    private Punishment buildPunishment(String description, double value){
        return Punishment.builder()
                .description(description)
                .amount(value)
                .build();
    }

    private PunishmentToPlayer buildPunishmentToPlayer(Player player, Punishment punishment){
        return PunishmentToPlayer.builder()
                .player(player)
                .punishment(punishment)
                .build();
    }
}

调试时,它向我显示单个 PunishmentToPlayerObjects 中的所有正确数据

在 DataHelper 的 saveOrUpdate 方法中,PunishmentToPlayerService 调用方法:

public void saveOrUpdate(PunishmentToPlayer punishmentToPlayer){
        punishmentToPlayerRepository.save(punishmentToPlayer);
}

仅指 Spring Data 中 CrudRepository 的标准

save
方法

这里是堆栈跟踪:

Caused by: org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find de.nils.Tus_Strafen.model.Player with id a62dde9d-2514-44d8-9d97-7d54afd69ef8; nested exception is javax.persistence.EntityNotFoundException: Unable to find de.nils.Tus_Strafen.model.Player with id a62dde9d-2514-44d8-9d97-7d54afd69ef8
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:379) ~[spring-orm-5.3.22.jar:5.3.22]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:235) ~[spring-orm-5.3.22.jar:5.3.22]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:551) ~[spring-orm-5.3.22.jar:5.3.22]
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61) ~[spring-tx-5.3.22.jar:5.3.22]
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242) ~[spring-tx-5.3.22.jar:5.3.22]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152) ~[spring-tx-5.3.22.jar:5.3.22]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.22.jar:5.3.22]
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:174) ~[spring-data-jpa-2.7.2.jar:2.7.2]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.22.jar:5.3.22]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-5.3.22.jar:5.3.22]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.22.jar:5.3.22]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) ~[spring-aop-5.3.22.jar:5.3.22]
    at com.sun.proxy.$Proxy123.save(Unknown Source) ~[na:na]
    at de.nils.Tus_Strafen.service.PunishmentToPlayerService.saveOrUpdate(PunishmentToPlayerService.java:48) ~[classes/:na]
    at de.nils.Tus_Strafen.DataHelper.addConnections(DataHelper.java:35) ~[classes/:na]
    at de.nils.Tus_Strafen.view.views.MainAppLayout.addData(MainAppLayout.java:81) ~[classes/:na]
    at de.nils.Tus_Strafen.view.views.MainAppLayout.<init>(MainAppLayout.java:41) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481) ~[na:na]
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:211) ~[spring-beans-5.3.22.jar:5.3.22]

并没有真正尝试任何特别的事情,因为它应该有效。

java spring spring-boot spring-data-jpa spring-data
1个回答
0
投票

尝试为

player
类中的
punishment
PunishmentToPlayer
字段添加级联类型

public class PunishmentToPlayer extends AbstractEntity{

  @OneToOne(cascade = CascadeType.ALL)
  private Player player;

  @OneToOne(cascade = CascadeType.ALL)
  private Punishment punishment;
}
© www.soinside.com 2019 - 2024. All rights reserved.