JPA外键是在子表空(JPA foreign key is null in the child ta

2019-09-28 17:56发布

我有以下类我试图使用JPA获得一对多的关系。 当发送的XML消息restcontroller,该消息同时存储父和子表,但是外键始终为空。 请让我知道,如果任何问题。

其中我使用来持久数据和下面其余服务是我张贴到该静止方法的示例XML。

@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);
}

儿童类

   @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....
}

和我的父类

   @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);
    }

}

和我的XML

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

下面是建立在团队入境后的结果我在表中看到。 请注意,在播放表TEAM_ID字段为空。 这是我的问题。 需要知道我可以修复它,使这一领域将得到TEAM_ID值。

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)

Answer 1:

看起来你已经保存的球员,但并没有为它们设置团队。 您应该执行这样的:

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

在另一种方法只有一个播放器属性设置 - 它的名字。 JPA是无法自动地检测,如果玩家在玩家收集比在球队。



文章来源: JPA foreign key is null in the child table