简体   繁体   English

持久实体“*******”在@Entity 类中应该有主键错误

[英]Persistent entity '*******' should have primary key error in @Entity class

When creating an @Entity mapping a table my IDE notifies me: Persistent entity '*******' should have primary key .创建 @Entity 映射表时,我的 IDE 通知我: Persistent entity '*******' should have primary key

But the table in the DB doesn't have an ID (I think it's bad but it's legacy which I've no permissions to fix)但是数据库中的表没有 ID(我认为这很糟糕,但它是我无权修复的遗留问题)

What should I do?我应该怎么办? Will it work if I just omit the ID field?如果我只是省略 ID 字段,它会起作用吗? Or should I make up an ID which is not mapped on the table but satisfy the code?还是我应该编一个没有映射到表格但满足代码的ID?

Every JPA entity must have a primary key.每个 JPA 实体都必须有一个主键。 You can find the documentation here .您可以在此处找到文档。

From the Java Persistence book: Identity and Sequencing: here来自 Java Persistence 书:身份和排序: 这里

If your object does not have an id, but its table does, this is fine.如果您的对象没有 id,但它的表有,这很好。 Make the object an Embeddable object, embeddable objects do not have ids.使对象成为可嵌入对象,可嵌入对象没有 id。 You will need a Entity that contains this Embeddable to persist and query it.您将需要一个包含此 Embeddable 的实体来持久化和查询它。

Try also to read the answers here也尝试在这里阅读答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 @MappedSuperclass :持久化实体应该有主键 - @MappedSuperclass : Persistent entity should have primary key Hibernate 持久化实体应该有一个主键 - Hibernate persistent entity should have a primary key EclipseLink错误:实体类未指定主键。 它应该定义@ Id,@ EmbeddedId或@IdClass - EclipseLink error: Entity class has no primary key specified. It should define either an @Id, @EmbeddedId or an @IdClass 具有复合主键的实体类没有getter和setter - Entity class with a composite primary key doesn't have getter and setter 实体类没有指定主键 - Entity class has no primary key specified DB显示没有主键的表的实体类 - Entity class with DB showing Table with No primary key 在jpa实体中设置外键时出错:您无法刷新与非托管对象具有持久关联的非托管对象… - Error setting foreign key in jpa entity: You cannot flush unmanaged objects… that have persistent associations to unmanaged objects Java实体错误-主键为空 - Java Entity error - null primary key 实体类是否应始终具有ID字段 - Should an entity class always have an ID field 向复合主键添加外键,并在JPA Entity类中进行更改 - Add a foreign key to composite primary key and changes in JPA Entity class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM