[英]Javax-Persistance : Entity does not have a primary key using Java records
I am trying to create a entity class using Java record, but I get the error message "Entity does not have primary key" although I assigned an ID annotation.我正在尝试使用 Java 记录创建实体类,但我收到错误消息“实体没有主键”,尽管我分配了 ID 注释。
import javax.persistence.*;
import java.time.LocalDate;
@Entity
public record Agent (
@Id
String code,
String name,
LocalDate date,
String workingArea,
String country,
String phoneNumber,
boolean licenseToKill,
int credits,
byte[] picture)
{}
What's wrong with this?这有什么问题?
A record
cannot be used as Hibernate entity because it breaks the requirements of an entity according to JPA specification. record
不能用作 Hibernate 实体,因为它违反了 JPA 规范对实体的要求。 Make it class
and use @Immutable
annotation instead:使它成为
class
并使用@Immutable
批注代替:
@Entity
@Immutable
public class Agent
Just clearing the answer for completeness (although @Turning85 and @gkatiforis have already provided correct explanation):只是为了完整性而清除答案(尽管@Turning85 和@gkatiforis 已经提供了正确的解释):
According to the JPA specification, an entity must follow these requirements:根据 JPA 规范,实体必须遵循以下要求:
However, as explained by this article , the Java Record type is defined like this:但是,正如本文所解释的,Java Record 类型是这样定义的:
But records are a good fit for a DTO projection, which is often used as a read-only representation of the data stored in your database.但是记录非常适合 DTO 投影,它通常用作存储在数据库中的数据的只读表示。 more info - https://thorben-janssen.com/java-records-hibernate-jpa/
更多信息 - https://thorben-janssen.com/java-records-hibernate-jpa/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.