Hibernate:为类提供了错误类型的 id
尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经知道如何复合主键,有两种方法可以使用
@IdClass()
或@EmbeddedId()
。您已经为复合主键创建了单独的类。StudentId.java
然后将其传递到IdClass
注释中。下面是使用
IdClass
组合主键的代码:StudentId.java
有关详细信息,请参阅此处 如何在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.StudentId.java
then pass it intoIdClass
annotation.Here down is code to composite primary key using
IdClass
:StudentId.java
For further information see here How to define composite foreign key mapping in hibernate?