This question already has answers here:
How do I print my Java object without getting “SomeType@2f92e0f4”?

(9个答案)


上个月关闭。





我正在休眠状态下学习onetomany,我有一个具有多个课程的讲师。我正在使用Instructor和Course类对数据库中具有相同名称的表进行注释。在GetInstructorCourseDemo中,我尝试使用相关课程打印讲师信息但是在System.out.println("Course "+tempinstructor.getCourse());中,我在控制台中看到的是Course [MyHibernate.Course@56ccd751, MyHibernate.Course@458544e0]。在我看来,休眠状态可以从数据库中读取课程,但是它无法正确显示它,否则我不知道什么会导致此问题

@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;
    }
}

最佳答案

它错过了toString()Instructor类型的Course实现。

因为您没有覆盖它,所以使用默认值->

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}


注意:注意不要在toString实现中直接使用课程和讲师的引用,从而导致循环引用,从而导致StackOverflow error

关于java - Hibernate不能正确显示arraylist的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61870406/

10-12 05:56