Skip to content

Commit 1987d69

Browse files
fgsofisyrodiere
authored andcommitted
HHH-15522 Implement a LazyInitializable interface that PersistentCollection and Envers collections extend or implement. Improve related tests.
1 parent e218b8f commit 1987d69

File tree

9 files changed

+109
-55
lines changed

9 files changed

+109
-55
lines changed

hibernate-core/src/main/java/org/hibernate/Hibernate.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.hibernate.engine.spi.SharedSessionContractImplementor;
2121
import org.hibernate.proxy.HibernateProxy;
2222
import org.hibernate.proxy.LazyInitializer;
23+
import org.hibernate.collection.spi.LazyInitializable;
2324

2425
/**
2526
* <ul>
@@ -61,8 +62,8 @@ public static void initialize(Object proxy) throws HibernateException {
6162
if ( proxy instanceof HibernateProxy ) {
6263
( (HibernateProxy) proxy ).getHibernateLazyInitializer().initialize();
6364
}
64-
else if ( proxy instanceof PersistentCollection ) {
65-
( (PersistentCollection) proxy ).forceInitialization();
65+
else if ( proxy instanceof LazyInitializable ) {
66+
( (LazyInitializable) proxy ).forceInitialization();
6667
}
6768
else if ( proxy instanceof PersistentAttributeInterceptable ) {
6869
final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) proxy;
@@ -91,8 +92,8 @@ else if ( proxy instanceof PersistentAttributeInterceptable ) {
9192
}
9293
return true;
9394
}
94-
else if ( proxy instanceof PersistentCollection ) {
95-
return ( (PersistentCollection) proxy ).wasInitialized();
95+
else if ( proxy instanceof LazyInitializable ) {
96+
return ( (LazyInitializable) proxy ).wasInitialized();
9697
}
9798
else {
9899
return true;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.collection.spi;
8+
9+
import org.hibernate.Incubating;
10+
11+
/**
12+
* Hibernate "wraps" a java collection in an instance of PersistentCollection. Envers uses custom collection
13+
* wrappers (ListProxy, SetProxy, etc). All of them need to extend LazyInitializable, so the
14+
* Hibernate.isInitialized method can check if the collection is initialized or not.
15+
*
16+
* @author Fabricio Gregorio
17+
*/
18+
@Incubating
19+
public interface LazyInitializable {
20+
21+
/**
22+
* Is this instance initialized?
23+
*
24+
* @return Was this collection initialized? Or is its data still not (fully) loaded?
25+
*/
26+
boolean wasInitialized();
27+
28+
/**
29+
* To be called internally by the session, forcing immediate initialization.
30+
*/
31+
void forceInitialization();
32+
33+
}

