2

I want to know why we need to use both @Fetch(FetchMode.SELECT) and fetch = FetchType.LAZY .fetchMode.select itself tells that all association should be loaded as lazy then why another term? `

@OneToMany(fetch = FetchType.LAZY, mappedBy = "stock")
@Cascade(CascadeType.ALL)
@Fetch(FetchMode.SELECT)
@BatchSize(size = 10)
public Set<StockDailyRecord> getStockDailyRecords() {
return this.stockDailyRecords;
}

`

RSingh
  • 171
  • 2
  • 12
  • 1
    Possible duplicate of [difference between FetchMode and FetchType](http://stackoverflow.com/questions/25821718/difference-between-fetchmode-and-fetchtype) – CaringDev Jan 23 '16 at 14:39

1 Answers1

1

FetchType.LAZY: refers to when Hibernate will fetch the association and entities.
@Fetch(FetchMode.SELECT): refers to how Hibernate will fetch the association and entities.

Abdelhak
  • 8,299
  • 4
  • 22
  • 36
  • 2
    If `fetchType` is `LAZY` then this implicitly assumes that `FetchMode` will be `SELECT` (or `SUBSELECT` if you set it explicitly). But I can't imagine how e.g. `FetchType.LAZY` + `FetchMode.JOIN` can work together. If it is joined already, it is not lazy by definition. The same is for `FetchType.EAGER` + `FetchMode.SELECT`/`FetchMode.SUBSELECT`. So, the real answer I think is that `FetchMode` is actually an "extension" to `FetchType`. `FetchType.EAGER` and `FetchMode.JOIN` is actually exactly the same. And `FetchType.LAZY` can be one of two types: `FetchMode.SELECT` and `FetchMode.SUBSELECT`. – Ruslan Stelmachenko Nov 13 '19 at 19:15