Can Hibernate return a collection of result objects OTHER than a List?

后端 未结 4 1806
無奈伤痛
無奈伤痛 2020-12-11 22:17

Does the Hibernate API support object result sets in the form of a collection other than a List?

For example, I have process that runs hundreds of thousands of iter

相关标签:
4条回答
  • 2020-12-11 22:36

    From Java Persistence with Hibernate:

    • A java.util.Map can be mapped with <map>, preserving key and value pairs. Use a java.util.HashMap to initialize a property.
    • A java.util.SortedMap can be mapped with <map> element, and the sort attribute can be set to either a comparator or natural ordering for in-memory sorting. Initialize the collection with a java.util.TreeMap instance.
    0 讨论(0)
  • 2020-12-11 22:39

    Yes, that can be done.

    However, you'll probably have to have your domain class implement Comparable; I don't think you can do it using a Comparator.

    Edit: It seems like I misunderstood the question. If you're talking about the result of an ad hoc query, then the above will not help you. It might be possible to make it work by binding an object with a TreeMap property to a database view if the query is fixed.

    And of course you can always build the map yourself with very little work and processing overhead.

    0 讨论(0)
  • 2020-12-11 22:47

    If I understand correctly, you load a bunch of data from the database to memory and then use them locally by looking for certain objects in that list.

    If this is the case, I see 2 options.

    1. Dont load all the data, but for each iteration access the database with a query returning only the specific record that you need. This will make more database queries, so it will probably bu slower, but with much less memory consumption. This solution could easily be improved by adding cache, so that most used values will be gotten fast. It will of course need some performance measurement, but I usually favor a naive solution with good caching, as the cache can implemented as a cross-concern and be very transparent to the programmer.
    2. If you really want to load all your data in memory (which is actually a form of caching), the time to transform your data from a list to a TreeMap (or any other efficient structure) will probably be small compared to the full processing. So you could do the data transformation yourself.

    As I said, in the general case, I would favor a solution with caching ...

    0 讨论(0)
  • 2020-12-11 22:57

    I assume you are referring to the Query.list() method. If so: no, there is no way to return top-level results other than a List. If you are receiving too many results, why not issue a more constrained query to the database? If the query is difficult to constrain, you can populate your own Map with the contents of Hibernate's List and then throw away the list.

    0 讨论(0)
提交回复
热议问题