Details
-
Type:
Bug
-
Status: Closed
-
Priority:
Minor
-
Resolution: Fixed
-
Affects Version/s: 2.1 M1 (Lovelace), 1.10.10 (Ingalls SR10), 2.0.5 (Kay SR5)
-
Fix Version/s: 1.10.11 (Ingalls SR11), 2.1 M2 (Lovelace), 2.0.6 (Kay SR6)
-
Component/s: Repository
-
Labels:None
-
Environment:Ubuntu 17.10 OpenJDK 1.8
Description
Let us have a simple inheritance hierarchy:
open class Animal class Bird(val color: String): Animal()
And let us make a repository for all Animals:
@Repository interface AnimalRepository: MongoRepository<Animal, String>
The problem is that if we save Birds to this repository, they are saved in either the "Animals" mongo collection, or the "Birds" collection, depending on whether we use MongoRepository::save or MongoRepository::saveAll:
@RunWith(SpringRunner::class) @SpringBootTest class DemoApplicationTests { @Autowired lateinit var animalRepository: AnimalRepository @Before fun cleanup() { animalRepository.deleteAll() } @Test fun testNormal() { animalRepository.save(Bird("green")) // Saves to "Animal" collection as expected animalRepository.saveAll(listOf(Bird("red"))) // Saves to "Bird" collection even though it should go into the "Animal" collection val count = animalRepository.count() // Counts only the documents in the "Animal" collection assertEquals(2, count) // count == 1 } }
This would not be a problem in itself, but the retreival functions always work on the more general repository ("Animal" in this case).