Answer the question
In order to leave comments, you need to log in
How to add new data to a table?
There is @Entity
in which it is stored as a property @ElementCollection Map<K,V>
.
How to make it so that when changing the Map through .setMap(...) only new rows are added to the table, while those that already exist in the table are not deleted.
Thank you very much in advance!
Answer the question
In order to leave comments, you need to log in
The setMap method should only be called if you've just created an instance and haven't saved it to the database yet. If the entity has already been saved in the database, or read from the database, then instead of the Map interface declared in the entity class, its implementation associated with the functions of working with the database is substituted. The JPA "engine" (usually Hibernate) "seeing" that such an implementation is in place of Map, as it were, "knows" that these are related records that are already stored in the database. If we replace the entire Map, then this "knowledge" is lost and unforeseen behavior begins.
To modify a list stored in a Map, call the Map.put and Map.remove methods:
entity.getMap().put(...);
entity.getMap().remove(child);
You can do the same as creating a "new folder" "new folder (1)" ... "new folder (3)"
i.e. add a check if there is such a key in the map, if there is, change the passed key value to key(n), check again if there is such a key, and if there is, change n to 1, and check again, and if not, then write key( 2)
z.s. if you are going to remember the keys somewhere, then you can return from the setMap method and return the "real" key, the one that turned out after renaming it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question