I am trying to access mongoDb using mongoTemplate (spring version 3.2) with java 21 and getting either MappingInstantiationException or InaccessibleObjectException.
I am using the below to read data from Mongo.
List<AbcDocumentResult> result = mongoTemplate.find(query, AbcDocumentResult.class);
I am intermittently getting error as either
org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate com.abc.AbcDocumentResult using constructor NO_CONSTRUCTOR with arguments
or
java.lang.reflect.InaccessibleObjectException: Unable to make field transient java.lang.Object[] java.util.ArrayList.elementData accessible: module java.base does not "opens java.util" to unnamed module @41a810ee
My AbcDocumentResult.java is as below
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder(toBuilder = true)
@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
@Document(collection = "abc_collection")
public class AbcDocumentResult implements Serializable {
@JsonProperty(value = "_id")
@Field("_id")
private String id;
private Integer department;
@JsonProperty(value = "source_data")
@Field("source_data")
private List<XyzDocument> sourceDataDtoList;
@JsonProperty(value = "target_data")
@Field("target_data")
private List<XyzDocument> targetDataDtoList;
}
XyzDocument.java
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder(toBuilder = true)
@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
public class XyzDocument implements Serializable {
private Integer rank;
private String objectId;
private BigDecimal price;
private Integer quantity;
@Field("system_data_timestamp")
@JsonProperty("system_data_timestamp")
private Date systemDataTimestamp;
private String srcSysName;
}
The behavior is inconsistent. It was working fine till recently and stopped working. There was no code change done between now and then.
In local tried to start the server with java run time argument as ‘–add-opens java.base/java.util=ALL-UNNAMED’ and that is working. However, read that it is not the desired way of fixing the issue.
Yaser A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.