How to JPA-declare a composite key with Quarkus?
Trying to use multiple @Id
annotations in an @Entity
class with Quarkus, results in the error:
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)
But first, after declaring
interface PropertyRepository : CrudRepository<Property, Pair<String, abc.model.Entity>>
Without the declaration above, there are no complaints, but no possibility to actively manage instances of Property
.
How to circumvent this error?
I'm dealing with two JPA Entities:
1. the first one named Entity
(don't mistake for the annotation)
2. the second one named Property
An Entity
can have 0..n instances of Property
. The code is as follows:
@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
}
@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
Declaring the composite primary key as an @EmbeddedId
as follows, does not solve the problem, as Quarkus currently doesn't allow other annotations than @Id
in this case:
@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
)
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)