How to save a HashMap with sleep mode

Hi, I am very new to the world of sleep mode and it seems that I got into the checkpoint. The object I need to save hashhap.

private Map<String, SentimentFrequencyCounts> modelData = null; 

The fact is that I will never have to search, sort, or do anything with this card. I just need to save it with the object and load it when the object is loaded, so I was hoping there was some way that sleep mode could just serialize it and then save it in the CLOB or BLOB field, but I cannot find way to do it.

So, I tried hibernate hibernate this way

  @OneToMany(mappedBy="ngram_data", fetch = FetchType.EAGER) @MapKey(name = "attributeName") public Map<String, SentimentFrequencyCounts> getModelData() { return modelData; } 

But this gives me the following exception at runtime org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class:

The SentimentFrequencyCounts class is the inner class of the one I'm trying to save. So basically I think I really don't understand how sleep mode for hashmap works. Actually a shame I can't just get him to serialize this and combine it into one column.

Thanks in advance for your help and time.

+3
source share
2 answers

Remove the existing annotations and annotate the list with @Lob - indicates that the persistent property or field should be saved as a large object for the database - a supported large type of object .

If the variable type was a subtype of Serializable, you can simply leave annotations at all; JPA rules for default mappings indicate that types that are Serializable rather than primitive or Embeddable are serialized and stored in BLOB columns. However, List is not Serializable, although ArrayList.

You can use @Lob with @ElementCollection, but I'm not sure what the result is; I don't know if this serializes the entire list or creates a table in which each list item is serialized separately. You probably are not interested in this anyway.

MUCH LAST CHANGES: Of course, as the specifier studies hard, this annotation only works for fields of the Serializable type, and not for fields that simply contain objects of the Serializable class. Therefore, to do this work, you have to do fraud. I looked to see if it is possible to do something smart with a common wildcard limited by the type of intersection, but I don't think you can. You can, however, write a small class as follows:

 class SerializableInstanceOf<T> implements Serializable { public final T instance; public SerializableInstanceOf(T instance) { Serializable s = (Serializable)instance; this.instance = instance; } } 

And use this as a holder for the list - the object has a field of this type, marked with @Lob, and stores the link to the list in it. Each time you want to work with a list, you go through the instance field, possibly using the getList method for the object.

This is ugly, but it should allow you to do what you need.

+1
source
 @org.hibernate.annotations.Type( type = "org.hibernate.type.SerializableToBlobType", parameters = { @Parameter( name = "classname", value = "java.util.HashMap" ) } ) public Map<String, SentimentFrequencyCounts> getModelData() { return modelData; } 

Or even this will work in most cases (distributed caching can be a problem):

 @org.hibernate.annotations.Type( type = "org.hibernate.type.SerializableType" ) public Map<String, SentimentFrequencyCounts> getModelData() { return modelData; } 
+1
source

Source: https://habr.com/ru/post/1343243/


All Articles