0

我已经将几个模型类中的代码从字段访问器更改为属性访问器,并且还从 List 更改为 Set,因为顺序并不重要。以下是我的实体类。

@Entity
@Table(name = "TPYP_GAME")
public class Game extends AbstractGeneratedIdEntity{
    private static final long serialVersionUID = -1390767030654384142L;

    private static final String GAME_FIELD = "GAME";

    private GameypeEnum gameTypeEnum;    
    private Set<GameCode> gameCodes;    

    @Transient
    public GameTypeEnum getGameTypeEnum() {
        return gameTypeEnum;
    }

    @OneToMany(cascade = CascadeType.ALL, mappedBy = GAME_FIELD, fetch = FetchType.LAZY)
    public Set<GameCode> getGameCodes() {
        return gameCodes;
    }

    public void setGameTypeEnum(GameTypeEnum gameTypeEnum) {
        this.gameTypeEnum = gameTypeEnum;
    }

    public void setGameCodes(Set<GameCode> gameCodes) {
        this.gameCodes = gameCodes;
    }

我调试了源代码,发现它在从存储库获取数据后提交事务时抛出以下异常。

 org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction;
 nested exception is javax.persistence.RollbackException: Error while committing the transaction
 javax.persistence.RollbackException: Error while committing the transaction java.util.HashSet 
 cannot be cast to org.hibernate.collection.PersistentCollection

我不知道如何解决它。我错过了什么?


客户端代码:

    @Transactional(readOnly = true) 
    public List<Game> getGames(String nameContains, Long pageSize){ 
    if(!nameContains.isEmpty() && pageSize !=null){ 
    return repository.findAll(nameIsLike(nameContains), constructPageSpecification(pageSize)).getContent(); }
    else{ if(pageSize != null){ 
    return repository.findAll(null, constructPageSpecification(pageSize)).getContent(); }
    else if(!nameContains.isEmpty()){ 
    return repository.findAll(nameIsLike(nameContains)); }else{ return repository.findAll(); 
}
 }
     }
4

1 回答 1

2

看起来setGameCodes是用新创建的数据库替换Set读取,HashSet而不是从现有数据库中添加或删除,这会在您尝试将更改提交回数据库时导致问题。

也许setGameCodes应该做

this.gameCodes.clear();
this.gameCodes.addAll(gameCodes);

代替

this.gameCodes = gameCodes;
于 2013-09-11T15:31:43.447 回答