Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate object with new boolean field

My Spring-Boot & Couchbase app has a Cat object in the DB.

In a new app version, we added new boolean field to out Cat document object:

@RequiredArgsConstructor
@AllArgsConstructor(onConstructor = @__(@PersistenceConstructor))
@Document
@Data
@Builder
@EqualsAndHashCode
public class Cat {
....
@Field
final boolean isHungry

But now, we already have Cat objects that are in the DB and don't have this field.

When the app is trying to read these Cats we get this error:

org.springframework.data.mapping.model.MappingInstantiationException: Failed 
  to instantiate com.example.Cat using constructor public 
  com.example.Cat(...) with arguments ... 
...
Caused by: java.lang.IllegalArgumentException: Parameter isHungry must not be null!

Isn't there a way to tell Spring that if the field is missing in the DB, it should use a default value (false in this case)

like image 514
riorio Avatar asked Jun 05 '26 01:06

riorio


1 Answers

Try making a constructor with all parameters except isHungry, and set isHungry to false

like image 196
aBnormaLz Avatar answered Jun 06 '26 13:06

aBnormaLz