Skip to content
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

In case of inheritance, MongoRepository.saveAll(…) does not insert elements into the aggregate collection [DATAMONGO-1896] #2798

Closed
spring-projects-issues opened this issue Mar 8, 2018 · 1 comment

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Mar 8, 2018

Martin Drozdik opened DATAMONGO-1896 and commented

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)


Affects: 2.1 M1 (Lovelace), 1.10.10 (Ingalls SR10), 2.0.5 (Kay SR5)

Referenced from: commits e4103ea, 14b4963, 5b7b690

Backported to: 2.0.6 (Kay SR6), 1.10.11 (Ingalls SR11)

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Mar 8, 2018

Oliver Drotbohm commented

That's fixed. Feel free to give the snapshots a try

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants