Hibernate:为类提供了错误类型的 id

发布于 2025-01-09 08:01:32 字数 988 浏览 0 评论 0原文

尝试将 StudentId 保存到 STUDENT_ID 表时出现以下异常。我不知道如何保存一个本身具有复合主键的实体。

org.hibernate.TypeMismatchException:为类 StudentIdEntity 提供了错误类型的 id。预期: StudentIdEntity ,获得类 java.lang.Long|ConsoleCatcherOutputStream:write

下面是我的代码:

@Entity
@Table(name = "STUDENT_ID")
@IdClass(StudentIdEntity.class)
public class StudentIdEntity implements Serializable {
    @Id
    @Column(name = "ID_1")
    private Long id1;
    @Id
    @Column(name = "ID_2", nullable = false)
    private Long id2;

    public StudentIdEntity() {
    }

    public Long getId1() {
        return id1;
    }

    public void setId1(Long id1) {
        this.id = id1;
    }

    public Long getLocationId2() {
        return id2;
    }

    public void setLocationId2(Long id2) {
        this.id2 = id2;
    }
}

StudentIdEntity studentIdEntity = new StudentIdEntity();
studentIdEntity.setId1(id1);
studentIdEntity.setId2(id2);
studentImplement.save(studentIdEntity);

I got the below exception when trying to save studentId to the STUDENT_ID table. I don't know how to save an entity has a composite primary key by itself.

org.hibernate.TypeMismatchException: Provided id of the wrong type for class StudentIdEntity . Expected: StudentIdEntity , got class java.lang.Long|ConsoleCatcherOutputStream:write

Here down is my code:

@Entity
@Table(name = "STUDENT_ID")
@IdClass(StudentIdEntity.class)
public class StudentIdEntity implements Serializable {
    @Id
    @Column(name = "ID_1")
    private Long id1;
    @Id
    @Column(name = "ID_2", nullable = false)
    private Long id2;

    public StudentIdEntity() {
    }

    public Long getId1() {
        return id1;
    }

    public void setId1(Long id1) {
        this.id = id1;
    }

    public Long getLocationId2() {
        return id2;
    }

    public void setLocationId2(Long id2) {
        this.id2 = id2;
    }
}

StudentIdEntity studentIdEntity = new StudentIdEntity();
studentIdEntity.setId1(id1);
studentIdEntity.setId2(id2);
studentImplement.save(studentIdEntity);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落成白 2025-01-16 08:01:32

您已经知道如何复合主键,有两种方法可以使用 @IdClass()@EmbeddedId()。您已经为复合主键创建了单独的类。

  • 首先,您必须创建类 StudentId.java 然后将其传递到 IdClass 注释中。

下面是使用 IdClass 组合主键的代码:

StudentId.java

public class StudentId implements Serializable {
    private Long id1;
    private Long id2;

    // default constructor

    public StudentId(Long id1, Long id2) {
        this.id1 = id1;
        this.id2 = id2;
    }

    // equals() and hashCode()
}
@Entity
@Table(name = "STUDENT_ID")
@IdClass(StudentId.class)
public class StudentIdEntity {
    @Id
    @Column(name = "ID_1")
    private Long id1;

    @Id
    @Column(name = "ID_2", nullable = false)
    private Long id2;

    // default constructor, constructer, getter and setter
}

StudentIdEntity studentIdEntity = new StudentIdEntity();
studentIdEntity.setId1(id1);
studentIdEntity.setId2(id2);
studentImplement.save(studentIdEntity);

有关详细信息,请参阅此处 如何在hibernate中定义复合外键映射?

You have know about how to composite primary key there are two way to composite primary key using @IdClass() or @EmbeddedId(). You have make separate class for composite primary key.

  • First you have to make class StudentId.java then pass it into IdClass annotation.

Here down is code to composite primary key using IdClass:

StudentId.java

public class StudentId implements Serializable {
    private Long id1;
    private Long id2;

    // default constructor

    public StudentId(Long id1, Long id2) {
        this.id1 = id1;
        this.id2 = id2;
    }

    // equals() and hashCode()
}
@Entity
@Table(name = "STUDENT_ID")
@IdClass(StudentId.class)
public class StudentIdEntity {
    @Id
    @Column(name = "ID_1")
    private Long id1;

    @Id
    @Column(name = "ID_2", nullable = false)
    private Long id2;

    // default constructor, constructer, getter and setter
}

StudentIdEntity studentIdEntity = new StudentIdEntity();
studentIdEntity.setId1(id1);
studentIdEntity.setId2(id2);
studentImplement.save(studentIdEntity);

For further information see here How to define composite foreign key mapping in hibernate?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文