I've tried to declare data entities for my mongo db and all I got is a null
in a nested (via reference) collection.
That's what I want to see:
My tries represented in this repository.
I've tried to declare data entities for my mongo db and all I got is a null
in a nested (via reference) collection.
That's what I want to see:
My tries represented in this repository.
You are not joining the two collections.
See: https://micronaut-projects.github.io/micronaut-data/latest/guide/#mongoJoinQueries
Note the @Join
can also go on the repository class.
@MongoRepository(databaseName = "test")
@Join("bananas-1")
interface FruitRepoV1 : CrudRepository<FruitV1, String>
UPDATE:
Fruit class and repository
@MappedEntity
data class Fruit(
val name: String,
@Relation(value = Relation.Kind.ONE_TO_MANY, mappedBy = "fruit")
//mappedBy refers to Banana::fruit property not the MongoDB collection
val bananas: MutableSet<Banana> = mutableSetOf()
) {
@field:Id
@GeneratedValue(GeneratedValue.Type.IDENTITY)
var id: String = ""
}
@MongoRepository
@Join("bananas.fruit")
// with collection need to state the property in Fruit
// and then the property in the concrete class "Banana" to join on
interface FruitRepository : CrudRepository<Fruit, String> {
override fun findAll() : List<Fruit>
fun updateNameById(@Id id : String, name : String)
}
Banana class and repository
@MappedEntity
data class Banana(
val name: String,
@Relation(Relation.Kind.MANY_TO_ONE)
val fruit: Fruit
) {
@field:Id
@GeneratedValue(GeneratedValue.Type.IDENTITY)
var id: String = ""
}
@MongoRepository
interface BananaRepository : CrudRepository<Banana, String> {
override fun findAll(): List<Banana>
fun findByFruitName(name : String) : List<Banana>
}
Test case
@MicronautTest
class FruitTests (
private val fruitRepository: FruitRepository,
private val bananaRepository: BananaRepository) : StringSpec({
"tests" {
val fruitBowl = Fruit("bowl of bananas")
fruitRepository.save(fruitBowl)
bananaRepository.save(Banana("1", fruitBowl))
bananaRepository.save(Banana("2", fruitBowl))
bananaRepository.save(Banana("3", fruitBowl))
val fruits = fruitRepository.findAll()
fruits.count() shouldBe 1
val fruit = fruits.first()
fruit.name shouldBe "bowl of bananas"
fruit.bananas.size shouldBe 3
bananaRepository.findByFruitName("bowl of bananas").size shouldBe 3
fruitRepository.updateNameById(fruit.id, "bananas")
fruitRepository.findById(fruit.id).get().name shouldBe "bananas"
bananaRepository.findByFruitName("bananas").size shouldBe 3
}
})
Update 2:
@MappedEntity
data class Student(
@field:Id
@GeneratedValue(GeneratedValue.Type.IDENTITY)
val id: String?,
val name: String,
@Relation(value = Relation.Kind.MANY_TO_MANY, cascade = [Relation.Cascade.PERSIST])
val courses : List<Course>
) {
constructor(name: String, courses: List<Course>) : this(null, name, courses)
}
@MongoRepository
@Join("courses")
interface StudentRepository : CrudRepository<Student, String>
@MappedEntity
data class Course(
@field:Id
@GeneratedValue(GeneratedValue.Type.IDENTITY)
val id: String?,
val name: String
) {
constructor(name: String) : this(null, name)
}
@MongoRepository
interface CourseRepository : CrudRepository<Course, String>
Test
@MicronautTest
class StudentTests(
private val studentRepository: StudentRepository,
private val courseRepository: CourseRepository
) : StringSpec( {
"tests" {
courseRepository.count() shouldBe 0
studentRepository.save(
Student("me",
listOf(
Course("Micronaut"),
Course("MongoDB"))
)
)
val students = studentRepository.findAll()
students.count() shouldBe 1
println(students)
val student = students.first()
println(student)
student.name shouldBe "me"
student.courses.size shouldBe 2
courseRepository.count() shouldBe 2
}
})