Hello,
I been searching for some time but I cant find any answers to my problem.
I have three classes Season, Game and GameSet with the following relations
Season
Code:
@OneToMany(targetEntity=Game.class, fetch=FetchType.LAZY,cascade=CascadeType.ALL)
@JoinColumn(name="SEASON_ID")
Collection<Game> games = new ArrayList<Game>();
Game
Code:
@ManyToOne(targetEntity=Season.class, fetch=FetchType.LAZY)
Season season;
@OneToMany(targetEntity=GameSet.class, fetch=FetchType.LAZY,cascade=CascadeType.ALL)
@JoinColumn(name="GAME_ID")
Collection<GameSet> gameSets = new ArrayList<Game>();
GameSet
Code:
@ManyToOne(targetEntity=Game.class, fetch=FetchType.LAZY)
Game game;
The I have created a method that creates a Season with a collection of Games and ofc the Games has a collection of GameSets.
In my eyes the relations are the same but when I persist the Season I get the following exception
Code:
Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`pingpong/game_set`, CONSTRAINT `FK9A6C52B5E6AFC091` FOREIGN KEY (`game_ID`) REFERENCES `season` (`ID`))
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1669)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1085)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
Why does the relation between Season and Game work and the one between Game and GameSet not?
It´s probably something very basic that I do wrong. But I really appriciate any suggestions.
Thanks!