user1617013
user1617013

Reputation: 13

Java Persistence & Hibernate

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...

Upvotes: 0

Views: 262

Answers (2)

dcernahoschi
dcernahoschi

Reputation: 15250

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>).

Upvotes: 0

davioooh
davioooh

Reputation: 24706

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

Upvotes: 2

Related Questions