Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SortedMap Key Persistance in Hibernate

I'm hoping someone can help me with my hibernate issue as I've been banging my head against Google for around an hour without result.

So the issue is that I have a SortedMap in a class, using Integer as the Key (and its natural built-in compareTo method) and another class as the value type. I'm using the key to keep the user-defined order of the value type and trying to get Hibernate to persist this.

For whatever reason, Hibernate has defaulted to disregarding the key I have inputted and instead using the value type's primary key as the key instead. When I load my Map back out of the database all of my keys have been changed in this way.

Definition of the Map is shown below (I'm using annotation-style Hibernate);

@ManyToMany(cascade = CascadeType.ALL)
@MapKey
@Sort(type = SortType.NATURAL)
private SortedMap<Integer, Column> columnOrder;

I can't use the Column type to store the order itself as the Column may be used in many instances of the containing type, with a different key value each time. Any guidance would be most appreciated.

like image 255
DavidH Avatar asked Oct 27 '25 00:10

DavidH


1 Answers

So I found the answer after discovering this StackOverflow question with a similar issue: Sorted map of Java primitives using JPA2 and Hibernate?

By changing the @MapKey annotation to the @MapKeyColumn annotation, I was able to give Hibernate the instruction to persist the key in the column name I specified, as below;

@ManyToMany(cascade = CascadeType.ALL)
@MapKeyColumn(name = "hierarchyOrdering")
@Sort(type = SortType.NATURAL)
private SortedMap<Integer, Column> columnOrder;

Thanks for the help.

like image 188
DavidH Avatar answered Oct 29 '25 14:10

DavidH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!