I´m writing a sample project for my personal blog about how to use Spring Boot from scratch. At this moment trying to save my entities on a H2 database, I am receiving this mapping exception from the class BulkController which is RestController class trying to save instances of my Document POJO class.
Here is the code of GET method using double brace initialization:
Iterable<Document> records = csvParser.getRecords()
.stream()
.skip(1)
.map(record -> new Document() {{
setSource(record.get(0));
setCodeListCode(record.get(1));
setCode(record.get(2));
setDisplayValue(record.get(3));
setLongDescription(record.get(4));
}})
.collect(Collectors.toList());
documentRepository.saveAll(records);
Checking the raised exception:
org.hibernate.MappingException: Unknown entity: com.josephrodriguez.learning.springboot.controller.BulkController$1
at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:704) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1608) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:149) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:70) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:99) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
Some part of me felt some curiosity to check the class of Iterable items.
for (Document d: records) {
System.out.println(d.getClass());
}
As I suspected, this was the output:
class com.josephrodriguez.learning.springboot.controller.BulkController$1
Removing the use of map function everything works fine.
Question: Why the map function with the double brace initialization changes the class type of the expected items of the collection?