• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

implementing fetching strategy

 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

As I read through the book Java Persistence with Hibernate, I come to the chapter 13 "Optimizing fetching and caching".

It describes on page 578 that one can use the following annotation on collections:

@OneToMany
@org.hibernate.annotations.Fetch(org.hibernate.annotations.FetchMode.SUBSELECT)
private Set<Bid> bids = new HashSet<Bid>();

In fact, on page 589 the author encourages the use of FetchMode.SUBSELECT as the preferred fetching strategy:
If you use bag collections (they are the default @OneToMany collection in Java Persistence), don't enable a fetching strategy that results in products. Use subselect or immediate secondary-select fetching for parellel eager fetching for bag collections.


For my project I have numerous collections where I'd like to make use of this optimization, for instance in my Company class I wrote the following:

@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, mappedBy="company")
@org.hibernate.annotations.Fetch(@org.hibernate.annotations.FetchMode.SUBSELECT)
@org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@org.hibernate.annotations.CollectionOfElements
@JoinTable(name = "ADMINISTRATORS", joinColumns = @JoinColumn(name = "USER_ID"))
private Collection<User> ADMINISTRATORS = new ArrayList<User>();

Well, guess what! When I go to compile I get the following:

[javac] C:\Scholastic_Hibernate\src\java\scholastic\models\Company.java:53:
annotation not valid for a value of type org.hibernate.annotations.FetchMode
[javac] @org.hibernate.annotations.Fetch(@org.hibernate.annotations.Fetch
Mode.SUBSELECT)

What does that mean? I looked it up in the javadocs and SUBSELECT is one of the constants listed for org.hibernate.annotations.FetchMode. So what gives?

Alan
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but look at

@org.hibernate.annotations.Fetch(@org.hibernate.annotations.FetchMode.SUBSELECT)

again. I think you will want to remove that second "@" sign.

to

@org.hibernate.annotations.Fetch(org.hibernate.annotations.FetchMode.SUBSELECT)




Good Job.

Mark
 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaagh! Stupid, stupid, stupid.

That was it. Sorry 'bout that.

Alan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic