我有包含垂钓者列表的 POJO 团队:
@Entity
public class Team {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private int orderNumber;
private String name;
private int rank;
@ManyToOne
private Competition competition;
@OneToMany
private List<Angler> anglers;
//Constructors, getters, setters
}
以及对团队有指针的 POJO Angler:
@Entity
public class Angler {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Integer trackNumber;
private Integer sector;
private Double points;
private String note;
private Integer rankSector;
private Integer totalRank;
@ManyToOne
private Team team;
//construcotrs, getters, setters
AnglerMapper.xml
<resultMap type="rs.necara.anglercompetitionbackend.model.Angler" id="AnglerResultMap">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="trackNumber" column="trackNumber" />
<result property="sector" column="sector" />
<result property="points" column="points" />
<result property="rankSector" column="rankSector" />
<result property="totalRank" column="totalRank" />
<result property="note" column="note" />
<association property="team" resultMap="TeamResultMap"/>
</resultMap>
<resultMap type="rs.necara.anglercompetitionbackend.model.Team" id="TeamResultMap">
<id property="id" column="id" />
<result property="name" column="teamname" />
<result property="rank" column="rank" />
<collection property="anglers" column="{id=teamId}" javaType="ArrayList" ofType="rs.necara.anglercompetitionbackend.model.Angler"
select="rs.necara.anglercompetitionbackend.persistence.mapper.AnglerMapper.getAll"/>
</resultMap>
<select id="getAll" resultMap="AnglerResultMap" >
select * from angler
</select>
TeamMapper.xml
<resultMap type="rs.necara.anglercompetitionbackend.model.Team" id="TeamResultMap">
<id property="id" column="id" />
<result property="title" column="title" />
<result property="orderNumber" column="orderNumber" />
<result property="name" column="name" />
<result property="rank" column="rank" />
<association property="competition" resultMap="CompetitionResultMap"/>
<collection property="anglers" javaType="ArrayList" ofType="rs.necara.anglercompetitionbackend.model.Angler">
<id property="id" column="anglerid" />
<result property="name" column="name" />
<result property="trackNumber" column="trackNumber" />
<result property="sector" column="sector" />
<result property="points" column="points" />
<result property="rankSector" column="rankSector" />
<result property="totalRank" column="totalRank" />
<result property="note" column="note" />
</collection>
</resultMap>
<resultMap type="rs.necara.anglercompetitionbackend.model.Competition" id="CompetitionResultMap">
<id property="id" column="competitionid" />
<result property="title" column="title" />
<result property="finished" column="finished" />
<result property="competitionDate" column="competitionDate" />
<result property="type" column="type" />
</resultMap>
当我想检索所有钓鱼者时,我得到了Could not write JSON: Infinite recursion (StackOverflowError)。结果是这样的:
[
{
"id": 7,
"name": "t1",
"trackNumber": null,
"sector": null,
"points": null,
"note": null,
"rankSector": null,
"totalRank": null,
"team": {
"id": 7,
"orderNumber": 0,
"name": null,
"rank": 0,
"competition": null,
"anglers": [
{
"id": 7,
"name": "t1",
"trackNumber": null,
"sector": null,
"points": null,
"note": null,
"rankSector": null,
"totalRank": null,
"team": {
"id": 7,
"orderNumber": 0,
"name": null,
"rank": 0,
"competition": null,
"anglers": [
{...
所以,钓鱼者指向团队,而不是团队指向钓鱼者等等,我很清楚,但是我可以避免这种映射方式吗?什么是解决方案,最佳实践?