Skip to content

Commit 08f08b8

Browse files
committed
HHH-18190 - Remove @LazyCollection
1 parent 4309cff commit 08f08b8

File tree

30 files changed

+62
-1429
lines changed

30 files changed

+62
-1429
lines changed

documentation/src/main/asciidoc/userguide/chapters/fetching/Fetching.adoc

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -597,72 +597,3 @@ include::{extrasdir}/fetching-strategies-fetch-mode-join-example.sql[]
597597
====
598598

599599
This time, there was no secondary query because the child collection was loaded along with the parent entity.
600-
601-
[[fetching-LazyCollection]]
602-
=== `@LazyCollection`
603-
604-
The https://docs.jboss.org/hibernate/orm/{majorMinorVersion}/javadocs/org/hibernate/annotations/LazyCollection.html[`@LazyCollection`] annotation is used to specify the lazy fetching behavior of a given collection.
605-
The possible values are given by the `https://docs.jboss.org/hibernate/orm/{majorMinorVersion}/javadocs/org/hibernate/annotations/LazyCollectionOption.html[LazyCollectionOption]` enumeration:
606-
607-
`TRUE`:: Load it when the state is requested.
608-
`FALSE`:: Eagerly load it.
609-
`EXTRA`:: Prefer extra queries over full collection loading.
610-
611-
The `TRUE` and `FALSE` values are deprecated since you should be using the Jakarta Persistence {jpaJavadocUrlPrefix}FetchType.html[`FetchType`] attribute of the <<annotations-jpa-elementcollection>>, <<annotations-jpa-onetomany>>, or <<annotations-jpa-manytomany>> collection.
612-
613-
The `EXTRA` value has no equivalent in the Jakarta Persistence specification, and it's used to avoid loading the entire collection even when the collection is accessed for the first time.
614-
Each element is fetched individually using a secondary query.
615-
616-
[[fetching-LazyCollection-domain-model-example]]
617-
.`LazyCollectionOption.EXTRA` mapping example
618-
====
619-
[source, java, indent=0]
620-
----
621-
include::{example-dir-fetching}/LazyCollectionTest.java[tags=fetching-LazyCollection-domain-model-example]
622-
----
623-
====
624-
625-
[NOTE]
626-
====
627-
`LazyCollectionOption.EXTRA` only works for ordered collections,
628-
either List(s) that are annotated with @OrderColumn or Map(s).
629-
630-
For bags (e.g. regular List(s) of entities that do not preserve any certain ordering),
631-
the `@LazyCollection(LazyCollectionOption.EXTRA)` behaves like any other `FetchType.LAZY` collection
632-
(the collection is fetched entirely upon being accessed for the first time).
633-
====
634-
635-
Now, considering we have the following entities:
636-
637-
[[fetching-LazyCollection-persist-example]]
638-
.`LazyCollectionOption.EXTRA` Domain Model example
639-
====
640-
[source, java, indent=0]
641-
----
642-
include::{example-dir-fetching}/LazyCollectionTest.java[tags=fetching-LazyCollection-persist-example]
643-
----
644-
====
645-
646-
When fetching the `employee` collection entries by their position in the `List`,
647-
Hibernate generates the following SQL statements:
648-
649-
[[fetching-LazyCollection-select-example]]
650-
.`LazyCollectionOption.EXTRA` fetching example
651-
====
652-
[source, java, indent=0]
653-
----
654-
include::{example-dir-fetching}/LazyCollectionTest.java[tags=fetching-LazyCollection-select-example]
655-
----
656-
657-
[source, SQL, indent=0]
658-
----
659-
include::{extrasdir}/fetching-LazyCollection-select-example.sql[]
660-
----
661-
====
662-
663-
[WARNING]
664-
====
665-
Therefore, the child entities were fetched one after the other without triggering a full collection initialization.
666-
667-
For this reason, caution is advised since accessing all elements using `LazyCollectionOption.EXTRA` can lead to N + 1 query issue.
668-
====

hibernate-core/src/main/java/org/hibernate/annotations/LazyCollection.java

Lines changed: 0 additions & 47 deletions
This file was deleted.

