【发布时间】:2016-09-04 01:00:26
【问题描述】:
我有以下课程,我正在尝试使用 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>
以下是我在团队中创建条目后在表格中看到的结果。请注意 player 表中的 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)
【问题讨论】:
-
XML 与 JPA 无关。您使用 JPA 检索对象的代码在哪里,您在哪里检查是否已检索到所需的所有字段?这是一个叫做“调试”的过程
-
你的 JPA 代码在哪里持久化对象?你确实设置了双向关系的两侧?以及执行此操作时调用的 SQL 是什么(在 JPA 提供程序日志中)。
-
更新了我用来持久化数据的restcontroller
-
正如我之前所说的“你确实设置了 BIDIRECTIONAL 关系的两边?”发布一些 REST 控制器对调试你的问题没有任何帮助。为什么不打印出设置了哪些字段?!然后您可以将其隔离到 REST 或 JPA ...
标签: spring hibernate jpa spring-rest spring-mvc-test