0

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(); 
}
 }
     }
4
  • What's the exception that's being thrown? Please provide a stacktrace. Your question title mentions "java.util.HashSet cannot be cast to org.hibernate.collection.PersistentCollection", but there's no mention of it in your actual question. Commented Sep 11, 2013 at 15:27
  • show us your client code Commented Sep 11, 2013 at 15:32
  • I have edited my question and provided more information. Commented Sep 11, 2013 at 19:25
  • The problem may be related to this: hibernate.atlassian.net/browse/HHH-5862 Commented Oct 27, 2014 at 5:51

1 Answer 1

2

It looks like setGameCodes is replacing a Set read from the database with a newly created HashSet instead of adding to or removing from the existing one which causes problems when you try to commit changes back to the DB.

Perhaps setGameCodes should do

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

instead of

this.gameCodes = gameCodes;
Sign up to request clarification or add additional context in comments.

1 Comment

I don't think so that is the case because it is happening before the control returns back to my code. It is failing (throwing exception) right after it gets the data from the repository and when it tries to commit the transaction. org.hibernate.ejb.TransactionImpl:commit()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.