Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to persist Collections/List inside an object using JPA

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("[email protected]");
 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("[email protected]");
 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);
}
like image 445
technocrat Avatar asked Nov 11 '13 20:11

technocrat


1 Answers

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.

like image 64
technocrat Avatar answered Oct 12 '22 00:10

technocrat