0

Can someone explain me why using the annotation @Fetch(FetchMode.SELECT) allows me to use Lists instead of Sets? Which is the difference by using other fetchmode types like SUBSELECT?

Here below a piece of exmple code:

class One{
   ..
   @Fetch(FetchMode.SELECT)
   @OneToMany(mappedBy="one", fetch = FetchType.EAGER, ... )
   private List<Something> listOne = new ArrayList<Something>();

   @Fetch(FetchMode.SELECT)
   @OneToMany(mappedBy="one", fetch = FetchType.EAGER, ... )
   private List<SomethingElse> listTwo = new ArrayList<SomethingElse>();
   ...
}

In this way it works but I'd like to know why....I found other discussion with alternatives solutions but this one wasn't the preferred one...

2
  • I think you're mixing two things up here. How Hibernate maps sets of records to a Java Collection objects is not really related to the Fetch mode. Fetch mode determines when and how associated records are fetched. In general you can map a OneToMany or ManyToMany to a List or a Set. List can be deceiving as it gives the illusion that the ordering is fixed, which it isn't unless you've included an ORDER BY in your query. From the top of my head Hibernate uses a LinkedHashSet for sets so the ordering is predictable there as well. Commented Aug 22, 2012 at 13:27
  • the order is not a problem for me. As I'm using old libs sometimes I need to have back Lists and not Sets(I don't want to migrate manually from set to list!)... Commented Aug 22, 2012 at 13:59

2 Answers 2

2

I suppose official documentation is the best place to find an answer to your question. Take a look to this link.

SELECT: use a select for each individual entity, collection, or join load JOIN

JOIN: use an outer join to load the related entities, collections or joins SUBSELECT

SUBSELECT: use a subselect query to load the additional collections

Sign up to request clarification or add additional context in comments.

3 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
I think the proper answer is indeed "RTFM", as politely answered by @davioooh . Just to make it clear: the second link is to the javadoc, which should be the best friend of a java developer. Stackoverflow is just the second best friend.
I hope you can do best than saying something known...I know what javadoc says....
0

You should be able to use List or Set as the interface for your collection, it has nothing to do with fetching.

@Fetch(FetchMode.SELECT) together with fetch = FetchType.EAGER means that after loading the One entity hibernate will immediately issue a second select to load list.

The @Fetch(FetchMode.SUBSELECT) without fetch = FetchType.EAGER means that hibernate will load the list with a subselect for all one enitities (they might also be part of a collection) when accessing the first list element(List<SomethingElse>).

3 Comments

this is what I was needing....thanks!But why if I use Lists insetad of sets without the @Fetch annotation gives me an error?!
no, should be OK. You might want to use a list instead of a set in want to preserve the order in the collection. Otherwise a set is more natural as it specify that the collection has no duplicates.
Which hibernate/Spring version are you using?!I'm using hibernate 4.0.0.CR6, spring 3.1.1...I've got an error when my app was loading before adding the @Fetch annotation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.