child table 中的 JPA 外键为空
JPA foreign key is null in the child table
我有以下 classes 我正在尝试使用 JPA 建立一对一关系。当 xml 消息发送到 restcontroller 时,消息会同时存储 parent 和 child table 但外键始终为空。如果有任何问题,请告诉我。
我用来保存数据的休息服务,下面是我发布到这个休息方法的示例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);
}
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....
}
和我的parentclass
@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>
下面是在团队中创建条目后我在 table 中看到的结果。请注意播放器 table 中的 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)
看起来你已经保存了玩家,但没有为他们设置团队。
你应该这样表演:
for (Player player: team.getPlayers())
player.setTeam(team);
以另一种方式,只为玩家设置了一个 属性 - 它的名字。 JPA 无法自动检测玩家是否在玩家集合中而不是在团队中。
我有以下 classes 我正在尝试使用 JPA 建立一对一关系。当 xml 消息发送到 restcontroller 时,消息会同时存储 parent 和 child table 但外键始终为空。如果有任何问题,请告诉我。
我用来保存数据的休息服务,下面是我发布到这个休息方法的示例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);
}
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....
}
和我的parentclass
@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>
下面是在团队中创建条目后我在 table 中看到的结果。请注意播放器 table 中的 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)
看起来你已经保存了玩家,但没有为他们设置团队。 你应该这样表演:
for (Player player: team.getPlayers())
player.setTeam(team);
以另一种方式,只为玩家设置了一个 属性 - 它的名字。 JPA 无法自动检测玩家是否在玩家集合中而不是在团队中。