spring-projects / spring-data-mongodb

Provides support to increase developer productivity in Java when using MongoDB. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-mongodb/
Apache License 2.0
1.62k stars 1.09k forks source link

Object Mapping error #4646

Closed myob-hankun closed 8 months ago

myob-hankun commented 8 months ago

Hi Team, I upgraded springboot to 3. spring-boot-starter-data-mongodb to 3.2.2. jdk to 21. And mongo is 5. So I think there is no problem with compatibility I have some errors.

And I have a java object

@Data
@Document
@Accessors(chain = true)
public class PublishedReportPack {
    @Id
    public String id;
    private Map<String, PublishedReportPackReport> reports;
}

PublishedReportPackReport is

@Data
@Accessors(chain = true)
public class PublishedReportPackReport {
    private ChartCollection charts;
    private String status;
}

ChartCollection is a basic class. it has a child class

@Accessors(chain = true)
@Data
@Document
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@JsonIdentifyingField("revenueSummary")
public class SalesCollection extends ChartCollection {
   private ChartDTO<List<LineChartDTO>> ytdRevenue;
}

TopRevenueContributingItemDTO is

@Data
@Accessors(chain = true)
public class TopRevenueContributingItemDTO {
    private BigDecimal percentageOfTotal;
    private int rank;
}

ChartDTO is

@Data
@Accessors(chain = true)
public class ChartDTO<T> {
    private T series;
    private List<String> label = new ArrayList<>();
}

LineChartDTO is

public class LineChartDTO {
    private String name;
    private List<Double> data;
}

Currently, when I use findAllById(), it has an error. org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.math.BigDecimal] for value [Food]

The Food is the name field of the LineChartDTO class. TopRevenueContributingItemDTO also has this error.

I think it should be of string type directly, but Mongo wants to map Food to BigDecimal, and the name field is defined as String instead of BigDecimal. Why does this happen?

mp911de commented 8 months ago

We have a hard time following what is happening here. Please provide a minimal reproducer or at least the MongoDB document as JSON so that we can understand the initial setup.

christophstrobl commented 8 months ago

Thank you @myob-hankun for the json. However there are still some bits (like the ChartCollection or TableDTO) missing. If you'd like us to spend some time investigating, please take the time to provide a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem.