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

Does the Hibernate API support a collection result other than a list?

For example, I have a process that runs hundreds of thousands of iterations to create some data for a client. This process uses entries from a table of values ​​(for example) to generate this output for each iteration.

With a list, I will have to iterate over the entire list to find a specific value that is expensive. I would like to be able to return the TreeMap and specify the key programmatically so that I can search the collection for the specific value that I need. Can Hibernate do this for me?

+3
java hibernate
source share
4 answers

I assume that you are referencing the Query.list() method. If yes: no, there is no way to return top-level results other than List . If you get too many results, why not issue a more limited database query? If the request is hard to contain, you can populate your own Map contents of the Hibernate List and then drop the list.

+2
source share

If I understand correctly, you load a bunch of data from the database into memory and then use it locally, looking for specific objects in this list.

If so, I see 2 options.

  • Do not download all the data, but for each iteration, refer to the database with a query that returns only the specific record that you need. This will make more database queries, so it will probably be slower, but with much less memory consumption. This solution can be easily improved by adding a cache, so that most of the values ​​used will be obtained quickly. Of course, this will require some measure of performance, but I usually prefer a naive solution with good caching, as the cache can be implemented as a cross anxiety and be very transparent to the programmer.
  • If you really want to load all your data into memory (which is actually a form of caching), the time to convert your data from a list to a TreeMap (or any other efficient structure) is likely to be small compared to the full processing. This way you can do the data conversion yourself.

As I said, in general, I would prefer a solution with caching ...

+2
source share

From Saving Java with Hibernate :

  • Map java.util.Map can be mapped to <map> , saving key and value pairs. Use java.util.HashMap to initialize the property.
  • A java.util.SortedMap can be displayed with a <map> element and the sorting attribute can be set as a comparator or in-memory. Initialize collections using java.util.TreeMap instance.
+1
source share

Yes, it can be done.

However, you may have to implement the Comparable domain Comparable ; I do not think you can do this with a comparator.

Edit: Looks like I did not understand this question. If you are talking about the result of a special request, the above will not help you. Perhaps it will be possible to make it work by binding the object with the TreeMap property to the database view if the request is corrected.

And, of course, you can always create a map yourself with a very small amount of work and processing.

+1
source share

All Articles