Min*_*eet 6 java jpa kotlin quarkus
如何使用 Quarkus JPA 声明复合密钥?
尝试@Id在@EntityQuarkus的类中使用多个注释,会导致错误:
Currently the @Id annotation can only be placed on a single field or method. Offending class is abc.model.Property
at io.quarkus.spring.data.deployment.generate.StockMethodsAdder.getIdAnnotationTargetRec(StockMethodsAdder.java:940)
Run Code Online (Sandbox Code Playgroud)
但首先,在声明之后
interface PropertyRepository : CrudRepository<Property, Pair<String, abc.model.Entity>>
Run Code Online (Sandbox Code Playgroud)
没有上述声明,就没有投诉,但没有可能主动管理Property.
如何规避这个错误?
我正在处理两个 JPA 实体:
1. 第一个命名Entity(不要误认为注释)
2. 第二个命名Property
AnEntity可以有 0..n 个Property. 代码如下:
@Entity
data class Entity (
@Id
@Column(name = "entity_id", updatable = false)
var entityId: String? = null,
@Column(nullable = true)
var type: String? = null
) {
@OneToMany(mappedBy = "entity")
var properties: List<Property>? = null
}
Run Code Online (Sandbox Code Playgroud)
@Entity
data class Property (
@Id
@Column(name = "type")
var type: String? = null,
@Id
@ManyToOne
@JoinColumn(name = "entity_id")
private var entity: abc.model.Entity? = null
) : Serializable
Run Code Online (Sandbox Code Playgroud)
如下声明复合主键@EmbeddedId并不能解决问题,因为 Quarkus 目前不允许其他注释,而不是@Id这种情况:
@Entity
data class Entity (
@Id
@Column(name = "entity_id", updatable = false)
var entityId: String? = null,
@Column(nullable = true)
var type: String? = null
) {
@OneToMany(mappedBy = "propertyId.entityId")
var properties: List<Property>? = null
}
interface PropertyRepository : CrudRepository<Property, PropertyId>
@Embeddable
data class PropertyId (
var type: String? = null,
@Column(name = "entity_id")
private var entityId: String? = null
) : Serializable
@Entity
data class Property (
@EmbeddedId
var propertyId: PropertyId? = null,
@Column(name = "constant_value")
var constantValue: String? = null
)
Run Code Online (Sandbox Code Playgroud)
java.lang.IllegalArgumentException: Currently only Entities with the @Id annotation are supported. Offending class is abc.model.Property
at io.quarkus.spring.data.deployment.generate.StockMethodsAdder.getIdAnnotationTargetRec(StockMethodsAdder.java:932)
Run Code Online (Sandbox Code Playgroud)
有可能,您的实体必须扩展 PanacheEntityBase 并使用类级别注释 @Table、@Entity、@IdClass(PK.class),其中 Pk 是具有复合主键字段的 POJO,那么您必须使用以下方式声明相同的字段实体中的 @Id 注释。例如:
@Entity()
@Table(name = "agent")
@IdClass(AgentPK.class)
public class AgentBRA extends PanacheEntityBase {
@Id
public Integer idUsuario;
@Id
public Integer idAgent;
Run Code Online (Sandbox Code Playgroud)
和 AgentPk:
public class AgentPK implements Serializable{
protected Integer idUsuario;
protected Integer idAgent;
// getters setters and hascode and equals methods
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1165 次 |
| 最近记录: |