Skip to content

ClassCastException on element update #1093

@Srdjan-V

Description

@Srdjan-V

If a repository gets open by clearing it, the type of the db will stay Object causing class cast exceptions while deserializing.

    @Data
    @Entity
    @AllArgsConstructor
    @NoArgsConstructor
    public static class TestEntity {
        @Id
        String name;
        String data;
    }

    @Test
    void execute() {
        File file = new File(FileUtils.getTempDirectory(), "db_test");
        file.deleteOnExit();

        RocksDBModule storeModule = RocksDBModule.withConfig()
                .filePath(file)
                .build();

        JacksonMapper jacksonMapper = new JacksonMapper() {
            @Override protected <Target> Target convertFromDocument(Document source, Class<Target> type) {
                try {
                    return super.convertFromDocument(source, type);
                } catch (IllegalArgumentException | ObjectMappingException e) {
                    return null;
                }
            }
        };
        for (Module jacksonModule : List.of(
                new ParameterNamesModule(),
                new Jdk8Module(),
                new JavaTimeModule())) {
            jacksonMapper.registerJacksonModule(jacksonModule);
        }

        Nitrite nitrite = Nitrite.builder()
                .loadModule(storeModule)
                .loadModule(NitriteModule.module(jacksonMapper))
                .openOrCreate();


        ObjectRepository<TestEntity> repository = nitrite.getRepository(TestEntity.class, "K");

        // this will open a NitriteMap<Object, Object>
        repository.clear();
        
        repository = nitrite.getRepository(TestEntity.class, "K");
        repository.update(new TestEntity("SS", "S"), true);
        repository.update(new TestEntity("SS", "S"), true);
    }
class java.lang.Object cannot be cast to class java.util.List (java.lang.Object and java.util.List are in module java.base of loader 'bootstrap')
java.lang.ClassCastException: class java.lang.Object cannot be cast to class java.util.List (java.lang.Object and java.util.List are in module java.base of loader 'bootstrap')
	at org.dizitart.no2.index.SingleFieldIndex.addIndexElement(SingleFieldIndex.java:145)
	at org.dizitart.no2.index.SingleFieldIndex.write(SingleFieldIndex.java:73)
	at org.dizitart.no2.index.ComparableIndexer.writeIndexEntry(ComparableIndexer.java:70)
	at org.dizitart.no2.collection.operation.DocumentIndexWriter.writeIndexEntryInternal(DocumentIndexWriter.java:97)
	at org.dizitart.no2.collection.operation.DocumentIndexWriter.writeIndexEntry(DocumentIndexWriter.java:50)
	at org.dizitart.no2.collection.operation.WriteOperations.insert(WriteOperations.java:99)
	at org.dizitart.no2.collection.operation.WriteOperations.update(WriteOperations.java:204)
	at org.dizitart.no2.collection.operation.CollectionOperations.update(CollectionOperations.java:102)
	at org.dizitart.no2.collection.DefaultNitriteCollection.update(DefaultNitriteCollection.java:125)
	at org.dizitart.no2.repository.DefaultObjectRepository.update(DefaultObjectRepository.java:129)
	at org.dizitart.no2.repository.DefaultObjectRepository.update(DefaultObjectRepository.java:118)
	at io.github.srdjanv.Testdb.execute(TestApp.java:87)

Metadata

Metadata

Assignees

Labels

Type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions