加入

时间:2017-06-29 04:52:10

标签: java database hibernate

我正在努力建立一对多的关系。

调查回复详情

@Entity
@Table(name = "survey_response_details")
public class SurveyResponseDetail {
    protected Long id;
    private SurveyResponseModel surveyResponseModel;


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="response_id")
    public SurveyResponseModel getSurveyResponseModel() {
        return surveyResponseModel;
    }

    // setter of id and surveyResponseModel
}

调查回复

@Entity
@Table(name = "survey_response")
public class SurveyResponseModel{
        protected Long id;
        private List<SurveyResponseDetail> surveyResponseDetails;

        @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

        @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL,     mappedBy = "surveyResponseModel")
    public List<SurveyResponseDetail> getSurveyResponseDetails() {
        return surveyResponseDetails;
    }

        // setter of id and surveyResponseDetails
}

主要课程

if(!surveyResponseDetailList.isEmpty())
                    surveyResponseModel.setSurveyResponseDetails(surveyResponseDetailList);

session.save(surveyResponseModel);

但是当我试图保存它时,同时保存surveyResponseDetail 它显示错误,即response_id不能为null,因为我保持它不为null。请帮忙。

0 个答案:

没有答案