我有以下课程,我试图使用JPA获得onetomany关系。当xml消息发送到restcontroller时,消息存储在父表和子表中,但外键始终为null。如果有任何问题,请告诉我。
我用来保存数据的Rest服务,下面是我发布到这个rest方法的示例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)
答案 0 :(得分:2)
看起来你已经保存了球员,但没有为他们设置球队。 你应该这样做:
for (Player player: team.getPlayers())
player.setTeam(team);
另一方面,只设置了一个玩家属性 - 它的名字。如果玩家在玩家收藏中而不是在团队中,则JPA无法自动检测到。