【问题标题】:Hibernate @embededId containing a foreign key of an entity with inheritanceHibernate @embededId 包含具有继承的实体的外键
【发布时间】:2019-07-13 16:03:04
【问题描述】:

以此为基点:https://vladmihalcea.com/the-best-way-to-map-a-many-to-many-association-with-extra-columns-when-using-jpa-and-hibernate/

我有一个名为 Attendance 的实体,它有一个 emebedid AttendacneId,它有两列 LectureId(字符串)和 studenId(UUID)。出勤实体还具有其他属性,其中包含使用 @MapsId 与学生和讲座实体建立多对一关系。学生扩展了 id 所在的用户实体。现在,当我尝试保存任何内容时,我会收到此错误。

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)

我的 AttendanceId 班级

@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() {} 
}

我的考勤班

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

我的学生班

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

我的用户类

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

我的课堂

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

我认为继承导致了一个问题。还有其他类/实体可以扩展用户实体。如果有人可以帮助我找到问题,那就太好了。谢谢。

【问题讨论】:

    标签: java database hibernate jpa


    【解决方案1】:

    这个问题是由HHH-7135 Hibernate 问题引起的。

    【讨论】:

    • 我已将所有实体添加到 hibernate 5 包中。如果您发现任何错误或不符合标准的地方,请告诉我。我愿意接受任何形式的反馈。我已经修改了 ORMUnitTestCase.java 类中的测试用例。如果您发现任何问题,请告诉我。感谢您的帮助。 github.com/sukhvir41/hibernate-test-case-templates.git
    • 您的项目无法编译。缺少导入和依赖项。此外,删除所有不必要的实体。您只需添加与您的问题相关的实体。
    • 我已经删除了依赖项。如果它仍然不起作用,请告诉我。谢谢。 github.com/sukhvir41/hibernate-test-case-templates.git
    • 还是同样的问题。
    • 现在试试我已经更新了实体并且能够重现它。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多