9

I'm trying to persist a List inside an object Team. I cannot get JPA to persist it for me. After having read some posts, it looks like you are required to iterate thru the List<>, and persist each one individually. This just doesn't seem right to me. There must be way for JPA to persist that List's auto-magically.

Am I missing an easy JPA-way of deeply persisting these objects?

Team class:

@Entity
@Table(name = "rahCLUB_TEAM")
public class Team implements Serializable{
....
@OneToMany(fetch=FetchType.EAGER, mappedBy = "team", cascade={CascadeType.PERSIST,  CascadeType.REMOVE})
    private List<Player> players;
...
}

Player class:

@Entity
@PrimaryKeyJoinColumn(name="id")
@Table(name = "rahCLUB_PLAYER")

public class Player extends Individual implements Serializable{
 ...
   @ManyToOne
   @JoinTable(name="rahCLUB_PLAYER_TEAM_LINK")
   private Team team;
 ...
 }

JPAClubDAOTest:

//create a valid team code
 ...
 Team team = new Team();
 team.setName("Pittsburgh Pirates");
 team.setLeague("NFL");
 team.setGroup("group");
 team.setLevel("AAA");

 Player player = new Player();
 player.setFirstName("firstNameA");
 player.setLastName("lastNameA");
 player.setEmail("emaila@email.com");
 player.setLogin("loginA");
 player.setJerseyNo(22);
 player.setPosition("fullback");
 Date dob  =new SimpleDateFormat("dd/MM/yyyy").parse("21/11/1986");
 player.setDob(dob);

 Player playerb = new Player();
 playerb.setFirstName("firstNameB");
 playerb.setLastName("lastNameB");
 playerb.setEmail("emailB@email.com");
 playerb.setLogin("loginB");
 playerb.setJerseyNo(15);
 playerb.setPosition("widereceiver");
 Date dobb  =new SimpleDateFormat("dd/MM/yyyy").parse("06/06/1986");
 playerb.setDob(dobb);

 ArrayList<Player> players = new ArrayList<Player>();
 players.add(player);
 players.add(playerb);

 team.setPlayers(players);

 Team queryTeam = em.find(Team.class, team.getId());
 log.debug("checking coach...");

These all fail:

assertTrue(queryTeam.getPlayers().size()>0);
assertNotNull(queryTeam.getPlayers().get(0));
assertTrue(queryTeam.getPlayers().size()==2);

Code to add this:

@Override
public void createTeam(Team team) throws ClubDAOException {
em.persist(team);
for (Player player : team.getPlayers())
{
   this.createPlayer(player);
   //em.persist(player);
}
technocrat
  • 3,513
  • 5
  • 25
  • 39
  • Where is the code adding players to the team? Read this: it's the third time **just today** I answer this question: http://stackoverflow.com/questions/19896025/understanding-annotations-and-jpahibernate/19896470#19896470 – JB Nizet Nov 11 '13 at 21:11
  • Read what your code does. It doesn't create any player. It iterates through the empty list of players of your new team. At some time, you need to have `new Player()` in your code to create a Player. – JB Nizet Nov 11 '13 at 21:20
  • When I pass in the object Team, it's got an already existing List of Players. I call `new Player()` in the area I stubbed out.. I'll copy paste in some more code. – technocrat Nov 11 '13 at 21:33
  • OK. So voting to close because it's a duplicate of http://stackoverflow.com/questions/19896025/understanding-annotations-and-jpahibernate/19896470#19896470. You have added players to the team, but haven't set the team of the players. – JB Nizet Nov 11 '13 at 21:38
  • Agree to close.. though can we combine some of this? The title "How to persist Collections/List inside an object using JPA" is far more descriptive than "Understanding annotations and JPA(hibernate)" – technocrat Nov 11 '13 at 21:45
  • I must have answered this question 600 times already, and inevitably, this question comes back again and again, whatever the title or text of the question is. – JB Nizet Nov 11 '13 at 21:48

2 Answers2

3

Well, I'm going to answer my own question because I'd like to see one here...

Kudos to JB Nizet to solving this for me/pointing me to the right post, here: Understanding annotations and JPA(hibernate)

The key is that the inverse relationship must be set. In the example above, to fix it:

player.setTeam(team);
playerb.setTeam(team);

Once both sides of the relationship are set, then it's persisted to the database.

Community
  • 1
  • 1
technocrat
  • 3,513
  • 5
  • 25
  • 39
3

You can use another approach:

Unidirectional OneToMany: http://en.wikibooks.org/wiki/Java_Persistence/OneToMany#Unidirectional_OneToMany.2C_No_Inverse_ManyToOne.2C_No_Join_Table_.28JPA_2.0_ONLY.29

You still need to persist the Object and then add it into your Collection.

r1ckr
  • 5,643
  • 5
  • 20
  • 24