I have changed the code in few model classes from field accessors to property accessors and also changed from List to Set because order was not important. Following is my entity class.
@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;
}
I debugged into the source code and found that it is throwing following exception when committing transaction after getting the data from the repository.
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
I am not sure how to fix it. What am I missing?
Client code:
@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();
}
}
}