gpt4 book ai didi

java.util.HashSet 无法转换为 org.hibernate.collection.PersistentCollection

转载 作者:行者123 更新时间:2023-12-02 06:44:59 27 4
gpt4 key购买 nike

我已将几个模型类中的代码从字段访问器更改为属性访问器,并且还从 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();
}
}
}

最佳答案

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

也许setGameCodes应该做

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

而不是

this.gameCodes = gameCodes;

关于java.util.HashSet 无法转换为 org.hibernate.collection.PersistentCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18745252/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com