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;
}

`

1

1 Answer 1

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.

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

1 Comment

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.

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.