-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Relations (OneToMany and ManyToOne) that dont cooporate
PostPosted: Fri Mar 06, 2009 5:11 pm 
Newbie

Joined: Fri Mar 06, 2009 4:30 pm
Posts: 3
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!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 09, 2009 7:57 am 
Beginner
Beginner

Joined: Tue Nov 27, 2007 9:44 am
Posts: 46
Hi,

would you be able to post a bit more of your code?

Thanks,
Frank


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 09, 2009 9:22 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Try to add the 'mappedBy' attribute to your @OneToMany collections. For example, in your Season class: @OneToMany(mappedBy="season" ....)

For more examples, read http://www.hibernate.org/hib_docs/annot ... ollections


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 09, 2009 4:25 pm 
Newbie

Joined: Fri Mar 06, 2009 4:30 pm
Posts: 3
EDIT: Got it to work! Found a FK in the db that was wrong. Had been lazy and autogenerated the tables. Inputs are still welcome though. I want to improve!


Hi,

thanks for your responses. I´ve tried the mappedBy before, but tried it again now. The result is that all the entities are stored in the database but the Game doesn´t have any Season id and the GameSets doesn´t have any Game id.

The thing I think is strange is that in the code-snippets I posted before is that the first relation (between Season and Game) work perfect but the other one does not.


Here is a simply modified code that creates a Season
Code:

   public Season createSeason(Contestant c, Contestant c2, Integer nbrGamesVsContestant, Integer setsPerGame,String name){
      
      Season season = new Season();
      //create a new list of Games
      ArrayList<Game> games = new ArrayList<Game>();
      
      Game game = new Game();
      game.setContestantOne(c);
      game.setContestantTwo(c2);
      game.setNbrOfSets(setsPerGame);
      
      //create a new list of GameSets
      ArrayList<GameSet> gameSets = new ArrayList<GameSet>();
      GameSet gs = new GameSet(0 , 0);
      
      //Add the the created GameSet to the list
      gameSets.add(gs);
      //Set the list of GameSet to the game
      game.setGameSets(gameSets);
      
      //add the game to the list of games
      games.add(game);
      
      //Set games and the rest of the values to season
      season.setGames(games);
      season.setGamesVsContestant(nbrGamesVsContestant);
      season.setName(name);
      season.setSetsPerGame(setsPerGame);

      return season;
   }


And then the code that persists the entities to the db (A Struts 2 action)

Code:
   public String execute(){

      SeasonCreator sc = new SeasonCreator();
      EntityManager em = ConnectDB.getEntityManager();
      try{
         em.getTransaction().begin();
         
         Contestant c = new Contestant();
         c.setFirstName("John");
         c.setLastName("Doe");
         
         Contestant c2 = new Contestant();
         c2.setFirstName("Julia");
         c2.setLastName("Doe");
         
         Season season = sc.createSeason(c, c2, 1, 1, "My first season");
         
         em.persist(season);

         em.getTransaction().commit();
      }catch (Exception e) {
         e.printStackTrace();
         em.getTransaction().rollback();
      }finally{
         if(em.isOpen()){
            em.close();
         }
      }
      
      return SUCCESS;
   }


Is it not possible to store just the season?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 09, 2009 5:08 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
I´ve tried the mappedBy before, but tried it again now. The result is that all the entities are stored in the database but the Game doesn´t have any Season id and the GameSets doesn´t have any Game id.


This is probably because you are not setting both ends of the association. You need to do both:

Code:
game.setSeason(season);
season.getGames().add(game);


For more information about how to handle parent/child relationship you can have a look at the Hibernate documentation: http://www.hibernate.org/hib_docs/v3/re ... bidir.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 4:23 am 
Newbie

Joined: Fri Mar 06, 2009 4:30 pm
Posts: 3
Yes, I think you are right.

Earlier in my code I hade a constructor for GameSet that took Game as a parameter. But I tried loads of things in my search for a solution, I took it away and didn´t remember to change it back.

Thanks again!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.