admin管理员组文章数量:1122846
I have an entity that looks like this
@Entity
@IdClass(AId.class)
class A {
@Id
private String userName
@Id
@NotNull
@JoinColumn(name = "location_id", referenceColumnName = "id", nullable=false, foreignKey = @ForeignKey(name = ...))
private Location location;
private String userInformation;
}
The class Location looks like this:
@Entity
class Location {
@NotNull
private Long id;
@NotNull
@Column(nullable = false}
private String name;
}
And then I have a class to define the Id
class AId implements Serializable {
private Long locationId;
private String userName;
}
Now, I do some manipulation on the object of class A and then want to store it. However, I get an exception that tells me that the non-nullable field Location#name that is reference by an object of A is null. I debugged through it and made sure that the field is actually not null. However, I still get this. What's the issue? The location that A is referring to is already persisted on the database.
I have an entity that looks like this
@Entity
@IdClass(AId.class)
class A {
@Id
private String userName
@Id
@NotNull
@JoinColumn(name = "location_id", referenceColumnName = "id", nullable=false, foreignKey = @ForeignKey(name = ...))
private Location location;
private String userInformation;
}
The class Location looks like this:
@Entity
class Location {
@NotNull
private Long id;
@NotNull
@Column(nullable = false}
private String name;
}
And then I have a class to define the Id
class AId implements Serializable {
private Long locationId;
private String userName;
}
Now, I do some manipulation on the object of class A and then want to store it. However, I get an exception that tells me that the non-nullable field Location#name that is reference by an object of A is null. I debugged through it and made sure that the field is actually not null. However, I still get this. What's the issue? The location that A is referring to is already persisted on the database.
Share Improve this question asked Nov 22, 2024 at 15:57 SCMSCM 756 bronze badges 1- Entity Location is missing annotation @Id – grigouille Commented Nov 25, 2024 at 10:02
2 Answers
Reset to default 1Try with ManyToOne and change locationId to location :
@Entity
class Location {
@Id
@NotNull
private Long id;
}
class AId implements Serializable {
private Long location;
private String userName;
}
@Entity
@IdClass(AId.class)
class A {
@Id
private String userName
@Id
@NotNull
@ManyToOne
@JoinColumn(name = "location_id", nullable=false, foreignKey = @ForeignKey(name = ...))
private Location location;
}
See also https://jakartaee.github.io/jakartaee-documentation/jakartaee-tutorial/9.1/persist/persistence-basicexamples/persistence-basicexamples.html
This may be caused by the types of @Id fields not matching the types in id class. According to https://docs.oracle.com/javaee/7/api/javax/persistence/IdClass.html
The names of the fields or properties in the primary key class and the primary key fields or properties of the entity must correspond and their types must be the same.
本文标签: javaUsing an Entity in an IdClass as IdStack Overflow
版权声明:本文标题:java - Using an @Entity in an @IdClass as @Id - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736302520a1931565.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论