Used this as my base point: https://vladmihalcea.com/the-best-way-to-map-a-many-to-many-association-with-extra-columns-when-using-jpa-and-hibernate/
I have an entity named Attendance that has an and emebedid AttendacneId that has two columns lectureId (string) and studenId(UUID). The attendance entity also has other properties as well containing a ManyToOne relationship with student and lecture enity using the @MapsId on them. The student extends user entity where the id resides. now when I try to save anything I get this error.
java.util.NoSuchElementException
at java.util.ArrayList$Itr.next(ArrayList.java:860)
at org.hibernate.cfg.annotations.TableBinder.linkJoinColumnWithValueOverridingNameIfImplicit(TableBinder.java:724)
at org.hibernate.cfg.PkDrivenByDefaultMapsIdSecondPass.doSecondPass(PkDrivenByDefaultMapsIdSecondPass.java:37)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1684)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1641)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:286)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:473)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:84)
at utility.Utils.<clinit>(Utils.java:60)
at utility.Testing.main(Testing.java:21)
My AttendanceId class
@Embeddable
public class AttendanceId implements Serializable {
@Column(name = "lecture_fid")
@Getter
@Setter
private String lectureId;
@Column(name = "student_fid", columnDefinition = "uuid")
@Getter
@Setter
private UUID studentId;
public AttendanceId() {}
}
My Attendance class
@Entity
@Table(name = "attendance")
public class Attendance implements Serializable {
@EmbeddedId
@Getter
@Setter(AccessLevel.PRIVATE)
private AttendanceId id;
// other fields
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("lectureId")
private Lecture lecture;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("studentId")
private Student student;
}
My Student Class
@Entity
@PrimaryKeyJoinColumn(name = "id")
public class Student extends User implements Serializable,
Comparable<Student> {
// other fields
@OneToMany(mappedBy = "student", fetch = FetchType.LAZY)
@Getter
private List<Attendance> attendances = new ArrayList<>();
}
My User class
@Entity
@Table(name = "users", uniqueConstraints = {
@UniqueConstraint(columnNames = {"email", "username"})})
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class User implements Serializable {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid")
@Column(columnDefinition = "UUID")
@Getter(AccessLevel.PUBLIC)
@Setter(AccessLevel.PUBLIC)
private UUID id;
// other fields.
}
My Lecture Class
@NamedNativeQueries(
@NamedNativeQuery(name = "getLectureId", query = "select
get_lecture_id()")
)
@Entity
@Table(name = "lecture")
public class Lecture implements Serializable {
@Id
@Column(name = "id")
@GenericGenerator(name = "lectureIdGenerator",
strategy = "entities.LectureIdGenerator")
@GeneratedValue(generator = "lectureIdGenerator")
@Getter
@Setter
private String id;
@OneToMany(mappedBy = "lecture", fetch = FetchType.LAZY)
@Getter
private List<Attendance> attendances = new ArrayList<>();
// other fields
}
I think the inheritance is causing an issue. There are other classes/entities as well that extends the user entity. If anyone could help me find the problem that will be great. Thank you.