当使用JPARepository,@ OneToMany< =>持久化对象时,字段未初始化@ManyToOne

时间:2014-01-19 23:23:59

标签: hibernate one-to-many spring-data-jpa many-to-one joincolumn

我有以下实体: MissionInfo:

@Entity
@Table(name = "mission_info")
public class MissionInfo implements Serializable {

    @Id
    @Column(name = "id")
    @GeneratedValue
    private Long id;

    @OneToMany(cascade={CascadeType.ALL})
    @JoinColumn(name="mission_info_id")
    private List<Mission> missions;

    public MissionInfo() {
    }
    // getters and setters
}

团:

@Entity
@Table(name = "mission")
public class Mission implements Serializable {

    @Id
    @Column(name = "id")
    @GeneratedValue
    private Long id;

    @Column(name = "mission_info_id")
    private Long missionInfoId;

    @ManyToOne
    @JoinColumn(name="mission_info_id", insertable=false, updatable=false)
    private MissionInfo missionInfo;

    public Mission() {
    }
    // getters and setters
}

正如我在这个方案所有者方面所理解的 - MissionInfo

我使用以下代码进行保存:     MissionInfo info = new MissionInfo();

Mission o = new Mission();

List<Mission> list = new ArrayList<Mission>();
list.add(o);
info.setMissions(list);

MissionInfo createdMissionInfo = missionInfoRepository.save(info);

MissionInfo mi = createdMissionInfo.getMissions().get(0).getMissionInfo(); <-- NULL
Long mId = createdMissionInfo.getMissions().get(0).getMissionInfoId(); <-- NULL

对象树保存正确,createdMissionInfo包含一个Mission列表。 但保存单个任务对象后(createdMissionInfo.getMissions()。get(0)) 在missionInfoId和missionInfo中有NULL。

可以调整保存之后具有实际值的jpa / hibernate实体吗? 像这样:

// should be a pointer to saved mission info
createdMissionInfo.getMissions().get(0).getMissionInfo(); 
// should be a id of saved mission info
createdMissionInfo.getMissions().get(0).getMissionInfoId();

谢谢!

更新:

我根据kostja评论纠正实体:

@Entity
@Table(name = "mission_info")
public class MissionInfo implements Serializable {

    @Id
    @Column(name = "id")
    @GeneratedValue
    private Long id;

    @OneToMany(mappedBy = "missionInfo", cascade=CascadeType.ALL)
    private List<Mission> missions;

}


@Entity
@Table(name = "mission")
public class Mission implements Serializable {

    @Id
    @Column(name = "id")
    @GeneratedValue
    private Long id;

    @ManyToOne
    @JoinColumn(name="mission_info_id")
    private MissionInfo missionInfo;

}

但是当我使用此代码保存时:

MissionInfo info = new MissionInfo();
Mission o = new Mission();
List<Mission> list = new ArrayList<Mission>();
list.add(o);
info.setMissions(list);
MissionInfo createdMissionInfo = missionInfoRepository.save(info);

sql外键'mission_info_id'为NULL。 而我的主要问题尚未解决:

MissionInfo mi = createdMissionInfo.getMissions().get(0).getMissionInfo(); <-- NULL

1 个答案:

答案 0 :(得分:1)

您的假设并不完全正确。

  • MissionInfo不是拥有方,而是Mission。拥有方是存储关系信息的方。在多对一关系中,许多方面是拥有者,因为它必须将单个FK存储到一个方。

  • 您的MissionInfo课程与Mission的关系不同。为此,请移除joinColumn注释并为mappedBy字段添加missions属性:

    @OneToMany(cascade=CascadeType.ALL, mappedBy="missionInfo")
    private List<Mission> missions;
    
  • 您可以从missionInfoId中删除Mission字段。由于missionInfo字段上的joinColumn注释,Thsi列已存在。

  • 如果使joinColumn不可插入且不可更新,您将无法添加任何关系数据。删除这些属性:

    @ManyToOne
    @JoinColumn(name="mission_info_id")
    private MissionInfo missionInfo; 
    

持久代码现在可以正常工作。

编辑:我认为MissionInfo上的级联将负责保存。我不知道为什么它没有被触发。你可以做些什么来解决这个问题,就是在拥有方设置关系数据,即Mission。然后你不必依赖级联:

Mission o = new Mission();
o.setMissionInfo(info);
// persist Mission