我遇到OneToMany关系的问题。我试图获取列表,但它没有检索任何东西。论文是我的资料来源:
这是主要的:
public class ModelTest {
public static void main(String[] args) {
//Creating
{
StudyEntity st = new StudyEntity();
st.setPatientName("Edwin");
st.setStudy_pk("111");
new StudyCRUD().create(st);
StudyEntity st2 = new StudyEntity();
st2.setPatientName("Jose");
st2.setStudy_pk("2333");
new StudyCRUD().create(st2); // Persist the object in DB
SeriesEntity se = new SeriesEntity();
se.setSeries_iuid("44444");
se.setStudyentity(st);
new SeriesCRUD().create(se);
SeriesEntity se2 = new SeriesEntity();
se2.setStudyentity(st);
new SeriesCRUD().create(se2); // Persist the object in DB
}
//Retrieving
{
StudyEntity study = new SeriesCRUD().getStudyEntityByID(1); // Get a Object by ID
System.out.println("Name: "+study.getPatientName());
System.out.println("Size: "+study.getStudySeries().size());
}
}
}
结果
姓名:Edwin
大小:0
表信息:
studyentity
ida, DELETED, MODALITY, PATIENTNAME, SENT, STUDY_DATETIME, STUDY_ID, STUDY_PK<br />
1, 0, '', 'Edwin', 0, '', '', '111'<br />
2, 0, '', 'Jose', 0, '', '', '2333'<br />
seriesentity
ID, SERIES_IUID, SERIES_PK, ida
1, '', '', 1
2, '', '', 1
实体表:
studyentity
@Entity @Table(name="studyentity")
public class StudyEntity implements Serializable{
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="ida")
private long id;
private String patientName;
private String study_id;
private String modality;
@Column(unique=true )
private String study_pk;
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
private Date study_datetime;
private boolean sent;
@Column(columnDefinition="boolean default false")
private boolean deleted;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY,mappedBy="studyentity")
private List<SeriesEntity> studySeries;
//getters and setters
seriesentity
@Entity @Table(name="seriesentity")
public class SeriesEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ida", nullable = false)
private StudyEntity studyentity;
private String series_pk;
private String series_iuid;
//Getters and Setters
答案 0 :(得分:0)
JPA不会为您维护关系。除非您将seriesEntity实例添加到studyEntity的集合中,否则除非您强制刷新,否则它不会存在。最好的做法是在创建实体时在实体中设置双向关系的两侧,然后强制数据库命中以便稍后修复它。