hibernate-core/src/main/java/org/hibernate/annotations/LazyCollectionOption.java

Lines changed: 0 additions & 68 deletions
This file was deleted.

hibernate-core/src/main/java/org/hibernate/boot/model/internal/CollectionBinder.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
import org.hibernate.annotations.Formula;
4040
import org.hibernate.annotations.HQLSelect;
4141
import org.hibernate.annotations.Immutable;
42-
import org.hibernate.annotations.LazyCollection;
43-
import org.hibernate.annotations.LazyCollectionOption;
4442
import org.hibernate.annotations.LazyGroup;
4543
import org.hibernate.annotations.ListIndexBase;
4644
import org.hibernate.annotations.ListIndexJavaType;
@@ -150,7 +148,6 @@
150148
import static jakarta.persistence.AccessType.PROPERTY;
151149
import static jakarta.persistence.ConstraintMode.NO_CONSTRAINT;
152150
import static jakarta.persistence.ConstraintMode.PROVIDER_DEFAULT;
153-
import static jakarta.persistence.FetchType.EAGER;
154151
import static jakarta.persistence.FetchType.LAZY;
155152
import static org.hibernate.boot.model.internal.AnnotatedClassType.EMBEDDABLE;
156153
import static org.hibernate.boot.model.internal.AnnotatedClassType.NONE;
@@ -1559,21 +1556,8 @@ private void setHibernateFetchMode(org.hibernate.annotations.FetchMode fetchMode
15591556
@SuppressWarnings("deprecation")
15601557
private void handleLazy() {
15611558
final FetchType jpaFetchType = getJpaFetchType();
1562-
final LazyCollection lazyCollectionAnnotation = property.getDirectAnnotationUsage( LazyCollection.class );
1563-
if ( lazyCollectionAnnotation != null ) {
1564-
final LazyCollectionOption option = lazyCollectionAnnotation.value();
1565-
boolean eager = option == LazyCollectionOption.FALSE;
1566-
if ( !eager && jpaFetchType == EAGER ) {
1567-
throw new AnnotationException("Collection '" + safeCollectionRole()
1568-
+ "' is marked 'fetch=EAGER' and '@LazyCollection(" + option + ")'");
1569-
}
1570-
collection.setLazy( !eager );
1571-
collection.setExtraLazy( option == LazyCollectionOption.EXTRA );
1572-
}
1573-
else {
1574-
collection.setLazy( jpaFetchType == LAZY );
1575-
collection.setExtraLazy( false );
1576-
}
1559+
collection.setLazy( jpaFetchType == LAZY );
1560+
collection.setExtraLazy( false );
15771561
}
15781562

15791563
private FetchType getJpaFetchType() {

hibernate-core/src/main/java/org/hibernate/boot/models/HibernateAnnotations.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,6 @@ public interface HibernateAnnotations {
366366
JoinFormula.class,
367367
JoinFormulaAnnotation.class
368368
);
369-
OrmAnnotationDescriptor<LazyCollection, LazyCollectionAnnotation> LAZY_COLLECTION = new OrmAnnotationDescriptor<>(
370-
LazyCollection.class,
371-
LazyCollectionAnnotation.class
372-
);
373369
OrmAnnotationDescriptor<LazyGroup, LazyGroupAnnotation> LAZY_GROUP = new OrmAnnotationDescriptor<>(
374370
LazyGroup.class,
375371
LazyGroupAnnotation.class

hibernate-core/src/main/java/org/hibernate/boot/models/annotations/internal/LazyCollectionAnnotation.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/fetch/FetchingTest.java

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,6 @@ public void testLazy() {
4747
s.close();
4848
}
4949

50-
@Test
51-
public void testExtraLazy() {
52-
Session s;
53-
Transaction tx;
54-
s = openSession();
55-
tx = s.beginTransaction();
56-
Person p = new Person( "Gavin", "King", "JBoss Inc" );
57-
Stay stay = new Stay( p, new Date(), new Date(), "A380", "Blah", "Blah" );
58-
p.getOrderedStay().add( stay );
59-
s.persist( p );
60-
tx.commit();
61-
s.clear();
62-
tx = s.beginTransaction();
63-
p = (Person) s.createQuery( "from Person p where p.firstName = :name" )
64-
.setParameter( "name", "Gavin" ).uniqueResult();
65-
assertFalse( Hibernate.isInitialized( p.getOrderedStay() ) );
66-
assertEquals( 1, p.getOrderedStay().size() );
67-
assertFalse( Hibernate.isInitialized( p.getOrderedStay() ) );
68-
assertEquals( "A380", p.getOrderedStay().get(0).getVessel() );
69-
assertFalse( Hibernate.isInitialized( p.getOrderedStay() ) );
70-
s.remove( p );
71-
tx.commit();
72-
s.close();
73-
}
74-
7550
@Test
7651
public void testHibernateFetchingLazy() {
7752
try(Session s = openSession()) {
@@ -95,7 +70,7 @@ public void testHibernateFetchingLazy() {
9570
.setParameter( "name", "Gavin" ).uniqueResult();
9671
assertFalse( Hibernate.isInitialized( p.getOldStays() ) );
9772
assertEquals( 1, p.getOldStays().size() );
98-
assertFalse( "lazy extra is failing", Hibernate.isInitialized( p.getOldStays() ) );
73+
assertTrue( Hibernate.isInitialized( p.getOldStays() ) );
9974
s.clear();
10075
stay = (Stay) s.get( Stay.class, stay.getId() );
10176
assertTrue( !Hibernate.isInitialized( stay.getOldPerson() ) );

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/fetch/Person.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77

88
//$Id$
99
package org.hibernate.orm.test.annotations.fetch;
10+
1011
import java.io.Serializable;
1112
import java.util.ArrayList;
1213
import java.util.Collection;
1314
import java.util.Date;
1415
import java.util.List;
16+
17+
import org.hibernate.annotations.Fetch;
18+
import org.hibernate.annotations.FetchMode;
19+
1520
import jakarta.persistence.CascadeType;
1621
import jakarta.persistence.Entity;
1722
import jakarta.persistence.FetchType;
@@ -21,11 +26,6 @@
2126
import jakarta.persistence.OrderColumn;
2227
import jakarta.persistence.Table;
2328

24-
import org.hibernate.annotations.Fetch;
25-
import org.hibernate.annotations.FetchMode;
26-
import org.hibernate.annotations.LazyCollection;
27-
import org.hibernate.annotations.LazyCollectionOption;
28-
2929

3030
/**
3131
* @author Emmanuel Bernard
@@ -101,7 +101,6 @@ public void setStays(List<Stay> stays) {
101101
}
102102

103103
@OneToMany(cascade=CascadeType.ALL, mappedBy = "oldPerson")
104-
@LazyCollection(LazyCollectionOption.EXTRA)
105104
@Fetch(FetchMode.SUBSELECT)
106105
public Collection<Stay> getOldStays() {
107106
return oldStays;
@@ -122,7 +121,6 @@ public void setVeryOldStays(Collection<Stay> veryOldStays) {
122121
}
123122

124123
@OneToMany(cascade=CascadeType.ALL)
125-
@LazyCollection(LazyCollectionOption.EXTRA)
126124
@Fetch(FetchMode.SUBSELECT)
127125
@OrderColumn(name="orderedStayIndex")
128126
public List<Stay> getOrderedStay() {

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/idmanytoone/Customers.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public class Customers implements Serializable {
2828
private int customerID;
2929

3030
@OneToMany(mappedBy="owner", cascade= CascadeType.ALL, targetEntity=ShoppingBaskets.class)
31-
@org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.TRUE)
3231
private java.util.Set shoppingBasketses = new java.util.HashSet();
3332

3433
public void setCustomerID(int value) {

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/idmanytoone/ShoppingBaskets.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public class ShoppingBaskets implements Serializable {
3737
private java.util.Date basketDatetime;
3838

3939
@OneToMany(mappedBy="shoppingBaskets", cascade=CascadeType.ALL, targetEntity=BasketItems.class)
40-
@org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.TRUE)
4140
private java.util.Set items = new java.util.HashSet();
4241

4342
public void setBasketDatetime(java.util.Date value) {

0 commit comments

Comments
 (0)