简体   繁体   English

Javax-Persistance:实体没有使用 Java 记录的主键

[英]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 规范,实体必须遵循以下要求:

  • the entity class needs to be non-final,实体类需要是非最终的,
  • the entity class needs to have a no-arg constructor that is either public or protected,实体类需要有一个公共或受保护的无参数构造函数,
  • the entity attributes must be non-final.实体属性必须是非最终的。

However, as explained by this article , the Java Record type is defined like this:但是,正如本文所解释的,Java Record 类型是这样定义的:

  • the associated Java class is final,关联的 Java 类是最终的,
  • there is only one constructor that takes all attributes,只有一个构造函数接受所有属性,
  • the Java record attributes are final. Java 记录属性是最终的。

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.

相关问题 我已经使用 PartitionKey 注释声明了主键,但仍然得到 Entity Order 没有声明主键 - I have declared primary using PartitionKey annotation but still getting Entity Order does not declare a primary key javax.persistance.Entity无法导入? - javax.persistance.Entity not getting imported? Hibernate 持久化实体应该有一个主键 - Hibernate persistent entity should have a primary key @MappedSuperclass :持久化实体应该有主键 - @MappedSuperclass : Persistent entity should have primary key 如何使用实体管理器插入记录而不定义主键字段? - How to insert records using the Entity Manager without defining the Primary Key field? 持久实体“*******”在@Entity 类中应该有主键错误 - Persistent entity '*******' should have primary key error in @Entity class Java实体错误-主键为空 - Java Entity error - null primary key 使用非主键列的实体映射 - Entity mapping using a non primary key column 为什么在 JPA 2/Hibernate 中使用共享主键时实体需要可序列化? - Why does an Entity need to be serializable when using shared primary key in JPA 2/Hibernate? 具有复合主键的实体类没有getter和setter - Entity class with a composite primary key doesn't have getter and setter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM