1

I thought I understood hibernate's fetching strategies, but it seems I was wrong.

So, I have an namedNativeQuery:

@NamedNativeQueries({
  @NamedNativeQuery(
    name = "getTest",
    resultClass = ArticleOnDate.class,
    query = "SELECT `a`.`id` AS `article_id`, `a`.`name` AS `name`, `b`.`price` AS `price` FROM article a LEFT JOIN price b ON (a.id = b.article_id) WHERE a.date <= :date"
  )
 })
 @Entity()
 @Immutable
 public class ArtikelOnDate implements Serializable {  
   @Id
   @OneToOne
   @JoinColumn(name = "article_id")
   private Article article;  
 ...  
 }

Then I call it:

Query query = session.getNamedQuery("getTest").setDate("date", date);
List<ArticleOnDate> list = (List<ArticleOnDate>) query.list();

The query returns thousand of entities... Well, ok, but after that query hibernate queries thousand other queries:

Hibernate: 
    select
        article0_.id as id1_0_0_,
        article0_.bereich as name2_0_0_,
        price1_.price as price1_14_1_
    from
        article artikel0_ 
    where
        artikel0_.id=?

Ok, that's logic, because the @OneToOne relation is fetched eagerly. I don't want to fetch it lazy, so I want a batch fetching strategy.

I tried to annotate the Article property but it didn't work:

@Id
@OneToOne
@JoinColumn(name = "article_id")
@BatchSize(size=100)
private Article article;  

So what can I do to fetch the relation in a batch?

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Roman B
  • 81
  • 7

0 Answers0