1

I have below classes i am trying to get onetomany relationship using JPA. When the xml message sent to restcontroller, the message is stored both parent and child table but foreign key is always null. Please let me know if any issue.

Rest Service which i am using to persist the data and the below is the sample xml which i am posting to this rest method.

@RequestMapping(value="/team/", method = RequestMethod.POST , headers="Accept=application/xml")
@ResponseStatus(value = HttpStatus.OK)
public void createTeam(@RequestBody Team team) throws Exception {
    teamService.createTeam(team);
}

Child class

   @XmlRootElement
    @Entity
    public class Player {

        @Id
        @GeneratedValue
        @Column(name = "player_id")
        private long player_id;

        @Column(unique=true , nullable = false)
        private String name;

        @ManyToOne
        @JoinColumn(name = "team_id")
        private Team team;

and setter and getters....
}

and my parent class

   @XmlRootElement
    @Entity(name ="team")
    public class Team {

        @Id
        @GeneratedValue
        @Column(name = "team_id")
        private long team_id;

        @Column(unique=true , nullable = false)
        private String name;

        @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "team")
        private List<Player> player;

and setter and getter
}

@Service
@Transactional
public class TeamServiceImpl implements TeamService{

protected EntityManager entityManager;

    public EntityManager getEntityManager() {
        return entityManager;
    }

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    public List<Team> getTeam() {
         Query query = getEntityManager().createQuery("select a from team a");
          List<Team> resultList = query.getResultList();
          return resultList;
    }

    @Override
    public void createTeam(Team team) {
        getEntityManager().persist(team);
    }

}



@Service
@Transactional
public class PlayerServiceImpl implements PlayerService {

    protected EntityManager entityManager;

    public EntityManager getEntityManager() {
        return entityManager;
    }

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }


    @Override
    public List<Player> getPlayer() {
         Query query = getEntityManager().createQuery("select a from Player a");
          List<Player> resultList = query.getResultList();
          return resultList;
    }

    @Override
    public void createPlayer(Player player) {
        getEntityManager().persist(player);
    }

}

and my xml

<team>
<name>CSK</name>
<Players>
<player>
    <name>kholi</name> 
</player>
<player>
    <name>king</name> 
</player>
<player>
    <name>raja</name> 
</player>
</Players>
</team>

Below is the result i am seeing in the table after creating entry in team. Please note team_id field in player table is null. this is my issue. Need to know how i can fix it so that this field will get team_id value.

mysql> select * from team;
+---------+-------+
| team_id | name  |
+---------+-------+
|       1 | Delhi |
+---------+-------+
1 row in set (0.00 sec)

mysql> select * from player;
+-----------+--------+---------+
| player_id | name   | team_id |
+-----------+--------+---------+
|         1 | kholi1 |    NULL |
|         2 | king   |    NULL |
|         3 | raja   |    NULL |
|         4 | kholi  |    NULL |
|         5 | a      |    NULL |
|         6 | b      |    NULL |
|         7 | c      |    NULL |
|         8 | d      |    NULL |
|         9 | e      |    NULL |
|        10 | f      |    NULL |
|        11 | g      |    NULL |
|        12 | h      |    NULL |
|        13 | i      |    NULL |
|        14 | j      |    NULL |
|        15 | k      |    NULL |
|        16 | l      |    NULL |
|        17 | m      |    NULL |
|        18 | n      |    NULL |
|        20 | sdsdsd |    NULL |
+-----------+--------+---------+
19 rows in set (0.00 sec)
mike
  • 377
  • 1
  • 9
  • 22
  • XML is nothing to do with JPA. Where is your code to retrieve objects using JPA, and where have you checked that you have retrieved all fields that you need? This is a process called "debugging" – Neil Stockton May 09 '16 at 12:30
  • where is your JPA code to PERSIST the objects? you did set BOTH SIDES of the BIDIRECTIONAL relation? and what is the SQL invoked when doing that (in the JPA provider log). – Neil Stockton May 09 '16 at 12:49
  • updated with restcontroller which i am using to persist the data – mike May 10 '16 at 05:33
  • As I said earlier "you did set BOTH SIDES of the BIDIRECTIONAL relation? " Posting some REST controller is doing nothing to DEBUG your problem. Why not print out what fields are set?! Then you can isolate it to REST or JPA ... – Neil Stockton May 10 '16 at 06:09

1 Answers1

4

Looks like you've saved players, but didn't set Team for them. You should perform like this:

for (Player player: team.getPlayers())
    player.setTeam(team);

In another way only one property for player is set - its name. JPA is unable to automagically detect that if the player is in players collection than it is in the team.

asm0dey
  • 2,841
  • 2
  • 20
  • 33
  • updated the rest controller which i am using to persist the data – mike May 10 '16 at 05:33
  • 1
    As I said earlier: you don't set reference to team from players. On each player you should call player.setTeam. Updated my answer – asm0dey May 10 '16 at 05:42