我正在 hibernate 中学习onetomany,我有一个拥有多个类(class)的讲师。我正在使用讲师和类(class)类来注释数据库中具有相同名称的表。在 GetInstructorCourseDemo 中,我尝试使用以下方式打印讲师信息相关类(class),但在 System.out.println("Course "+tempinstructor.getCourse());
我在控制台中看到的是 Course [MyHibernate.Course@56ccd751, MyHibernate.Course@458544e0]
。在我看来, hibernate 可以从数据库读取类(class),但它无法正确显示,否则我不知道是什么会导致这个问题
@Entity
@Table(name = "instructor")
public class Instructor {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name ="first_name" )
private String first_name;
@Column(name = "last_name")
private String last_name;
@Column(name = "email")
private String email;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "instructor_detail_id")
private instructor_detail Instructor_detail;
Instructor(){}
@OneToMany(mappedBy = "instructor",cascade = {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,
CascadeType.REFRESH})
private List<Course> course;
public Instructor(String first_name, String last_name, String email) {
this.first_name = first_name;
this.last_name = last_name;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public instructor_detail getInstructor_detail() {
return Instructor_detail;
}
public void setInstructor_detail(instructor_detail instructor_detail) {
Instructor_detail = instructor_detail;
}
public List<Course> getCourse() {
return course ;
}
public void setCourse(List<Course> course) {
this.course = course;
}
public void add(Course tempciurse){
if(course==null){
course=new ArrayList<>();
}
course.add(tempciurse);
tempciurse.setInstructor(this);
}
public class GetInstructorCourseDemo {
public static void main(String[]args) {
// create session factory
SessionFactory factory = new Configuration()
.configure("hibername.cfg.xml")
.addAnnotatedClass(Instructor.class)
.addAnnotatedClass(instructor_detail.class)
.addAnnotatedClass(Course.class)
.buildSessionFactory();
Session session = factory.getCurrentSession();
try {
session.beginTransaction();
int id=2;
Instructor tempinstructor=session.get(Instructor.class,id);
System.out.println("instructor : "+tempinstructor);
System.out.println("Course "+tempinstructor.getCourse());
session.getTransaction().commit();
System.out.println("Done!");
}
finally {
factory.close();
}
}
}
@Entity
@Table(name = "course")
public class Course {
@Id@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "title")
private String title;
@ManyToOne(cascade = {CascadeType.DETACH,CascadeType.REFRESH,CascadeType.MERGE,CascadeType.PERSIST})
@JoinColumn(name = "instructor_id")
private Instructor instructor;
Course(){}
public Course(String title) {
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Instructor getInstructor() {
return instructor;
}
public void setInstructor(Instructor instructor) {
this.instructor = instructor;
}
}
最佳答案
它错过了 Instructor
和 Course
类型中的 toString()
实现。
由于您没有覆盖它,因此使用默认值 -->
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
注意:请注意不要在 toString
实现中直接使用类(class)和讲师引用,从而导致循环引用,从而导致 StackOverflow 错误
。
关于java - Hibernate 无法正确显示 arraylist 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61870406/