JAXB解组类具有两个相同对象的实例

时间:2016-07-25 11:47:33

标签: java xml jaxb

我有两个班级:

public class Game {

protected int Id;
protected int gameNumber;
protected int roundId;
protected Player playerOne;
protected Player playerTwo;
protected int playerOneScore;
protected int playerTwoScore;
protected Round round;

@XmlIDREF
@XmlElement(name = "playerOne")
public Player getPlayerOne() {
    return playerOne;
}

public void setPlayerOne(Player playerOne) {
    this.playerOne = playerOne;
}
}

public class Player {

protected int Id;
protected String userName;
protected String eMail;
protected int points;
protected String firstName;
protected String lastName;
protected List<Game> games;    


@XmlElementWrapper(name = "Games")
@XmlElement(name = "Game")
public List<Game> getGames() {
    return games;
}

public void setGames(List<Game> games) {
    this.games = games;
}

}

使用JAXB如何将游戏中的玩家设置为玩家并将游戏中的两个对象设置为正确的对象引用?

当我将XML导入我的程序时,它不会将玩家添加到正确的游戏中。

我尝试使用

 public void afterUnmarshal(Unmarshaller u, Object parent) {
    if (parent instanceof Player) {
        this.playerOne = (Player) parent;
    }

}

但问题是有两个对象。

有什么建议吗?

0 个答案:

没有答案