Hibernate返回重复项

时间:2016-09-21 12:11:44

标签: java mysql spring hibernate

我正在使用spring,spring-mvc,hibernate,MySQL,jQuery,bootstrap编写Web应用程序。

我有这样的实体

@Entity
@Table(name = "PLAN_HISTORY")
public class PlanHistory {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

     @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "id_year")
    private LearningYear learningYear;


    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "id_gruppa")
    private GroupTbl groupTbl;

    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "id_kafedra")
    private Kafedra kafedra;

    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "id_discipline")
    private Discipline discipline;

    @Column(name = "n_plan")
    private int nPlan;
    @Column(name = "n_consult")
    private int nConsult;

    @Column(name = "z_lesson")
    private int zLesson;
    @Column(name = "z_lab")
    private int zLab;
    @Column(name = "z_practice")
    private int zPractice;
    @Column(name = "z_consult")
    private int zConsult;
    @Column(name = "z_kurswork")
    private int zKursWork;
    @Column(name = "z_kurswork_ing_fah")
    private int zKursWorkIngFah;
    @Column(name = "z_kurswork_fah")
    private int zKursWorkFah;
    @Column(name = "z_controlwork")
    private int zControlWork;
    @Column(name = "z_cce")
    private int zCce;
    @Column(name = "z_exam")
    private int zExam;
    @Column(name = "z_zalik")
    private int zZalik;
    @Column(name = "z_cpa")
    private int zCpa;
    @Column(name = "z_atest")
    private int zAtest;
    @Column(name = "z_ccrdp")
    private int zCcrDp;
    @Column(name = "z_pzdp")
    private int zPzdp;

    @Column(name = "s_lesson")
    private int sLesson;
    @Column(name = "s_lab")
    private int sLab;
    @Column(name = "s_practice")
    private int sPractice;
    @Column(name = "s_consult")
    private int sConsult;
    @Column(name = "s_kurswork")
    private int sKursWork;
    @Column(name = "s_kurswork_ing_fah")
    private int sKursWorkIngFah;
    @Column(name = "s_kurswork_fah")
    private int sKursWorkFah;
    @Column(name = "s_controlwork")
    private int sControlWork;
    @Column(name = "s_cce")
    private int sCce;
    @Column(name = "s_exam")
    private int sExam;
    @Column(name = "s_zalik")
    private int sZalik;
    @Column(name = "s_cpa")
    private int sCpa;
    @Column(name = "s_atest")
    private int sAtest;
    @Column(name = "s_ccrdp")
    private int sCcrDp;
    @Column(name = "s_pzdp")
    private int sPzdp;
   }

与另一个看起来像这个

的实体有关系
@Entity
@Table(name = "LEARNING_YEAR")
public class LearningYear {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @NotEmpty(message = "Ім\'я не може бути пустим")
    @Column(name = "name")
    private String name;


    @NotNull
    @Min(value = 2000)
    @Max(value = 3000)
    @Column(name = "begin")
    private int beginYear;

    @NotNull
    @Min(value = 2000)
    @Max(value = 3000)
    @Column(name = "end")
    private int endYear;

    @NotNull
    @Column(name = "current")
    private int current;

    @JsonIgnore
    @OneToMany(mappedBy = "learningYear")
    private Set<PlanHistory> planHistory;
}

与LearningYear相同的其他实体;

如果我尝试使用条件LearninYear从planHistory获取类似数据,Kafedra我收到此错误:

  

org.hibernate.LazyInitializationException:懒得初始化   角色集合

如果我在实体fetch = FetchType.EAGER中添加以下内容,则会收到重复项。我通过setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)解决了这个问题,但是当我尝试获取数据时收到溢出异常。

我该怎么做才能避免这个问题?

0 个答案:

没有答案