You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@Id
@GeneratedValue(generator ="SharedPrimaryKeyGenerator")
@GenericGenerator(
name ="SharedPrimaryKeyGenerator",
strategy ="foreign",
parameters = [Parameter(name ="property", value ="bookLineItem")]
)
@Column(
name =ENTITY_B_ID,
unique =true,
nullable =false
)
var entityBId:Int?=null
@OneToOne
@PrimaryKeyJoinColumn
@JsonBackReference
var entityA:EntityA?=null
Custom generator:
classReactiveSharedPrimaryKeyGenerator: ReactiveIdentifierGenerator<Int> {
overridefungenerate(p0:ReactiveConnectionSupplier?, entity:Any?): CompletionStage<Int> {
returnCompletableFuture.supplyAsync {
val primaryKey = getPrimaryKey(entity!!)
primaryKey asInt?:throwIllegalStateException("Primary Key value is null")
}.toCompletableFuture()
}
/** * Returns the ID or the One2One parent by checking for the @PrimaryKeyJoinColumn field to retrieve the parent * and then searches for the field annotated with @Id to retrieve the ID of the parent*/fungetPrimaryKey(entity:Any?): Int? {
entity?.let {
var currentClass:Class<*> = it::class.java
while (currentClass !=null&& currentClass !=Any::class.java) {
currentClass.declaredFields.forEach { field ->val annotation = field.getAnnotation(PrimaryKeyJoinColumn::class.java)
annotation?.let { an ->
field.trySetAccessible()
val primaryKeyOwner = field.get(it)
primaryKeyOwner?.let { pk ->val primaryKeyField = pk::class.java.getDeclaredFields().find { pkF ->
pkF.getAnnotation(javax.persistence.Id::class.java) !=null
}
if (primaryKeyField !=null) {
primaryKeyField.trySetAccessible()
val pkId = primaryKeyField.get(pk)
return pkId asInt?
}
}
}
}
currentClass = currentClass.superclass
}
}
returnnull
}
}
If somebody wants to provide a test, we a similar use case in OneToOneMapsIdTest, except that it doesn't use a custom generator.
The text was updated successfully, but these errors were encountered:
Related to this question on Stack Overflow.
Entity Example:
Custom generator:
If somebody wants to provide a test, we a similar use case in OneToOneMapsIdTest, except that it doesn't use a custom generator.
The text was updated successfully, but these errors were encountered: