Hibernate performs incorrect insertion order for child objects with compound keys

I have a table that uses the insertion order (a bad obsolete design that I cannot change) and has temporary objects inserted in the wrong order. This table is called Medium, which is a child of Faith. When session.save(belief); is called session.save(belief); , the action is cascaded to child mediums, which are stored as a list in the belief class. Medium objects are stored in the appropriate order in the .getMeans () belief list, but after they are saved to the database, they are inserted in the order of their composite key. for example, if there are 3 medium objects that need to be inserted with the following order and compound keys:

 [1, 1], [1, 3], [1, 2] 

They will be inserted by ordered composite keys like this:

 [1, 1], [1, 2], [1, 3] 

Any idea as to what could be causing this? I thought that Hibernate should have inserted according to the order that they display on the List? I even tried running session.save () for each Medium entity individually, to find out if this would change, but it is not.

I appreciate your help!

EDIT: So, what I ended up doing is adding a new column to the col_index middle table, which contains the index of the Average column in the resulting matrix. I used the javax.persistence.OrderBy annotation with a new col_index column so that the list of medium objects is returned in the appropriate order. Although, this still does not fix the insertion order issue, and I would still like to get an answer for that. However, at the moment this solution will be enough.

+6
source share
2 answers

I think you should use @OrderColumn , not @OrderBy .

The @OrderBy annotation @OrderBy used when retrieving collection items, but the order is not supported by Hibernate.

@OrderColumn allows Hibernate to store the index of the item collection in the designated order column, which is then used when retrieving the collection.

+1
source

If you use the Hibernate annotation, you can use the @OrderBy (") annotation to indicate the order of your child objects. Please refer to this.

Maybe you should use a separate property to set the sorting and use of the property in @OrderBy.

0
source

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


All Articles