多次调用 jpa repository.save() 抛出异常

Posted

技术标签:

【中文标题】多次调用 jpa repository.save() 抛出异常【英文标题】:Calling jpa repository.save() multiple times throws exception 【发布时间】:2015-10-10 21:57:44 【问题描述】:

我想多次调用 jpa repository.save。

但是当我第二次尝试保存对象时,它会抛出 PersistentObjectException 并说分离的实体传递给持久化。

代码和异常如下。我错过了什么?

public void createGame(GameTeamRequest gameTeamRequest) 
        for(Entry<String, Integer> hm : gameTeamRequest.getTeamsInnigsPostionMap().entrySet()) 
            GameTeam gameTeam = new GameTeam();
            Team team = new Team();
            team.setTeamName(hm.getKey());
            gameTeam.setTeam(team);
            gameTeam.setGame(gameTeamRequest.getGame());
            gameTeam.setInningsPosition(hm.getValue());
            gameTeamRepository.save(gameTeam);
        
    

例外情况如下:

SEVERE: Servlet.service() for servlet [appServlet] in context with path [/iscorecard] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.dragonfly.iscorecard.domain.Game; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.dragonfly.iscorecard.domain.Game] with root cause
org.hibernate.PersistentObjectException: detached entity passed to persist: com.dragonfly.iscorecard.domain.Game
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:141)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:842)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:835)
    at org.hibernate.engine.spi.EJB3CascadingAction$1.cascade(EJB3CascadingAction.java:52)
    at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:387)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:330)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165)
    at org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:424)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:263)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:192)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:135)
    at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:78)
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:208)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:151)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:852)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:826)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:830)
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:865)

游戏类:

  @Entity
@Table(name = "GAME")

public class Game extends Model 

@Column(name = "TOURNAMENT_ID")
private String tournamentId;

@OneToMany(mappedBy = "game")
private Set<GameTeam> gameTeam;          

团队班:

    @Entity
@Table(name = "TEAM")

    public class Team extends Model 

    @Column(name = "TEAM_NAME")
    private String teamName;

    @OneToMany(mappedBy = "game")
    private Set<GameTeam> gameTeam;


游戏团队:

    @Entity
@Table(name = "GAME_TEAM")

    public class GameTeam extends Model 

    @ManyToOne(cascade =  CascadeType.PERSIST, CascadeType.MERGE )
    @JoinColumn(name = "GAME_ID")
    private Game game;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "TEAM_ID")
    private Team team;

    @Column(name = "INNINGS_POSITION")
    private int inningsPosition;


【问题讨论】:

可能是您忽略了一些 JPA 规则,您的示例没有提供所有信息,我邀请您查看此网站并阅读其他内容aquiseprograma.co/category/jpa 【参考方案1】:

例外是指 Game 对象,因此看起来您正在为第二个 GameTeam 设置一个分离的 Game。 您可以通过添加(至少)来解决此问题

 cascade =  CascadeType.PERSIST, CascadeType.MERGE 

到您的映射。

【讨论】:

我应该将它添加到哪个类和字段中? 到 GameTeam 中游戏属性的映射 - 也许将您的实体代码添加到您的问题中以获得更详细的答案。 然后添加您的实体

以上是关于多次调用 jpa repository.save() 抛出异常的主要内容,如果未能解决你的问题,请参考以下文章

使用JPA中@Query 注解实现update 操作

Spring数据jpa保存无法获取id

Spring数据jpa保存无法获取id

JPA 和数据库交互

Spring Boot JPA 批量插入

Hibernate/JPA - 使用@Future 更新还包含日期字段的实体字段