Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you add extra fields on a linked table (STUDENT_COURSE), you have to choose an approach according to skaffman's answer or another as shown bellow.</p> <p>There is an approach where the linked table (STUDENT_COURSE) behaves like a @Embeddable according to:</p> <pre><code>@Embeddable public class JoinedStudentCourse { // Lets suppose you have added this field @Column(updatable=false) private Date joinedDate; @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="STUDENT_ID", insertable=false, updatable=false) private Student student; @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="COURSE_ID", insertable=false, updatable=false) private Course course; // getter's and setter's public boolean equals(Object instance) { if(instance == null) return false; if(!(instance instanceof JoinedStudentCourse)) return false; JoinedStudentCourse other = (JoinedStudentCourse) instance; if(!(student.getId().equals(other.getStudent().getId())) return false; if(!(course.getId().equals(other.getCourse().getId())) return false; // ATT: use immutable fields like joinedDate in equals() implementation if(!(joinedDate.equals(other.getJoinedDate())) return false; return true; } public int hashcode() { // hashcode implementation } } </code></pre> <p>So you will have in both Student and Course classes</p> <pre><code>public class Student { @CollectionOfElements @JoinTable( table=@Table(name="STUDENT_COURSE"), joinColumns=@JoinColumn(name="STUDENT_ID") ) private Set&lt;JoinedStudentCourse&gt; joined = new HashSet&lt;JoinedStudentCourse&gt;(); } public class Course { @CollectionOfElements @JoinTable( table=@Table(name="STUDENT_COURSE"), joinColumns=@JoinColumn(name="COURSE_ID") ) private Set&lt;JoinedStudentCourse&gt; joined = new HashSet&lt;JoinedStudentCourse&gt;(); } </code></pre> <p>remember: @Embeddable class has its lifecycle bound to the owning entity class (Both Student and Course), so take care of it.</p> <p>advice: Hibernate team suppports these two approachs (@OneToMany (skaffman's answer) or @CollectionsOfElements) due some limitations in @ManyToMany mapping - cascade operation.</p> <p>regards,</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload