Hello, I am making the link tables on Hibernate, one teacher to many students.

Teacher:

@Entity @Table(name = "teacher") public class Teacher implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") private int id; @Column(name = "name") private String name; @OneToMany(mappedBy = "teacher", cascade = CascadeType.ALL) private Set<Pupil> pupils = new HashSet<>(); // геттеры сеттеры } 

Pupil:

 @Entity @Table(name = "pupil") public class Pupil implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") private int id ; @Column(name = "name") private String name; @ManyToOne() @JoinColumn(name = "teacher_id") private Teacher teacher; } 

Then I create a teacher and a couple of students, put the students in the teacher and save the teacher. I.e:

  // сервис ObjectDao objectDao = new ObjectDao(); // создаю учителя Teacher teacher = new Teacher(); teacher.setName("teacher name"); // создаю ученика Pupil pupil = new Pupil(); pupil.setName("pupil name"); // кладу ученика в учителя teacher.getPupils().add(pupil); // сохраняю учителя. objectDao.saveObject(teacher); 

Result:

 mysql> select * from teacher; +----+--------------+ | id | name | +----+--------------+ | 1 | teacher name | +----+--------------+ mysql> select * from pupil; +----+------------+------------+ | id | name | teacher_id | +----+------------+------------+ | 1 | pupil name | NULL | +----+------------+------------+ 

Can you please tell me why my student teacher ID is null?

    3 answers 3

    At the request of Nicolas Chabanovsky more fully.

    The founding fathers of Hibernate CHRISTIAN BAUER, GAVIN KING and GARY GREGORY in their book Java Persistence with Hibernate SECOND EDITION 2016 ( https://www.manning.com/books/java-persistence-with-hibernate-second-edition ) write (more two passages in my translation into Russian)

    (p. 43-44)

    The basic procedure for linking a bid with an item ...

    The basic procedure for connecting a Bid and Item is as follows:

    • anItem.getBids (). Add (aBid);

    • aBid.setItem (anItem);

    When you create a bidirectional link, two actions are required:

    • You must add the Bid to the bids collection Item.

    • item Bid property must be set.

    JPA does not support persistent connections. If you want to manipulate connections, you must do it as if you were working without Hibernate. If you have a bidirectional link, you must take care of the two sides of the link. If you still do not understand, then ask yourself “What would I do without Hibernate?”

    Further in clause 7.3.3.

    The following code creates ...

    The following code creates a new Item and a new Bid and then connects them.

    Item someItem = new Item ("Some Item");

    Bid someBid = new Bid (new BigDecimal ("123.00"), someItem);

    someItem.getBids (). add (someBid); <- Don't forget!

    You should take care of the two sides of the link: The Bid constructor sets up a reference to the item. To maintain the integrity of the memory, you must add the bid to the Item list for the Item. Now the link in terms of Java code is complete, all links are established.

      Try to add a teacher to a student before saving:

       pupil.setTeacher(teacher); 

        HIBERNATE (and JPA) during data update operations (not when loading data) do not support master-detailed links in any way. In particular, when creating a part, you must manually register a link to the master in the part and include the part in the master list.

        • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky