Home > Blockchain >  Spring Boot Entity model member converter
Spring Boot Entity model member converter

Time:02-18

Here my problem, I have to store serialized website's cookie information in DB, and I tried to write a simple PesistentConverter to it can be convert Map to String and vice-versa. But the IDE show a warning about this:

'Basic' attribute type should not be a map

Here my Entity:

@Entity
@Table(name = "website")
public class Website implements Serializable {

    @Id
    @Column(name = "name", length = 16, nullable = false)
    @NotNull
    private String name;

    @Column(name = "serialized_cookie_map", nullable = false, length = 2048)
    @Convert(converter = CookieMapPersistenceConverter.class)
    @NotNull
    private Map<String,String> serializedCookieMap;
    
    ...
}

CookieMapPersistenceConverter:

public class CookieMapPersistenceConverter implements AttributeConverter<Map<String, String>, String> {
    @Override
    public String convertToDatabaseColumn(Map<String, String> stringStringMap) {
        return stringStringMap.toString();
    }

    @Override
    public Map<String, String> convertToEntityAttribute(String s) {
        ObjectMapper mapper = new ObjectMapper();
        TypeFactory typeFactory = mapper.getTypeFactory();
        MapType mapType = typeFactory.constructMapType(HashMap.class, String.class, String.class);
        HashMap<String,String> convertedMap = null;
        try {
            convertedMap = mapper.readValue(s, mapType);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return convertedMap;

    }
}

I'm using the 2.6.3 version of Spring Boot.

Many thanks your time!

CodePudding user response:

According to this:

Any attributes that have no other annotations and do not reference other entities will be automatically mapped as basic.

and according to the table from the above link map is not a basic type. so IDE is right about

'Basic' attribute type should not be a map

I suggest try the following approach to see it satisfies your requirements or not.

  @Id
  @Column(name = "id", length = 16, nullable = false)
  @NotNull
  private String id;

  @ElementCollection
  @MapKeyColumn(name="property")
  @Column(name="value")
  @CollectionTable(name="cookies", joinColumns=@JoinColumn(name="id"))
  @NotNull
  private Map<String,String> serializedCookieMap;
  //setter and getter

and the code for repositry:

@Repository
public interface WebsiteRepository extends CrudRepository<Website, String> {}

code I did for test:

Website w = new Website();
w.setId("id");
Map<String, String> m = new HashMap<>();
m.put("key1", "value1");
m.put("key2", "value2");
m.put("key3", "value3");
w.setSerializedCookieMap(m);
repo.save(w);

which creates two table named WEBSITE and COOKIES and the content of each is like this:

WEBSITES:
ID  
id

and:

COOKIES:
ID      VALUE   PROPERTY  
id  value1  key1
id  value2  key2
id  value3  key3

if you insist to store Serialized version of map take a look at this

CodePudding user response:

I have tried same thing in my project (with same version of spring boot). I did not notice any warning.

But here are some suggestions...

  1. The field you have annotated @convert not meant to be persisted in DB. So instead of @column try using @Transient.
  2. You can also try using @Type( type = "json" ) instead of @convert.
  3. Also this can be possibly issue with IDE, so just to ignore it as a warning you can use @SuppressWarnings("JpaAttributeTypeInspection") annotation.
  • Related