0

I am studying onetomany in hibernate I have an Instructor that has more than one course.I am using Instructor and Course class for Annotating my tables with the same names in the database.in the GetInstructorCourseDemo am trying to print the Instructor information with an associated course but inSystem.out.println("Course "+tempinstructor.getCourse()); what I see in the console is Course [MyHibernate.Course@56ccd751, MyHibernate.Course@458544e0] . it seems to me hibernate can read courses from database but it can't show it correctly otherwise have i no idea what can cause this problem

@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;
    }
}
pooya
  • 115
  • 3
  • 13
  • 1
    Does this answer your question? [How do I print my Java object without getting "SomeType@2f92e0f4"?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – OH GOD SPIDERS May 18 '20 at 13:14
  • there's nothing wrong with hibernate or java or mapping. What you will need to improve is your Course Class and Override the toString() method there. The toString method is called by default when you try to print the object. – Tushar Jajodia May 18 '20 at 13:19

2 Answers2

0

It missed toString() implementation in Instructor and Course types.

As you don't override it the default one is used -->

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

NB : take care to not use course and instructor references directly in toString implementation causing a circular reference and so a StackOverflow error.

CodeScale
  • 3,046
  • 1
  • 12
  • 20
  • I add the toString method but still, its show me the same thing can you please tell me what exactly i must add to my both class exactly? I think i got your point wrong or i write toStrong method in the wrong way – pooya May 18 '20 at 13:41
  • could you add your toString implementation in your question please ? – CodeScale May 18 '20 at 15:52
0

for solving this issue i have to add toString method in Course, Instructor as following
for Instructor

@Override
    public String toString() {
        return "Instructor{" +
                "id=" + id +
                ", first_name='" + first_name + '\'' +
                ", last_name='" + last_name + '\'' +
                ", email='" + email + '\'' +
                ", Instructor_detail=" + Instructor_detail +
                ", course=" + course +
                '}';
    }

for Course

   @Override
    public String toString() {
        return "Course{" +
                "id=" + id +
                ", title='" + title + '\'' +
                '}';
    }

what was causing this confusion was that I didn't have toString method and I shouldn't have Instructor filed in my toString method

pooya
  • 115
  • 3
  • 13
YWILLS
  • 128
  • 10