JPA foreign key is null in the child table

百般思念 提交于 2019-12-24 18:40:18

问题


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)

回答1:


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.



来源:https://stackoverflow.com/questions/37114984/jpa-foreign-key-is-null-in-the-child-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!