hibernate-core/src/main/java/org/hibernate/collection/spi/PersistentCollection.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
*
4646
* @author Gavin King
4747
*/
48-
public interface PersistentCollection {
48+
public interface PersistentCollection extends LazyInitializable {
4949
/**
5050
* Get the owning entity. Note that the owner is only
5151
* set during the flush cycle, and when a new collection
@@ -271,11 +271,6 @@ Object readFrom(ResultSet rs, CollectionPersister role, CollectionAliases descri
271271
*/
272272
Serializable getSnapshot(CollectionPersister persister);
273273

274-
/**
275-
* To be called internally by the session, forcing immediate initialization.
276-
*/
277-
void forceInitialization();
278-
279274
/**
280275
* Does the given element/entry exist in the collection?
281276
*
@@ -335,13 +330,6 @@ Object readFrom(ResultSet rs, CollectionPersister role, CollectionAliases descri
335330
*/
336331
boolean isWrapper(Object collection);
337332

338-
/**
339-
* Is this instance initialized?
340-
*
341-
* @return Was this collection initialized? Or is its data still not (fully) loaded?
342-
*/
343-
boolean wasInitialized();
344-
345333
/**
346334
* Does this instance have any "queued" operations?
347335
*

hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/lazy/proxy/CollectionProxy.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
import java.io.Serializable;
1010
import java.util.Collection;
1111
import java.util.Iterator;
12+
import org.hibernate.collection.spi.LazyInitializable;
1213

1314
import org.hibernate.envers.internal.entities.mapper.relation.lazy.initializor.Initializor;
1415

1516
/**
1617
* @author Adam Warski (adam at warski dot org)
1718
*/
18-
public abstract class CollectionProxy<U, T extends Collection<U>> implements Collection<U>, Serializable {
19+
public abstract class CollectionProxy<U, T extends Collection<U>> implements Collection<U>, LazyInitializable, Serializable {
20+
1921
private static final long serialVersionUID = 8698249863871832402L;
2022

2123
private transient org.hibernate.envers.internal.entities.mapper.relation.lazy.initializor.Initializor<T> initializor;
@@ -34,6 +36,16 @@ protected void checkInit() {
3436
}
3537
}
3638

39+
@Override
40+
public final boolean wasInitialized() {
41+
return delegate != null;
42+
}
43+
44+
@Override
45+
public final void forceInitialization() {
46+
checkInit();
47+
}
48+
3749
@Override
3850
public int size() {
3951
checkInit();
@@ -118,7 +130,7 @@ public String toString() {
118130
return delegate.toString();
119131
}
120132

121-
@SuppressWarnings({"EqualsWhichDoesntCheckParameterClass"})
133+
@SuppressWarnings({ "EqualsWhichDoesntCheckParameterClass" })
122134
@Override
123135
public boolean equals(Object obj) {
124136
checkInit();

hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/lazy/proxy/MapProxy.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
import java.util.Collection;
1111
import java.util.Map;
1212
import java.util.Set;
13-
13+
import org.hibernate.collection.spi.LazyInitializable;
1414
import org.hibernate.envers.internal.entities.mapper.relation.lazy.initializor.Initializor;
1515

1616
/**
1717
* @author Adam Warski (adam at warski dot org)
1818
*/
19-
public class MapProxy<K, V> implements Map<K, V>, Serializable {
19+
public class MapProxy<K, V> implements Map<K, V>, LazyInitializable, Serializable {
20+
2021
private static final long serialVersionUID = 8418037541773074646L;
2122

2223
private transient Initializor<Map<K, V>> initializor;
@@ -35,6 +36,16 @@ private void checkInit() {
3536
}
3637
}
3738

39+
@Override
40+
public final boolean wasInitialized() {
41+
return delegate != null;
42+
}
43+
44+
@Override
45+
public final void forceInitialization() {
46+
checkInit();
47+
}
48+
3849
@Override
3950
public int size() {
4051
checkInit();
@@ -114,7 +125,7 @@ public String toString() {
114125
}
115126

116127
@Override
117-
@SuppressWarnings({"EqualsWhichDoesntCheckParameterClass"})
128+
@SuppressWarnings({ "EqualsWhichDoesntCheckParameterClass" })
118129
public boolean equals(Object obj) {
119130
checkInit();
120131
return delegate.equals( obj );

hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/lazy/proxy/SortedMapProxy.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
import java.util.Map;
1313
import java.util.Set;
1414
import java.util.SortedMap;
15-
15+
import org.hibernate.collection.spi.LazyInitializable;
1616
import org.hibernate.envers.internal.entities.mapper.relation.lazy.initializor.Initializor;
1717

1818
/**
1919
* @author Adam Warski (adam at warski dot org)
2020
*/
21-
public class SortedMapProxy<K, V> implements SortedMap<K, V>, Serializable {
21+
public class SortedMapProxy<K, V> implements SortedMap<K, V>, LazyInitializable, Serializable {
22+
2223
private static final long serialVersionUID = 2645817952901452375L;
2324

2425
private transient Initializor<SortedMap<K, V>> initializor;
@@ -37,6 +38,16 @@ private void checkInit() {
3738
}
3839
}
3940

41+
@Override
42+
public final boolean wasInitialized() {
43+
return delegate != null;
44+
}
45+
46+
@Override
47+
public final void forceInitialization() {
48+
checkInit();
49+
}
50+
4051
@Override
4152
public int size() {
4253
checkInit();
@@ -146,7 +157,7 @@ public K lastKey() {
146157
}
147158

148159
@Override
149-
@SuppressWarnings({"EqualsWhichDoesntCheckParameterClass"})
160+
@SuppressWarnings({ "EqualsWhichDoesntCheckParameterClass" })
150161
public boolean equals(Object o) {
151162
checkInit();
152163
return delegate.equals( o );

hibernate-envers/src/test/java/org/hibernate/envers/test/entities/collection/MultipleCollectionEntity.java

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
package org.hibernate.envers.test.entities.collection;
88

99
import java.util.ArrayList;
10-
import java.util.Collections;
1110
import java.util.List;
11+
import java.util.Objects;
12+
1213
import javax.persistence.CascadeType;
1314
import javax.persistence.Column;
1415
import javax.persistence.Entity;
@@ -26,6 +27,7 @@
2627
@Entity
2728
@Audited
2829
public class MultipleCollectionEntity {
30+
2931
@Id
3032
@GeneratedValue(strategy = GenerationType.IDENTITY)
3133
@Column(name = "ID", length = 10)
@@ -65,7 +67,7 @@ public void setText(String text) {
6567
}
6668

6769
public List<MultipleCollectionRefEntity1> getRefEntities1() {
68-
return Collections.unmodifiableList( refEntities1 );
70+
return refEntities1;
6971
}
7072

7173
public void addRefEntity1(MultipleCollectionRefEntity1 refEntity1) {
@@ -77,7 +79,7 @@ public void removeRefEntity1(MultipleCollectionRefEntity1 refEntity1) {
7779
}
7880

7981
public List<MultipleCollectionRefEntity2> getRefEntities2() {
80-
return Collections.unmodifiableList( refEntities2 );
82+
return refEntities2;
8183
}
8284

8385
public void addRefEntity2(MultipleCollectionRefEntity2 refEntity2) {
@@ -110,34 +112,20 @@ public String toString() {
110112
}
111113

112114
@Override
113-
public boolean equals(Object o) {
114-
if ( this == o ) {
115-
return true;
116-
}
117-
if ( !(o instanceof MultipleCollectionEntity) ) {
118-
return false;
119-
}
120-
121-
MultipleCollectionEntity that = (MultipleCollectionEntity) o;
115+
public int hashCode() {
116+
return Objects.hash( id );
117+
}
122118

123-
if ( refEntities1 != null ? !refEntities1.equals( that.refEntities1 ) : that.refEntities1 != null ) {
124-
return false;
125-
}
126-
if ( refEntities2 != null ? !refEntities2.equals( that.refEntities2 ) : that.refEntities2 != null ) {
119+
@Override
120+
public boolean equals(Object obj) {
121+
if ( this == obj )
122+
return true;
123+
if ( obj == null )
127124
return false;
128-
}
129-
if ( text != null ? !text.equals( that.text ) : that.text != null ) {
125+
if ( getClass() != obj.getClass() )
130126
return false;
131-
}
132-
133-
return true;
127+
MultipleCollectionEntity other = (MultipleCollectionEntity) obj;
128+
return Objects.equals( id, other.id );
134129
}
135130

136-
@Override
137-
public int hashCode() {
138-
int result = text != null ? text.hashCode() : 0;
139-
result = 31 * result + (refEntities1 != null ? refEntities1.hashCode() : 0);
140-
result = 31 * result + (refEntities2 != null ? refEntities2.hashCode() : 0);
141-
return result;
142-
}
143131
}

hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/lazy/IsCollectionInitializedBytecodeEnhancementTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public void initData() {
7777
}
7878

7979
@Test
80+
@SuppressWarnings("unchecked")
8081
public void testIsInitialized() {
8182
EntityManager em = getEntityManager();
8283

@@ -88,6 +89,10 @@ public void testIsInitialized() {
8889
MultipleCollectionEntity ret = res.get( 0 );
8990

9091
assertEquals( Hibernate.isInitialized( ret.getRefEntities1() ), false );
91-
92+
93+
Hibernate.initialize(ret.getRefEntities1());
94+
95+
assertEquals( Hibernate.isInitialized( ret.getRefEntities1() ), true );
96+
9297
}
9398
}

hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/lazy/IsCollectionInitializedTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public void initData() {
7272
}
7373

7474
@Test
75+
@SuppressWarnings("unchecked")
7576
public void testIsInitialized() {
7677
EntityManager em = getEntityManager();
7778

@@ -81,8 +82,12 @@ public void testIsInitialized() {
8182
.getResultList();
8283

8384
MultipleCollectionEntity ret = res.get( 0 );
84-
85+
8586
assertEquals( Hibernate.isInitialized( ret.getRefEntities1() ), false );
87+
88+
Hibernate.initialize(ret.getRefEntities1());
89+
90+
assertEquals( Hibernate.isInitialized( ret.getRefEntities1() ), true );
8691

8792
}
8893
}

0 commit comments

Comments
 (0)