Hibernate使用Spring REST将对象附加到错误的实体

时间:2017-11-07 16:03:12

标签: json spring hibernate rest spring-data

所以我试图在Spring使用一些Seed Data将一些信息保存到我的数据库中。我通过List保存并在列表对象上使用调试器,然后通过我的服务保存方法发送它,它正确地返回。在我发送之后,我的一个对象“Front Office”被错误地映射。但其他一切都正确映射。

这是我的种子数据:

    public void generateData()
{

    //region RECORDS
    Record colts17 = new Record();
    colts17.setLosses(6);
    colts17.setWins(3);
    colts17.setSeasonSpan("2017-2018");

    Record colts16 = new Record();
    colts16.setLosses(8);
    colts16.setWins(8);
    colts16.setSeasonSpan("2016-2017");

    List<Record> coltsRecord = new ArrayList<>();
    coltsRecord.add(colts16);
    coltsRecord.add(colts17);

    Record vikings17 = new Record();
    vikings17.setLosses(2);
    vikings17.setWins(6);
    vikings17.setSeasonSpan("2017-2018");

    List<Record> vikingsRecord = new ArrayList<>();
    vikingsRecord.add(vikings17);
    //endregion

    //region FRONT OFFICES
    FrontOffice coltsOffice = new FrontOffice();
    coltsOffice.setGeneralManager("Chris Ballard");
    coltsOffice.setHeadCoach("Chuck Pagano");
    coltsOffice.setOwner("Jim Irsay");

    FrontOffice vikingsOffice = new FrontOffice();
    vikingsOffice.setHeadCoach("Mike Zimmer");
    vikingsOffice.setOwner("Zygi Wilf");
    vikingsOffice.setGeneralManager("Rick Spielman");

    frontOfficeService.saveFrontOffice(coltsOffice);
    frontOfficeService.saveFrontOffice(vikingsOffice);


    //endregionS

    //region PLAYERS

    Player andrewLuck = new Player();
    andrewLuck.setFirstName("Andrew");
    andrewLuck.setLastName("Luck");
    andrewLuck.setPosition("QB");
    andrewLuck.setJerseyNumber(12);
    andrewLuck.setRookieSeason(2012);
    andrewLuck.setLastSeason(2017);
    andrewLuck.setStatus("Active");

    Player malikHooker = new Player();
    malikHooker.setFirstName("Malik");
    malikHooker.setLastName("Hooker");
    malikHooker.setPosition("FS");
    malikHooker.setJerseyNumber(29);
    malikHooker.setRookieSeason(2017);
    malikHooker.setLastSeason(2017);
    malikHooker.setStatus("Active");

    List<Player> coltsPlayers = new ArrayList<>();
    coltsPlayers.add(andrewLuck);
    coltsPlayers.add(malikHooker);

    Player harrisonSmith = new Player();
    harrisonSmith.setFirstName("Harrison");
    harrisonSmith.setLastName("Smith");
    harrisonSmith.setPosition("FS");
    harrisonSmith.setJerseyNumber(22);
    harrisonSmith.setRookieSeason(2012);
    harrisonSmith.setLastSeason(2017);
    harrisonSmith.setStatus("Active");

    Player dalvinCook = new Player();
    dalvinCook.setFirstName("Dalvin");
    dalvinCook.setLastName("Cook");
    dalvinCook.setPosition("RB");
    dalvinCook.setJerseyNumber(33);
    dalvinCook.setRookieSeason(2012);
    dalvinCook.setLastSeason(2012);
    dalvinCook.setStatus("Active");

    List<Player> vikingsPlayers = new ArrayList<>();
    vikingsPlayers.add(harrisonSmith);
    vikingsPlayers.add(dalvinCook);

    //endregion

    //region TEAMS
    Team colts = new Team();
    colts.setHomeTown("Indianapolis");
    colts.setTeamName("Colts");
    colts.setFrontOffice(coltsOffice);
    colts.setPlayerList(coltsPlayers);
    colts.setRecordList(coltsRecord);
    teamService.saveTeam(colts);

    Team cardinals = new Team();
    cardinals.setHomeTown("Arizona");
    cardinals.setTeamName("Cardinals");
    teamService.saveTeam(cardinals);

    Team falcons = new Team();
    falcons.setHomeTown("Atlanta");
    falcons.setTeamName("Falcons");
    teamService.saveTeam(falcons);

    Team ravens = new Team();
    ravens.setHomeTown("Baltimore");
    ravens.setTeamName("Ravens");
    teamService.saveTeam(ravens);

    Team bills = new Team();
    bills.setHomeTown("Buffalo");
    bills.setTeamName("Bills");
    teamService.saveTeam(bills);

    Team panthers = new Team();
    panthers.setHomeTown("Carolina");
    panthers.setTeamName("Panthers");
    teamService.saveTeam(panthers);

    Team bears = new Team();
    bears.setHomeTown("Chicago");
    bears.setTeamName("Bears");
    teamService.saveTeam(bears);

    Team bengals = new Team();
    bengals.setHomeTown("Cincinnati");
    bengals.setTeamName("Bengals");
    teamService.saveTeam(bengals);

    Team browns = new Team();
    browns.setHomeTown("Cleveland");
    browns.setTeamName("Browns");
    teamService.saveTeam(browns);

    Team cowboys = new Team();
    cowboys.setHomeTown("Dallas");
    cowboys.setTeamName("Cowboys");
    teamService.saveTeam(cowboys);

    Team broncos = new Team();
    broncos.setHomeTown("Denver");
    broncos.setTeamName("Broncos");
    teamService.saveTeam(broncos);

    Team lions = new Team();
    lions.setHomeTown("Detroit");
    lions.setTeamName("Lions");
    teamService.saveTeam(lions);

    Team packers = new Team();
    packers.setHomeTown("Green Bay");
    packers.setTeamName("Packers");
    teamService.saveTeam(packers);

    Team texans = new Team();
    texans.setHomeTown("Houston");
    texans.setTeamName("Texans");
    teamService.saveTeam(texans);

    Team jaguars = new Team();
    jaguars.setHomeTown("Jacksonville");
    jaguars.setTeamName("Jaguars");
    teamService.saveTeam(jaguars);

    Team chiefs = new Team();
    chiefs.setHomeTown("Kansas City");
    chiefs.setTeamName("Chiefs");
    teamService.saveTeam(chiefs);

    Team chargers = new Team();
    chargers.setHomeTown("Los Angeles");
    chargers.setTeamName("Chargers");
    teamService.saveTeam(chargers);

    Team rams = new Team();
    rams.setHomeTown("Los Angeles");
    rams.setTeamName("Rams");
    teamService.saveTeam(rams);

    Team dolphins = new Team();
    dolphins.setHomeTown("Miami");
    dolphins.setTeamName("Dolphins");
    teamService.saveTeam(dolphins);

    Team vikings = new Team();
    vikings.setHomeTown("Minnesota");
    vikings.setTeamName("Vikings");
    vikings.setRecordList(vikingsRecord);
    vikings.setPlayerList(vikingsPlayers);
    vikings.setFrontOffice(vikingsOffice);
    teamService.saveTeam(vikings);

    Team patriots = new Team();
    patriots.setHomeTown("New England");
    patriots.setTeamName("Patriots");
    teamService.saveTeam(patriots);

    Team saints = new Team();
    saints.setHomeTown("New Orleans");
    saints.setTeamName("Saints");
    teamService.saveTeam(saints);

    Team giants = new Team();
    giants.setHomeTown("New York");
    giants.setTeamName("Giants");
    teamService.saveTeam(giants);

    Team jets = new Team();
    jets.setHomeTown("New York");
    jets.setTeamName("Jets");
    teamService.saveTeam(jets);

    Team raiders = new Team();
    raiders.setHomeTown("Oakland");
    raiders.setTeamName("Raiders");
    teamService.saveTeam(raiders);

    Team eagles = new Team();
    eagles.setHomeTown("Philadelphia");
    eagles.setTeamName("Eagles");
    teamService.saveTeam(eagles);

    Team steelers = new Team();
    steelers.setHomeTown("Pittsburgh");
    steelers.setTeamName("Steelers");
    teamService.saveTeam(steelers);

    Team fortyNiners = new Team();
    fortyNiners.setHomeTown("San Francisco");
    fortyNiners.setTeamName("49ers");
    teamService.saveTeam(fortyNiners);

    Team seahawks = new Team();
    seahawks.setHomeTown("Seattle");
    seahawks.setTeamName("Seahawks");
    teamService.saveTeam(seahawks);

    Team buccaneers = new Team();
    buccaneers.setHomeTown("Tampa Bay");
    buccaneers.setTeamName("Buccaneers");
    teamService.saveTeam(buccaneers);

    Team titans = new Team();
    titans.setHomeTown("Tennessee");
    titans.setTeamName("Titans");
    teamService.saveTeam(titans);

    Team redskins = new Team();
    redskins.setHomeTown("Washington");
    redskins.setTeamName("Redskins");
    teamService.saveTeam(redskins);

    //endregion

    //region DIVISIONS

    List<Team> afcSouthList = new ArrayList<>();
    afcSouthList.add(colts);
    afcSouthList.add(titans);
    afcSouthList.add(texans);
    afcSouthList.add(jaguars);

    List<Team> afcEastList = new ArrayList<>();
    afcEastList.add(patriots);
    afcEastList.add(jets);
    afcEastList.add(bills);
    afcEastList.add(dolphins);

    List<Team> afcNorthList = new ArrayList<>();
    afcNorthList.add(steelers);
    afcNorthList.add(ravens);
    afcNorthList.add(bengals);
    afcNorthList.add(browns);

    List<Team> afcWestList = new ArrayList<>();
    afcWestList.add(chiefs);
    afcWestList.add(raiders);
    afcWestList.add(chargers);
    afcWestList.add(broncos);

    List<Team> nfcNorthList = new ArrayList<>();
    nfcNorthList.add(vikings);
    nfcNorthList.add(packers);
    nfcNorthList.add(lions);
    nfcNorthList.add(bears);

    List<Team> nfcSouthList = new ArrayList<>();
    nfcSouthList.add(saints);
    nfcSouthList.add(panthers);
    nfcSouthList.add(falcons);
    nfcSouthList.add(buccaneers);

    List<Team> nfcEastList = new ArrayList<>();
    nfcEastList.add(eagles);
    nfcEastList.add(cowboys);
    nfcEastList.add(giants);
    nfcEastList.add(redskins);

    List<Team> nfcWestList = new ArrayList<>();
    nfcWestList.add(rams);
    nfcWestList.add(seahawks);
    nfcWestList.add(cardinals);
    nfcWestList.add(fortyNiners);


    Division afcSouth = new Division();
    afcSouth.setTeamList(afcSouthList);

    Division afcEast = new Division();
    afcEast.setTeamList(afcEastList);

    Division afcNorth = new Division();
    afcNorth.setTeamList(afcNorthList);

    Division afcWest = new Division();
    afcWest.setTeamList(afcWestList);

    List<Division> afcDivisions = new ArrayList<>();
    afcDivisions.add(afcEast);
    afcDivisions.add(afcNorth);
    afcDivisions.add(afcSouth);
    afcDivisions.add(afcWest);

    Division nfcSouth = new Division();
    nfcSouth.setTeamList(nfcSouthList);

    Division nfcEast = new Division();
    nfcEast.setTeamList(nfcEastList);

    Division nfcNorth = new Division();
    nfcNorth.setTeamList(nfcNorthList);

    Division nfcWest = new Division();
    nfcWest.setTeamList(nfcWestList);

    List<Division> nfcDivisions = new ArrayList<>();
    nfcDivisions.add(nfcEast);
    nfcDivisions.add(nfcNorth);
    nfcDivisions.add(nfcSouth);
    nfcDivisions.add(nfcWest);

    divisionService.saveDivisionList(afcDivisions);
    divisionService.saveDivisionList(nfcDivisions);


    //endregion

    //region CONFERENCES
    Conference afc = new Conference();
    afc.setConferenceName("AFC");
    afc.setDivisionList(afcDivisions);


    Conference nfc = new Conference();
    nfc.setConferenceName("NFC");
    nfc.setDivisionList(nfcDivisions);

    List<Conference> conferenceList = new ArrayList<>();
    conferenceList.add(afc);
    conferenceList.add(nfc);

    conferenceService.saveConferenceList(conferenceList);
    //endregion
}

这是我的Service Imp,因为您看不到我的保存方法的自定义逻辑:

@Service
public class FrontOfficeImpl implements FrontOfficeService
{
private final FrontOfficeRepository frontOfficeRepository;

public FrontOfficeImpl(FrontOfficeRepository frontOfficeRepository)
{
    this.frontOfficeRepository = frontOfficeRepository;
}

@Override
public FrontOffice getFrontOfficeById(Integer id) {
    return frontOfficeRepository.findOne(id);
}

@Override
public FrontOffice saveFrontOffice(FrontOffice frontOffice) {
    return frontOfficeRepository.save(frontOffice);
}

@Override
public Iterable<FrontOffice> listAllFrontOffices() {
    return frontOfficeRepository.findAll();
}

@Override
public Iterable<FrontOffice> saveFrontOfficeList(Iterable<FrontOffice> frontOffices) {
    return frontOfficeRepository.save(frontOffices);
}

@Override
public boolean deleteFrontOffice(Integer id) {
    try
    {
        frontOfficeRepository.delete(id);
        return true;
    }
    catch (Exception e)
    {
        return false;
    }
}

以下是包含错误保存对象的实体:

public class Team
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "TeamId")
private Integer id;

@Version
private Integer version;

private String teamName;

private String homeTown;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "TeamId")
private List<Record> recordList;

@OneToOne(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
@JoinColumn(name = "TeamId")
private FrontOffice frontOffice;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "TeamId")
private List<Player> playerList;

最后我在邮递员队伍中为团队返回JSON:

[
{
    "id": 1,
    "version": 0,
    "teamName": "Colts",
    "homeTown": "Indianapolis",
    "recordList": [
        {
            "id": 1,
            "version": 0,
            "wins": 8,
            "losses": 8,
            "ties": 0,
            "seasonSpan": "2016-2017"
        },
        {
            "id": 2,
            "version": 0,
            "wins": 3,
            "losses": 6,
            "ties": 0,
            "seasonSpan": "2017-2018"
        }
    ],
    "frontOffice": {
        "id": 1,
        "version": 0,
        "owner": "Jim Irsay",
        "generalManager": "Chris Ballard",
        "headCoach": "Chuck Pagano"
    },
    "playerList": [
        {
            "id": 1,
            "version": 0,
            "firstName": "Andrew",
            "lastName": "Luck",
            "position": "QB",
            "rookieSeason": 2012,
            "lastSeason": 2017,
            "jerseyNumber": 12,
            "status": "Active"
        },
        {
            "id": 2,
            "version": 0,
            "firstName": "Malik",
            "lastName": "Hooker",
            "position": "FS",
            "rookieSeason": 2017,
            "lastSeason": 2017,
            "jerseyNumber": 29,
            "status": "Active"
        }
    ]
},
{
    "id": 2,
    "version": 0,
    "teamName": "Cardinals",
    "homeTown": "Arizona",
    "recordList": [],
    "frontOffice": {
        "id": 2,
        "version": 0,
        "owner": "Zygi Wilf",
        "generalManager": "Rick Spielman",
        "headCoach": "Mike Zimmer"
    },
    "playerList": []
},
{
    "id": 3,
    "version": 0,
    "teamName": "Falcons",
    "homeTown": "Atlanta",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 4,
    "version": 0,
    "teamName": "Ravens",
    "homeTown": "Baltimore",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 5,
    "version": 0,
    "teamName": "Bills",
    "homeTown": "Buffalo",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 6,
    "version": 0,
    "teamName": "Panthers",
    "homeTown": "Carolina",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 7,
    "version": 0,
    "teamName": "Bears",
    "homeTown": "Chicago",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 8,
    "version": 0,
    "teamName": "Bengals",
    "homeTown": "Cincinnati",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 9,
    "version": 0,
    "teamName": "Browns",
    "homeTown": "Cleveland",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 10,
    "version": 0,
    "teamName": "Cowboys",
    "homeTown": "Dallas",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 11,
    "version": 0,
    "teamName": "Broncos",
    "homeTown": "Denver",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 12,
    "version": 0,
    "teamName": "Lions",
    "homeTown": "Detroit",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 13,
    "version": 0,
    "teamName": "Packers",
    "homeTown": "Green Bay",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 14,
    "version": 0,
    "teamName": "Texans",
    "homeTown": "Houston",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 15,
    "version": 0,
    "teamName": "Jaguars",
    "homeTown": "Jacksonville",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 16,
    "version": 0,
    "teamName": "Chiefs",
    "homeTown": "Kansas City",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 17,
    "version": 0,
    "teamName": "Chargers",
    "homeTown": "Los Angeles",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 18,
    "version": 0,
    "teamName": "Rams",
    "homeTown": "Los Angeles",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 19,
    "version": 0,
    "teamName": "Dolphins",
    "homeTown": "Miami",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 20,
    "version": 0,
    "teamName": "Vikings",
    "homeTown": "Minnesota",
    "recordList": [
        {
            "id": 3,
            "version": 0,
            "wins": 6,
            "losses": 2,
            "ties": 0,
            "seasonSpan": "2017-2018"
        }
    ],
    "frontOffice": null,
    "playerList": [
        {
            "id": 3,
            "version": 0,
            "firstName": "Harrison",
            "lastName": "Smith",
            "position": "FS",
            "rookieSeason": 2012,
            "lastSeason": 2017,
            "jerseyNumber": 22,
            "status": "Active"
        },
        {
            "id": 4,
            "version": 0,
            "firstName": "Dalvin",
            "lastName": "Cook",
            "position": "RB",
            "rookieSeason": 2012,
            "lastSeason": 2012,
            "jerseyNumber": 33,
            "status": "Active"
        }
    ]
},
{
    "id": 21,
    "version": 0,
    "teamName": "Patriots",
    "homeTown": "New England",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 22,
    "version": 0,
    "teamName": "Saints",
    "homeTown": "New Orleans",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 23,
    "version": 0,
    "teamName": "Giants",
    "homeTown": "New York",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 24,
    "version": 0,
    "teamName": "Jets",
    "homeTown": "New York",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 25,
    "version": 0,
    "teamName": "Raiders",
    "homeTown": "Oakland",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 26,
    "version": 0,
    "teamName": "Eagles",
    "homeTown": "Philadelphia",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 27,
    "version": 0,
    "teamName": "Steelers",
    "homeTown": "Pittsburgh",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 28,
    "version": 0,
    "teamName": "49ers",
    "homeTown": "San Francisco",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 29,
    "version": 0,
    "teamName": "Seahawks",
    "homeTown": "Seattle",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 30,
    "version": 0,
    "teamName": "Buccaneers",
    "homeTown": "Tampa Bay",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 31,
    "version": 0,
    "teamName": "Titans",
    "homeTown": "Tennessee",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
},
{
    "id": 32,
    "version": 0,
    "teamName": "Redskins",
    "homeTown": "Washington",
    "recordList": [],
    "frontOffice": null,
    "playerList": []
}
]

正如您所看到的,由于某种原因,frontOffice对象只是被持久化到前两个团队对象。我已经尝试搞乱一些持久性设置,并通过会议进行了所有的保存,这比团队更高级别。我知道它会在前两个,因为小马队不是第一个,而他们的frontOffice也是不正确的,并附在第一个对象上。

我真的不确定从哪里开始。我觉得这是一个hibernate问题,试图将它附加到错误的团队实体。任何帮助将不胜感激,如果您需要更多,请告诉我。谢谢。

1 个答案:

答案 0 :(得分:0)

我发现问题出在我的团队实体中。这是@JoinColumn的错误命名。它只是遇到了Front Office的一个问题,因为它没有hibernate创建的关联表。

以下是解决方案:

public class Team
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "TeamId")
private Integer id;

@Version
private Integer version;

private String teamName;

private String homeTown;

@OneToMany(cascade = CascadeType.MERGE)
//@JoinColumn(name = "TeamId")
private List<Record> recordList;

@OneToOne(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
//@JoinColumn(name = "FrontOfficeId")
private FrontOffice frontOffice;

@OneToMany(cascade = CascadeType.MERGE)
//@JoinColumn(name = "TeamId")
private List<Player> playerList;
}