Search code examples
javahibernatejpahibernate-mapping

Hibernate: store List as JSON using @JsonSerialize and Converter: Basic attribute should not be a container


I have an @Entity that has a List<List<Integer>> field:

@Entity
public class Game  {
  @Column
  List<List<Integer>> field;
}

I want to save it as JSON in a database via Hibernate. I tried:

 @JsonSerialize(converter = FieldConverter.class)
 @JsonDeserialize(converter = FieldDeconverter.class)
 private List<List<Integer>> field;
@Converter
public class FieldConverter extends StdConverter<List<List<Integer>>, String> {
    @Override
    public String convert(List<List<Integer>> field)  {
        return "test-field";
    }
}
@Converter
public class FieldDeconverter extends StdConverter<String, List<List<Integer>>> {
    @Override
    public List<List<Integer>> convert(String field)  {
        return null;
    }
}

but I get Basic attribute should not be a container error.

I also tried this:

public class FieldConverter1 implements AttributeConverter<List<List<Integer>>, String> {
    @Override
    public String convertToDatabaseColumn(List<List<Integer>> attribute) {
        return "test";
    }

    @Override
    public List<List<Integer>> convertToEntityAttribute(String dbData) {
        return null;
    }
}
@Column
@Convert(converter = FieldConverter1.class)
private List<List<Integer>> field;

but the error pesists:

enter image description here


Solution

  • Pretty strange issue if you've done the rest properly and tried restarting IDE and rebuilding the project.

    I copied your FieldConverter1 class and created the same field in my entity and got no error, I'm assuming that wen you run your project it works smoothly even if IDE shows that error.

    After doing some research, it appears to be known bug in some older versions of IntelliJ IDEA, but it's fixed in newer releases. It was mentioned here and here.

    If you can, try upgrading IntelliJ as it should fix that issue.
    You could also disable/edit or suppress that inspection if you just want to get rid of that error: IntelliJ inspection options For example, disable inspection just for that field using this annotation:

    @SuppressWarnings("JpaAttributeTypeInspection")