JPA default fetch type

RNJ picture RNJ · Apr 12, 2012 · Viewed 32k times · Source

From my understanding @OneToOne and @ManyToOne JPA annotations do an eager fetch. I want these to be lazily loaded in my application, or at least hint at it (which is what hibernate defaults to). I have started to add the annotation fetch = FetchType.LAZY

eg

@ManyToOne(optional = false, fetch = FetchType.LAZY)

instead of

@ManyToOne(optional = false)

This is both tedious and error prone. Is there a way I can do this at an application level? In the persistence.xml perhaps?

Answer

Steve Ebersole picture Steve Ebersole · Oct 2, 2012

To date, I have chosen to have Hibernate follow the JPA spec in terms of mapping via annotations simply because I have not received any feature requests for making it configurable, which was surprising TBH. As you point out, since Hibernate 3.0 what you want has been the default set up when using hbm.xml mapping files.

Allowing this via configuration would not violate the spec as another answer suggested.

So long story short, no it is not possible today. Create a feature request if you'd like to see that be configurable.