许多与Class和Section之间的关系如下:
@Entity
@Table(name = "class")
public class Classz extends BaseEntity{
private int classId;
private String className;
private Set<ClassSection> classHasSections;
public Classz() {
}
public Classz(String className) {
this.className = className;
classHasSections = new HashSet<>();
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getClassId() {
return classId;
}
public void setClassId(int classId) {
this.classId = classId;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className =className;
}
@OneToMany(mappedBy = "classz", cascade = CascadeType.ALL, orphanRemoval = true,fetch = FetchType.EAGER)
public Set<ClassSection> getClassHasSections() {
return classHasSections;
}
public void setClassHasSections(Set<ClassSection> classHasSections) {
this.classHasSections = classHasSections;
}
@Override
public String toString() {
return "Classz{" +
"className='" + className + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Classz classz = (Classz) o;
return classId == classz.classId;
}
@Override
public int hashCode() {
return classId;
}
}
Section.java
@Entity(name = "section")
public class Section extends BaseEntity {
private int sectionId;
private String sectionName;
private Set<ClassSection> classSections;
public Section(){
}
public Section(String sectionName){
this.sectionName = sectionName;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getSectionId() {
return sectionId;
}
public void setSectionId(int sectionId) {
this.sectionId= sectionId;
}
public String getSectionName() {
return sectionName;
}
public void setSectionName(String sectionName) {
this.sectionName = sectionName;
}
@OneToMany(mappedBy = "section")
public Set<ClassSection> getClassSections() {
return classSections;
}
public void setClassSections(Set<ClassSection> classSections) {
this.classSections = classSections;
}
@Override
public String toString() {
return "Section{" +
"sectionName='" + sectionName + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Section section = (Section) o;
return sectionId == section.sectionId;
}
@Override
public int hashCode() {
return sectionId;
}
}
ClassSection.java
@Entity
@Table(name = "class_has_section")
public class ClassSection implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer classSectionId;
private Classz classz;
private Section section;
private Integer capacity;
@Id
@ManyToOne
@JoinColumn(name = "class_id")
public Classz getClassz() {
return classz;
}
public void setClassz(Classz classz) {
this.classz = classz;
}
@Id
@ManyToOne
@JoinColumn(name = "section_id")
public Section getSection() {
return section;
}
public void setSection(Section section) {
this.section= section;
}
@Column(name = "capacity")
public Integer getCapacity() {
return capacity;
}
public void setCapacity(Integer capacity) {
this.capacity = capacity;
}
public Integer getClassSectionId() {
return classSectionId;
}
public void setClassSectionId(Integer classSectionId) {
this.classSectionId = classSectionId;
}
@Override
public String toString() {
return "ClassSection{" +
"classSectionId=" + classSectionId +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ClassSection that = (ClassSection) o;
return classSectionId != null ? classSectionId.equals(that.classSectionId) : that.classSectionId == null;
}
@Override
public int hashCode() {
return classSectionId != null ? classSectionId.hashCode() : 0;
}
}
在Springboot应用程序中,我运行以下命令。考虑这样的情况,其中一个记录已经插入到db中,用于类和section以及classSection。
由Springboot应用程序运行程序调用的方法
@Transactional
public void runTest1(){
Classz classz = new Classz("6");
Section section = new Section("F");
ClassSection classSection = new ClassSection();
classSection.setClassz(classz);
classSection.setSection(section);
classSection.setCapacity(1);
classz.getClassHasSections().add(classSection);
sectionRepository.save(section);
classRepository.save(classz);
// test
// update
//bookA.getBookPublishers().remove(bookPublisher);
// Classz classz1 = new Classz();
//classz1.setClassId(4);
Classz bok=classRepository.findOne(1); // StackOverflowError
// Classz bok=classRepository.findOne(classz1.getClassId()); //Worked
// test
System.out.println("Size ===="+bok.getClassHasSections().size());
}
这里考虑1已经插入了ID: 发生以下错误:
java.lang.StackOverflowError: null
at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:117) ~[na:1.8.0_45]
at java.io.OutputStreamWriter.write(OutputStreamWriter.java:207) ~[na:1.8.0_45]
at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:129) ~[na:1.8.0_45]
at java.io.PrintStream.write(PrintStream.java:526) ~[na:1.8.0_45]
at java.io.PrintStream.print(PrintStream.java:669) ~[na:1.8.0_45]
任何人都可以帮忙吗?或有类似的问题。我对这个问题提出了一些问题。建议是toString()和hasCode()但到目前为止没有运气