-
-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to declare @Id in interface? #54
Comments
There is no equivalent of The ideal way to do it as follows: @JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes(
value = [
JsonSubTypes.Type(CaoHistoryRecordNotification::class, name = "history-record-notification")
]
)
@Indices(value = [(Index(value = "caObjectLocalId", type = IndexType.NonUnique)),
(Index(value = "checked", type = IndexType.NonUnique))])
interface CaoNotification {
val localId: UUID
val caObjectLocalId: UUID
val checked: Boolean
val dateTime: LocalDateTime
val importance: CaoNotificationImportance
}
@InheritIndices
data class CaoHistoryRecordNotification(
@Id override val localId: UUID,
override val caObjectLocalId: UUID,
override val checked: Boolean,
override val dateTime: LocalDateTime,
override val importance: CaoNotificationImportance,
val localCaoHistoryRecordId: UUID
) : CaoNotification
private val repository = nitrite.getRepository<CaoHistoryRecordNotification>() See issue #37 for |
Thanks for quick reply! But what If I have multiple data classes extending |
Yes you can not create repository of interfaces. You can have a repository of abstract class. But remember all index fields and id fields can not be abstract in that class. Here is an example which I have added in the test cases also interface MyInterface {
val id: UUID
}
@Indices(value = [(Index(value = "name", type = IndexType.NonUnique))])
abstract class SomeAbsClass (
@Id override val id: UUID = UUID.randomUUID(),
open val name: String = "abcd"
) : MyInterface {
abstract val checked: Boolean
}
@InheritIndices
class MyClass(
override val id: UUID,
override val name: String,
override val checked: Boolean) : SomeAbsClass(id, name)
@InheritIndices
class MyClass2(
override val id: UUID,
override val name: String,
override val checked: Boolean,
val importance: Int
) : SomeAbsClass(id, name)
@Test
fun testIssue54() {
val repository = db?.getRepository<SomeAbsClass>()!!
assertTrue(repository.hasIndex("id"))
assertTrue(repository.hasIndex("name"))
val item = MyClass(UUID.randomUUID(), "xyz", true)
var writeResult = repository.insert(item)
assertEquals(writeResult.affectedCount, 1)
var cursor = repository.find()
assertEquals(cursor.size(), 1)
val item2 = MyClass2(UUID.randomUUID(), "123", true, 3)
writeResult = repository.insert(item2)
assertEquals(writeResult.affectedCount, 1)
cursor = repository.find()
assertEquals(cursor.size(), 2)
} |
Interface:
Data class:
But there is a problem,
@Id
annotation must be applied to fields only. How can I overcome it?Without such annotation, Nitrite can't find id field for object when I trying to
update
repoOr repository of interfaces isn't supported?
The text was updated successfully, but these errors were encountered: