Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug found by using Videoshop: Using Spring data for Ordering #151

Open
der-imp opened this issue Nov 14, 2016 · 4 comments
Open

Bug found by using Videoshop: Using Spring data for Ordering #151

der-imp opened this issue Nov 14, 2016 · 4 comments

Comments

@der-imp
Copy link

der-imp commented Nov 14, 2016

Try to use Spring data for sorting.
Iterable<Disc> findAllByOrderByPriceAsc();
Attribute price uses javax.money.MonetaryAmount

At start log notification shows the possible problem with MonetaryAmount

2016-11-14 14:34:01.317 WARN 1410 --- [ restartedMain] o.h.t.d.java.JavaTypeDescriptorRegistry : Could not find matching type descriptor for requested Java class [javax.money.MonetaryAmount]; using fallback

@odrotbohm
Copy link
Member

The log message you see "should" appear whether you try the above code or not. It's a glich in HIbernate that has been reported before that shouldn't be affecting the actual functionality of the application.

I am not quite getting the first part of your report. What's the actual problem? Do you get an exception?

MonetaryAmount is a tricky thing to sort by as there's no such thing as a natural order to the values. This mostly stems from the fact that without information about exchange rates, you simply cannot decide whether €1 is greater or less than $1. Especially not on the database level where all you have is exactly those values represented as String.

@odrotbohm odrotbohm added the lifecycle: feedback requested Information missing label Nov 18, 2016
@der-imp
Copy link
Author

der-imp commented Nov 18, 2016

That MonetaryAmount is represented as String on database layer was my suggestion to. Thats answer, why the ordering ist not correct.

Than i reformat my question a little bit. Can i sort over the Price using spring data. So that
Iterable<Disc> findAllByOrderByPriceAsc(); give me the correct/ expected order. Otherwise i could extend the videoshop example an do ordering in the service layer.

@odrotbohm
Copy link
Member

This came up in the tutors' meeting today. Once we move to Streamable as return type, client side sorting like this is possible:

discs.findByType(type).stream()
  .sorted(Disc.BY_PRICE) // that being a Comparator.comparing(Disc::getPrice)
  .collect(Collectors.toList())

That could even be hidden inside a default method in e.g. VideoShop's VideoCatalog. That said, users would still have to detect that sorting via the general findBy…(…, Sort sort) doesn't properly work for Sort instances referencing MonentaryAmount fields.

Another option would be to Jadira Usertypes and its support for Moneta. We'd have to explore how intensive the ripple effects of that are. Every MonetaryAmount field in the students' code would have to be equipped with it which creates noise and we currently don't even have an example that'd document this need.

/cc @martinmo

@odrotbohm odrotbohm removed the lifecycle: feedback requested Information missing label Oct 25, 2021
@martinmo
Copy link
Contributor

I think the first options is a good intermediate solution. It could be included in a minor release, existing code should work without changes after a recompile (Streamable just extends Iterable).

Have not looked into Jadira Usertypes yet :-/ how does "equipping" a field with it look like? Is it just an annotation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants