HHH-18190 - Remove @LazyCollection
This commit is contained in:
parent
4309cffb4d
commit
08f08b8820
|
@ -597,72 +597,3 @@ include::{extrasdir}/fetching-strategies-fetch-mode-join-example.sql[]
|
|||
====
|
||||
|
||||
This time, there was no secondary query because the child collection was loaded along with the parent entity.
|
||||
|
||||
[[fetching-LazyCollection]]
|
||||
=== `@LazyCollection`
|
||||
|
||||
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.
|
||||
The possible values are given by the `https://docs.jboss.org/hibernate/orm/{majorMinorVersion}/javadocs/org/hibernate/annotations/LazyCollectionOption.html[LazyCollectionOption]` enumeration:
|
||||
|
||||
`TRUE`:: Load it when the state is requested.
|
||||
`FALSE`:: Eagerly load it.
|
||||
`EXTRA`:: Prefer extra queries over full collection loading.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
Each element is fetched individually using a secondary query.
|
||||
|
||||
[[fetching-LazyCollection-domain-model-example]]
|
||||
.`LazyCollectionOption.EXTRA` mapping example
|
||||
====
|
||||
[source, java, indent=0]
|
||||
----
|
||||
include::{example-dir-fetching}/LazyCollectionTest.java[tags=fetching-LazyCollection-domain-model-example]
|
||||
----
|
||||
====
|
||||
|
||||
[NOTE]
|
||||
====
|
||||
`LazyCollectionOption.EXTRA` only works for ordered collections,
|
||||
either List(s) that are annotated with @OrderColumn or Map(s).
|
||||
|
||||
For bags (e.g. regular List(s) of entities that do not preserve any certain ordering),
|
||||
the `@LazyCollection(LazyCollectionOption.EXTRA)` behaves like any other `FetchType.LAZY` collection
|
||||
(the collection is fetched entirely upon being accessed for the first time).
|
||||
====
|
||||
|
||||
Now, considering we have the following entities:
|
||||
|
||||
[[fetching-LazyCollection-persist-example]]
|
||||
.`LazyCollectionOption.EXTRA` Domain Model example
|
||||
====
|
||||
[source, java, indent=0]
|
||||
----
|
||||
include::{example-dir-fetching}/LazyCollectionTest.java[tags=fetching-LazyCollection-persist-example]
|
||||
----
|
||||
====
|
||||
|
||||
When fetching the `employee` collection entries by their position in the `List`,
|
||||
Hibernate generates the following SQL statements:
|
||||
|
||||
[[fetching-LazyCollection-select-example]]
|
||||
.`LazyCollectionOption.EXTRA` fetching example
|
||||
====
|
||||
[source, java, indent=0]
|
||||
----
|
||||
include::{example-dir-fetching}/LazyCollectionTest.java[tags=fetching-LazyCollection-select-example]
|
||||
----
|
||||
|
||||
[source, SQL, indent=0]
|
||||
----
|
||||
include::{extrasdir}/fetching-LazyCollection-select-example.sql[]
|
||||
----
|
||||
====
|
||||
|
||||
[WARNING]
|
||||
====
|
||||
Therefore, the child entities were fetched one after the other without triggering a full collection initialization.
|
||||
|
||||
For this reason, caution is advised since accessing all elements using `LazyCollectionOption.EXTRA` can lead to N + 1 query issue.
|
||||
====
|
||||
|
|
|
@ -1,47 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Specify the laziness of a collection, either a
|
||||
* {@link jakarta.persistence.OneToMany} or
|
||||
* {@link jakarta.persistence.ManyToMany} association,
|
||||
* or an {@link jakarta.persistence.ElementCollection}.
|
||||
* This is an alternative to specifying the JPA
|
||||
* {@link jakarta.persistence.FetchType}. This annotation
|
||||
* is used to enable {@linkplain LazyCollectionOption#EXTRA
|
||||
* extra-lazy collection fetching}.
|
||||
*
|
||||
* @author Emmanuel Bernard
|
||||
*
|
||||
* @deprecated
|
||||
* <ul>
|
||||
* <li>Use the JPA-defined {@link jakarta.persistence.FetchType#EAGER}
|
||||
* instead of {@code LazyCollection(FALSE)}.
|
||||
* <li>Use static methods of {@link org.hibernate.Hibernate},
|
||||
* for example {@link org.hibernate.Hibernate#size(Collection)},
|
||||
* {@link org.hibernate.Hibernate#contains(Collection, Object)}, or
|
||||
* {@link org.hibernate.Hibernate#get(Map, Object)} instead
|
||||
* of {@code LazyCollection(EXTRA)}.
|
||||
* </ul>
|
||||
*/
|
||||
@Deprecated(since="6.2")
|
||||
@Target({ElementType.METHOD, ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface LazyCollection {
|
||||
/**
|
||||
* The laziness of the collection.
|
||||
*/
|
||||
LazyCollectionOption value() default LazyCollectionOption.TRUE;
|
||||
}
|
|
@ -1,68 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.annotations;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Enumerates the options for lazy loading of a
|
||||
* {@linkplain jakarta.persistence.ElementCollection collection},
|
||||
* {@linkplain jakarta.persistence.ManyToOne many to one association},
|
||||
* or {@linkplain jakarta.persistence.ManyToMany many to many association}.
|
||||
*
|
||||
* @author Emmanuel Bernard
|
||||
*
|
||||
* @see LazyCollection
|
||||
*
|
||||
* @deprecated
|
||||
* <ul>
|
||||
* <li>Use the JPA-defined {@link jakarta.persistence.FetchType#EAGER}
|
||||
* instead of {@code LazyCollection(FALSE)}.
|
||||
* <li>Use static methods of {@link org.hibernate.Hibernate},
|
||||
* for example {@link org.hibernate.Hibernate#size(Collection)},
|
||||
* {@link org.hibernate.Hibernate#contains(Collection, Object)}, or
|
||||
* {@link org.hibernate.Hibernate#get(Map, Object)} instead
|
||||
* of {@code LazyCollection(EXTRA)}.
|
||||
* </ul>
|
||||
*/
|
||||
@Deprecated
|
||||
public enum LazyCollectionOption {
|
||||
/**
|
||||
* The collection is always loaded eagerly, and all its
|
||||
* elements are available immediately. However, access to
|
||||
* the collection is still mediated by an instance of
|
||||
* {@link org.hibernate.collection.spi.PersistentCollection},
|
||||
* which tracks modifications to the collection.
|
||||
*
|
||||
* @deprecated use {@link jakarta.persistence.FetchType#EAGER}
|
||||
*/
|
||||
@Deprecated
|
||||
FALSE,
|
||||
/**
|
||||
* The underlying Java collection is proxied by an instance of
|
||||
* {@link org.hibernate.collection.spi.PersistentCollection}
|
||||
* and lazily fetched when a method of the proxy is called.
|
||||
* All elements of the collection are retrieved at once.
|
||||
*
|
||||
* @deprecated use {@link jakarta.persistence.FetchType#LAZY}
|
||||
*/
|
||||
@Deprecated
|
||||
TRUE,
|
||||
/**
|
||||
* The underlying Java collection is proxied by an instance of
|
||||
* {@link org.hibernate.collection.spi.PersistentCollection}
|
||||
* and its state is fetched lazily from the database as needed,
|
||||
* when methods of the proxy are called. When reasonable, the
|
||||
* proxy will avoid fetching all elements of the collection
|
||||
* at once.
|
||||
*
|
||||
* @deprecated use operations of {@link org.hibernate.Hibernate}
|
||||
*/
|
||||
@Deprecated
|
||||
EXTRA
|
||||
}
|
|
@ -39,8 +39,6 @@ import org.hibernate.annotations.FilterJoinTable;
|
|||
import org.hibernate.annotations.Formula;
|
||||
import org.hibernate.annotations.HQLSelect;
|
||||
import org.hibernate.annotations.Immutable;
|
||||
import org.hibernate.annotations.LazyCollection;
|
||||
import org.hibernate.annotations.LazyCollectionOption;
|
||||
import org.hibernate.annotations.LazyGroup;
|
||||
import org.hibernate.annotations.ListIndexBase;
|
||||
import org.hibernate.annotations.ListIndexJavaType;
|
||||
|
@ -150,7 +148,6 @@ import jakarta.persistence.UniqueConstraint;
|
|||
import static jakarta.persistence.AccessType.PROPERTY;
|
||||
import static jakarta.persistence.ConstraintMode.NO_CONSTRAINT;
|
||||
import static jakarta.persistence.ConstraintMode.PROVIDER_DEFAULT;
|
||||
import static jakarta.persistence.FetchType.EAGER;
|
||||
import static jakarta.persistence.FetchType.LAZY;
|
||||
import static org.hibernate.boot.model.internal.AnnotatedClassType.EMBEDDABLE;
|
||||
import static org.hibernate.boot.model.internal.AnnotatedClassType.NONE;
|
||||
|
@ -1559,21 +1556,8 @@ public abstract class CollectionBinder {
|
|||
@SuppressWarnings("deprecation")
|
||||
private void handleLazy() {
|
||||
final FetchType jpaFetchType = getJpaFetchType();
|
||||
final LazyCollection lazyCollectionAnnotation = property.getDirectAnnotationUsage( LazyCollection.class );
|
||||
if ( lazyCollectionAnnotation != null ) {
|
||||
final LazyCollectionOption option = lazyCollectionAnnotation.value();
|
||||
boolean eager = option == LazyCollectionOption.FALSE;
|
||||
if ( !eager && jpaFetchType == EAGER ) {
|
||||
throw new AnnotationException("Collection '" + safeCollectionRole()
|
||||
+ "' is marked 'fetch=EAGER' and '@LazyCollection(" + option + ")'");
|
||||
}
|
||||
collection.setLazy( !eager );
|
||||
collection.setExtraLazy( option == LazyCollectionOption.EXTRA );
|
||||
}
|
||||
else {
|
||||
collection.setLazy( jpaFetchType == LAZY );
|
||||
collection.setExtraLazy( false );
|
||||
}
|
||||
collection.setLazy( jpaFetchType == LAZY );
|
||||
collection.setExtraLazy( false );
|
||||
}
|
||||
|
||||
private FetchType getJpaFetchType() {
|
||||
|
|
|
@ -366,10 +366,6 @@ public interface HibernateAnnotations {
|
|||
JoinFormula.class,
|
||||
JoinFormulaAnnotation.class
|
||||
);
|
||||
OrmAnnotationDescriptor<LazyCollection, LazyCollectionAnnotation> LAZY_COLLECTION = new OrmAnnotationDescriptor<>(
|
||||
LazyCollection.class,
|
||||
LazyCollectionAnnotation.class
|
||||
);
|
||||
OrmAnnotationDescriptor<LazyGroup, LazyGroupAnnotation> LAZY_GROUP = new OrmAnnotationDescriptor<>(
|
||||
LazyGroup.class,
|
||||
LazyGroupAnnotation.class
|
||||
|
|
|
@ -1,49 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
|
||||
*/
|
||||
package org.hibernate.boot.models.annotations.internal;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
import org.hibernate.annotations.LazyCollection;
|
||||
import org.hibernate.models.spi.SourceModelBuildingContext;
|
||||
|
||||
@SuppressWarnings({ "ClassExplicitlyAnnotation", "unused" })
|
||||
@jakarta.annotation.Generated("org.hibernate.orm.build.annotations.ClassGeneratorProcessor")
|
||||
public class LazyCollectionAnnotation implements LazyCollection {
|
||||
private org.hibernate.annotations.LazyCollectionOption value;
|
||||
|
||||
/**
|
||||
* Used in creating dynamic annotation instances (e.g. from XML)
|
||||
*/
|
||||
public LazyCollectionAnnotation(SourceModelBuildingContext modelContext) {
|
||||
this.value = org.hibernate.annotations.LazyCollectionOption.TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used in creating annotation instances from JDK variant
|
||||
*/
|
||||
public LazyCollectionAnnotation(LazyCollection annotation, SourceModelBuildingContext modelContext) {
|
||||
this.value = annotation.value();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Class<? extends Annotation> annotationType() {
|
||||
return LazyCollection.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public org.hibernate.annotations.LazyCollectionOption value() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void value(org.hibernate.annotations.LazyCollectionOption value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -47,31 +47,6 @@ public class FetchingTest extends BaseCoreFunctionalTestCase {
|
|||
s.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtraLazy() {
|
||||
Session s;
|
||||
Transaction tx;
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
Person p = new Person( "Gavin", "King", "JBoss Inc" );
|
||||
Stay stay = new Stay( p, new Date(), new Date(), "A380", "Blah", "Blah" );
|
||||
p.getOrderedStay().add( stay );
|
||||
s.persist( p );
|
||||
tx.commit();
|
||||
s.clear();
|
||||
tx = s.beginTransaction();
|
||||
p = (Person) s.createQuery( "from Person p where p.firstName = :name" )
|
||||
.setParameter( "name", "Gavin" ).uniqueResult();
|
||||
assertFalse( Hibernate.isInitialized( p.getOrderedStay() ) );
|
||||
assertEquals( 1, p.getOrderedStay().size() );
|
||||
assertFalse( Hibernate.isInitialized( p.getOrderedStay() ) );
|
||||
assertEquals( "A380", p.getOrderedStay().get(0).getVessel() );
|
||||
assertFalse( Hibernate.isInitialized( p.getOrderedStay() ) );
|
||||
s.remove( p );
|
||||
tx.commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHibernateFetchingLazy() {
|
||||
try(Session s = openSession()) {
|
||||
|
@ -95,7 +70,7 @@ public class FetchingTest extends BaseCoreFunctionalTestCase {
|
|||
.setParameter( "name", "Gavin" ).uniqueResult();
|
||||
assertFalse( Hibernate.isInitialized( p.getOldStays() ) );
|
||||
assertEquals( 1, p.getOldStays().size() );
|
||||
assertFalse( "lazy extra is failing", Hibernate.isInitialized( p.getOldStays() ) );
|
||||
assertTrue( Hibernate.isInitialized( p.getOldStays() ) );
|
||||
s.clear();
|
||||
stay = (Stay) s.get( Stay.class, stay.getId() );
|
||||
assertTrue( !Hibernate.isInitialized( stay.getOldPerson() ) );
|
||||
|
|
|
@ -7,11 +7,16 @@
|
|||
|
||||
//$Id$
|
||||
package org.hibernate.orm.test.annotations.fetch;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.annotations.Fetch;
|
||||
import org.hibernate.annotations.FetchMode;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
|
@ -21,11 +26,6 @@ import jakarta.persistence.OneToMany;
|
|||
import jakarta.persistence.OrderColumn;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import org.hibernate.annotations.Fetch;
|
||||
import org.hibernate.annotations.FetchMode;
|
||||
import org.hibernate.annotations.LazyCollection;
|
||||
import org.hibernate.annotations.LazyCollectionOption;
|
||||
|
||||
|
||||
/**
|
||||
* @author Emmanuel Bernard
|
||||
|
@ -101,7 +101,6 @@ public class Person implements Serializable {
|
|||
}
|
||||
|
||||
@OneToMany(cascade=CascadeType.ALL, mappedBy = "oldPerson")
|
||||
@LazyCollection(LazyCollectionOption.EXTRA)
|
||||
@Fetch(FetchMode.SUBSELECT)
|
||||
public Collection<Stay> getOldStays() {
|
||||
return oldStays;
|
||||
|
@ -122,7 +121,6 @@ public class Person implements Serializable {
|
|||
}
|
||||
|
||||
@OneToMany(cascade=CascadeType.ALL)
|
||||
@LazyCollection(LazyCollectionOption.EXTRA)
|
||||
@Fetch(FetchMode.SUBSELECT)
|
||||
@OrderColumn(name="orderedStayIndex")
|
||||
public List<Stay> getOrderedStay() {
|
||||
|
|
|
@ -28,7 +28,6 @@ public class Customers implements Serializable {
|
|||
private int customerID;
|
||||
|
||||
@OneToMany(mappedBy="owner", cascade= CascadeType.ALL, targetEntity=ShoppingBaskets.class)
|
||||
@org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.TRUE)
|
||||
private java.util.Set shoppingBasketses = new java.util.HashSet();
|
||||
|
||||
public void setCustomerID(int value) {
|
||||
|
|
|
@ -37,7 +37,6 @@ public class ShoppingBaskets implements Serializable {
|
|||
private java.util.Date basketDatetime;
|
||||
|
||||
@OneToMany(mappedBy="shoppingBaskets", cascade=CascadeType.ALL, targetEntity=BasketItems.class)
|
||||
@org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.TRUE)
|
||||
private java.util.Set items = new java.util.HashSet();
|
||||
|
||||
public void setBasketDatetime(java.util.Date value) {
|
||||
|
|
|
@ -19,16 +19,13 @@ import jakarta.persistence.Id;
|
|||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.OrderColumn;
|
||||
|
||||
import org.hibernate.annotations.LazyCollection;
|
||||
import org.hibernate.annotations.LazyCollectionOption;
|
||||
|
||||
@Entity(name="Forum")
|
||||
public class Forum{
|
||||
|
||||
private Long id;
|
||||
private String name;
|
||||
protected List<Comment> posts = new ArrayList<Comment>();
|
||||
protected List<User> users = new ArrayList<User>();
|
||||
protected List<Comment> posts = new ArrayList<>();
|
||||
protected List<User> users = new ArrayList<>();
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
|
@ -42,7 +39,6 @@ public class Forum{
|
|||
}
|
||||
|
||||
@OneToMany(mappedBy = "forum", cascade = CascadeType.ALL , orphanRemoval = false, fetch = FetchType.LAZY)
|
||||
@LazyCollection(LazyCollectionOption.EXTRA)
|
||||
@OrderColumn(name = "idx2")
|
||||
public List<Comment> getPosts() {
|
||||
return posts;
|
||||
|
@ -53,7 +49,6 @@ public class Forum{
|
|||
}
|
||||
|
||||
@OneToMany(mappedBy = "forum", cascade = CascadeType.ALL , orphanRemoval = true, fetch = FetchType.LAZY)
|
||||
@LazyCollection(LazyCollectionOption.EXTRA)
|
||||
@OrderColumn(name = "idx3")
|
||||
public List<User> getUsers() {
|
||||
return users;
|
||||
|
|
|
@ -16,17 +16,13 @@ import jakarta.persistence.FetchType;
|
|||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.OrderColumn;
|
||||
|
||||
import org.hibernate.annotations.LazyCollection;
|
||||
import org.hibernate.annotations.LazyCollectionOption;
|
||||
|
||||
@Entity(name = "Post")
|
||||
@DiscriminatorValue(value = "WCT")
|
||||
public class Post extends Comment{
|
||||
|
||||
protected List<Comment> comments = new ArrayList<Comment>();
|
||||
protected List<Comment> comments = new ArrayList<>();
|
||||
|
||||
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL , orphanRemoval = false, fetch = FetchType.LAZY)
|
||||
@LazyCollection(LazyCollectionOption.EXTRA)
|
||||
@OrderColumn(name = "idx")
|
||||
public List<Comment> getComments() {
|
||||
return comments;
|
||||
|
|
|
@ -9,6 +9,17 @@ package org.hibernate.orm.test.collection.delayedOperation;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
|
||||
import org.hibernate.testing.orm.junit.DomainModel;
|
||||
import org.hibernate.testing.orm.junit.JiraKey;
|
||||
import org.hibernate.testing.orm.junit.SessionFactory;
|
||||
import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
|
@ -19,18 +30,6 @@ import jakarta.persistence.ManyToOne;
|
|||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.OrderColumn;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.annotations.LazyCollection;
|
||||
import org.hibernate.annotations.LazyCollectionOption;
|
||||
|
||||
import org.hibernate.testing.orm.junit.DomainModel;
|
||||
import org.hibernate.testing.orm.junit.JiraKey;
|
||||
import org.hibernate.testing.orm.junit.SessionFactory;
|
||||
import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
@ -302,7 +301,6 @@ public class ListDelayedOperationTest {
|
|||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// remove a detached element and commit
|
||||
p.removeChild( c2 );
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
p = (Parent) session.merge( p );
|
||||
Hibernate.initialize( p );
|
||||
}
|
||||
|
@ -319,50 +317,6 @@ public class ListDelayedOperationTest {
|
|||
);
|
||||
}
|
||||
|
||||
/* STILL WORKING ON THIS ONE...
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-5855")
|
||||
public void testSimpleRemoveManaged() {
|
||||
// Remove a managed entity element and commit
|
||||
Session s = openSession();
|
||||
s.getTransaction().begin();
|
||||
Parent p = s.get( Parent.class, parentId );
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// get c1 so it is managed, then remove and commit
|
||||
p.removeChild( s.get( Child.class, childId1 ) );
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
s.getTransaction().begin();
|
||||
p = s.get( Parent.class, parentId );
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
assertEquals( 1, p.getChildren().size() );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
s.getTransaction().begin();
|
||||
p = s.get( Parent.class, parentId );
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// get c1 so it is managed, then remove, merge and commit
|
||||
p.removeChild( s.get( Child.class, childId2 ) );
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
s.merge( p );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
s.getTransaction().begin();
|
||||
p = s.get( Parent.class, parentId );
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
assertEquals( 0, p.getChildren().size() );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
*/
|
||||
|
||||
@Entity(name = "Parent")
|
||||
public static class Parent {
|
||||
|
||||
|
@ -371,7 +325,6 @@ public class ListDelayedOperationTest {
|
|||
private Long id;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", orphanRemoval = true)
|
||||
@LazyCollection(LazyCollectionOption.EXTRA)
|
||||
@OrderColumn
|
||||
private List<Child> children = new ArrayList<>();
|
||||
|
||||
|
|
|
@ -9,18 +9,8 @@ package org.hibernate.orm.test.collection.delayedOperation;
|
|||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.OneToMany;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.annotations.LazyCollection;
|
||||
import org.hibernate.annotations.LazyCollectionOption;
|
||||
|
||||
import org.hibernate.testing.orm.junit.DomainModel;
|
||||
import org.hibernate.testing.orm.junit.JiraKey;
|
||||
|
@ -30,8 +20,18 @@ import org.junit.jupiter.api.AfterEach;
|
|||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.OneToMany;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -108,8 +108,8 @@ public class SetDelayedOperationTest {
|
|||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// add detached Child c
|
||||
p.addChild( session.merge( c1 ) );
|
||||
// collection should still be uninitialized
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// collection should now be uninitialized
|
||||
assertTrue( Hibernate.isInitialized( p.getChildren() ) );
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -128,7 +128,8 @@ public class SetDelayedOperationTest {
|
|||
Parent p = session.get( Parent.class, parentId );
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
p.addChild( c2 );
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// collection should now be uninitialized
|
||||
assertTrue( Hibernate.isInitialized( p.getChildren() ) );
|
||||
session.merge( p );
|
||||
}
|
||||
);
|
||||
|
@ -152,8 +153,8 @@ public class SetDelayedOperationTest {
|
|||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// add transient Child
|
||||
p.addChild( new Child( "Darwin" ) );
|
||||
// collection should still be uninitialized
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// collection should now be uninitialized
|
||||
assertTrue( Hibernate.isInitialized( p.getChildren() ) );
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -173,8 +174,8 @@ public class SetDelayedOperationTest {
|
|||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// add transient Child
|
||||
p.addChild( new Child( "Comet" ) );
|
||||
// collection should still be uninitialized
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// collection should now be uninitialized
|
||||
assertTrue( Hibernate.isInitialized( p.getChildren() ) );
|
||||
session.merge( p );
|
||||
}
|
||||
);
|
||||
|
@ -209,8 +210,8 @@ public class SetDelayedOperationTest {
|
|||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// get the first Child so it is managed; add to collection
|
||||
p.addChild( session.get( Child.class, c1.getId() ) );
|
||||
// collection should still be uninitialized
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// collection should now be uninitialized
|
||||
assertTrue( Hibernate.isInitialized( p.getChildren() ) );
|
||||
}
|
||||
);
|
||||
scope.inTransaction(
|
||||
|
@ -229,8 +230,8 @@ public class SetDelayedOperationTest {
|
|||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// get the second Child so it is managed; add to collection
|
||||
p.addChild( session.get( Child.class, c2.getId() ) );
|
||||
// collection should still be uninitialized
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// collection should now be uninitialized
|
||||
assertTrue( Hibernate.isInitialized( p.getChildren() ) );
|
||||
session.merge( p );
|
||||
}
|
||||
);
|
||||
|
@ -263,7 +264,8 @@ public class SetDelayedOperationTest {
|
|||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// remove a detached element and commit
|
||||
p.removeChild( c1 );
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// collection should now be uninitialized
|
||||
assertTrue( Hibernate.isInitialized( p.getChildren() ) );
|
||||
session.merge( p );
|
||||
}
|
||||
);
|
||||
|
@ -285,7 +287,8 @@ public class SetDelayedOperationTest {
|
|||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// remove a detached element and commit
|
||||
p.removeChild( c2 );
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// collection should now be uninitialized
|
||||
assertTrue( Hibernate.isInitialized( p.getChildren() ) );
|
||||
p = (Parent) session.merge( p );
|
||||
Hibernate.initialize( p );
|
||||
}
|
||||
|
@ -311,7 +314,8 @@ public class SetDelayedOperationTest {
|
|||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// get c1 so it is managed, then remove and commit
|
||||
p.removeChild( session.get( Child.class, childId1 ) );
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// collection should now be uninitialized
|
||||
assertTrue( Hibernate.isInitialized( p.getChildren() ) );
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -329,7 +333,8 @@ public class SetDelayedOperationTest {
|
|||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// get c1 so it is managed, then remove, merge and commit
|
||||
p.removeChild( session.get( Child.class, childId2 ) );
|
||||
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
|
||||
// collection should now be uninitialized
|
||||
assertTrue( Hibernate.isInitialized( p.getChildren() ) );
|
||||
session.merge( p );
|
||||
}
|
||||
);
|
||||
|
@ -351,7 +356,6 @@ public class SetDelayedOperationTest {
|
|||
private Long id;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", orphanRemoval = true)
|
||||
@LazyCollection(value = LazyCollectionOption.EXTRA)
|
||||
private Set<Child> children = new HashSet<>();
|
||||
|
||||
public Parent() {
|
||||
|
|
|
@ -13,8 +13,6 @@ import java.util.Set;
|
|||
|
||||
import org.hibernate.annotations.Fetch;
|
||||
import org.hibernate.annotations.FetchMode;
|
||||
import org.hibernate.annotations.LazyCollection;
|
||||
import org.hibernate.annotations.LazyCollectionOption;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.orm.test.util.connections.ConnectionCheckingConnectionProvider;
|
||||
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
|
||||
|
@ -239,7 +237,6 @@ public class LazyLoadingConnectionCloseTest extends EntityManagerFactoryBasedFun
|
|||
}
|
||||
|
||||
@OneToMany(targetEntity = ChildEntity.class, mappedBy = "parent")
|
||||
@LazyCollection(LazyCollectionOption.EXTRA)
|
||||
@Fetch(FetchMode.SELECT)
|
||||
public Set<ChildEntity> getChildren() {
|
||||
return children;
|
||||
|
|
|
@ -1,45 +0,0 @@
|
|||
package org.hibernate.orm.test.extralazy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.annotations.LazyCollection;
|
||||
import org.hibernate.annotations.LazyCollectionOption;
|
||||
import org.hibernate.annotations.SQLRestriction;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "championship")
|
||||
public class Championship {
|
||||
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
@LazyCollection(LazyCollectionOption.EXTRA)
|
||||
@SQLRestriction(" gpa >= 4 ")
|
||||
private List<Student> students = new ArrayList<>();
|
||||
|
||||
public Championship() {}
|
||||
|
||||
public Championship(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public List<Student> getStudents() {
|
||||
return students;
|
||||
}
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.orm.test.extralazy;
|
||||
|
||||
|
||||
|
||||
public class Child {
|
||||
|
||||
private String id;
|
||||
|
||||
private Parent parent;
|
||||
|
||||
private String firstName;
|
||||
|
||||
public void setParent(Parent parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public Parent getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
|
||||
//$Id: Document.java 7635 2005-07-24 23:04:30Z oneovthafew $
|
||||
package org.hibernate.orm.test.extralazy;
|
||||
|
||||
|
||||
public class Document {
|
||||
|
||||
private String title;
|
||||
private String content;
|
||||
private User owner;
|
||||
|
||||
Document() {}
|
||||
|
||||
public Document(String title, String content, User owner) {
|
||||
this.content = content;
|
||||
this.owner = owner;
|
||||
this.title = title;
|
||||
owner.getDocuments().add(this);
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
public User getOwner() {
|
||||
return owner;
|
||||
}
|
||||
public void setOwner(User owner) {
|
||||
this.owner = owner;
|
||||
}
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,140 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.orm.test.extralazy;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.orm.junit.DomainModel;
|
||||
import org.hibernate.testing.orm.junit.SessionFactory;
|
||||
import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
@DomainModel(
|
||||
xmlMappings = {
|
||||
"org/hibernate/orm/test/extralazy/UserGroup.hbm.xml"
|
||||
}
|
||||
)
|
||||
@SessionFactory
|
||||
public class ExtraLazyCollectionConsistencyTest {
|
||||
|
||||
private User user;
|
||||
|
||||
@BeforeEach
|
||||
protected void prepareTest(SessionFactoryScope scope) {
|
||||
scope.inTransaction( session -> {
|
||||
user = new User( "victor", "hugo" );
|
||||
session.persist( user );
|
||||
} );
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void tearDown(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
session.createQuery( "delete from Document" ).executeUpdate();
|
||||
session.createQuery( "delete from User" ).executeUpdate();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-9933")
|
||||
public void testSetSize(SessionFactoryScope scope) {
|
||||
scope.inTransaction( session -> {
|
||||
User _user = session.get( User.class, user.getName() );
|
||||
new Document( "Les Miserables", "sad", _user );
|
||||
assertThat( _user.getDocuments().size(), is( 1 ) );
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-9933")
|
||||
public void testSetIterator(SessionFactoryScope scope) {
|
||||
scope.inTransaction( session -> {
|
||||
User _user = session.get( User.class, user.getName() );
|
||||
new Document( "Les Miserables", "sad", _user );
|
||||
assertTrue( _user.getDocuments().iterator().hasNext() );
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-9933")
|
||||
public void testSetIsEmpty(SessionFactoryScope scope) {
|
||||
scope.inTransaction( session -> {
|
||||
User _user = session.get( User.class, user.getName() );
|
||||
new Document( "Les Miserables", "sad", _user );
|
||||
assertFalse( _user.getDocuments().isEmpty() );
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-9933")
|
||||
public void testSetContains(SessionFactoryScope scope) {
|
||||
scope.inTransaction( session -> {
|
||||
User _user = session.get( User.class, user.getName() );
|
||||
Document document = new Document( "Les Miserables", "sad", _user );
|
||||
assertTrue( _user.getDocuments().contains( document ) );
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-9933")
|
||||
public void testSetAdd(SessionFactoryScope scope) {
|
||||
scope.inTransaction( session -> {
|
||||
User _user = session.get( User.class, user.getName() );
|
||||
Document document = new Document();
|
||||
document.setTitle( "Les Miserables" );
|
||||
document.setContent( "sad" );
|
||||
document.setOwner( _user );
|
||||
assertTrue( _user.getDocuments().add( document ), "not added" );
|
||||
assertFalse( _user.getDocuments().add( document ), "added" );
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-9933")
|
||||
public void testSetRemove(SessionFactoryScope scope) {
|
||||
scope.inTransaction( session -> {
|
||||
User _user = session.get( User.class, user.getName() );
|
||||
|
||||
Document document = new Document( "Les Miserables", "sad", _user );
|
||||
assertTrue( _user.getDocuments().remove( document ), "not removed" );
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-9933")
|
||||
public void testSetToArray(SessionFactoryScope scope) {
|
||||
scope.inTransaction( session -> {
|
||||
User _user = session.get( User.class, user.getName() );
|
||||
|
||||
new Document( "Les Miserables", "sad", _user );
|
||||
assertThat( _user.getDocuments().toArray().length, is( 1 ) );
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-9933")
|
||||
public void testSetToArrayTyped(SessionFactoryScope scope) {
|
||||
scope.inTransaction( session -> {
|
||||
User _user = session.get( User.class, user.getName() );
|
||||
|
||||
new Document( "Les Miserables", "sad", _user );
|
||||
assertThat( _user.getDocuments().size(), is( 1 ) );
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
|
@ -1,409 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.orm.test.extralazy;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
|
||||
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
|
||||
import org.hibernate.testing.orm.junit.DomainModel;
|
||||
import org.hibernate.testing.orm.junit.FailureExpected;
|
||||
import org.hibernate.testing.orm.junit.JiraKey;
|
||||
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
|
||||
import org.hibernate.testing.orm.junit.SessionFactory;
|
||||
import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Gavin King
|
||||
*/
|
||||
@DomainModel(
|
||||
annotatedClasses = { School.class, Student.class, Championship.class },
|
||||
xmlMappings =
|
||||
{
|
||||
"org/hibernate/orm/test/extralazy/UserGroup.hbm.xml",
|
||||
"org/hibernate/orm/test/extralazy/Parent.hbm.xml",
|
||||
"org/hibernate/orm/test/extralazy/Child.hbm.xml"
|
||||
}
|
||||
)
|
||||
@SessionFactory
|
||||
public class ExtraLazyTest {
|
||||
|
||||
@AfterEach
|
||||
public void tearDown(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
session.createMutationQuery( "delete from Group" ).executeUpdate();
|
||||
session.createMutationQuery( "delete from Document" ).executeUpdate();
|
||||
session.createMutationQuery( "delete from User" ).executeUpdate();
|
||||
session.createMutationQuery( "delete from Child" ).executeUpdate();
|
||||
session.createMutationQuery( "delete from Parent" ).executeUpdate();
|
||||
session.createMutationQuery( "delete from Student" ).executeUpdate();
|
||||
session.createMutationQuery( "delete from School" ).executeUpdate();
|
||||
session.createMutationQuery( "delete from Championship" ).executeUpdate();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOrphanDelete(SessionFactoryScope scope) {
|
||||
User user = new User( "gavin", "secret" );
|
||||
Document hia = new Document( "HiA", "blah blah blah", user );
|
||||
Document hia2 = new Document( "HiA2", "blah blah blah blah", user );
|
||||
scope.inTransaction(
|
||||
session ->
|
||||
session.persist( user )
|
||||
);
|
||||
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
User gavin = session.get( User.class, "gavin" );
|
||||
assertThat( gavin.getDocuments().size(), is( 2 ) );
|
||||
gavin.getDocuments().remove( hia2 );
|
||||
assertFalse( gavin.getDocuments().contains( hia2 ) );
|
||||
assertTrue( gavin.getDocuments().contains( hia ) );
|
||||
assertThat( gavin.getDocuments().size(), is( 1 ) );
|
||||
assertFalse( Hibernate.isInitialized( gavin.getDocuments() ) );
|
||||
}
|
||||
);
|
||||
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
User gavin = session.get( User.class, "gavin" );
|
||||
assertThat( gavin.getDocuments().size(), is( 1 ) );
|
||||
assertFalse( gavin.getDocuments().contains( hia2 ) );
|
||||
assertTrue( gavin.getDocuments().contains( hia ) );
|
||||
assertFalse( Hibernate.isInitialized( gavin.getDocuments() ) );
|
||||
assertNull( session.get( Document.class, "HiA2" ) );
|
||||
gavin.getDocuments().clear();
|
||||
assertTrue( Hibernate.isInitialized( gavin.getDocuments() ) );
|
||||
session.remove( gavin );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
User gavin = new User( "gavin", "secret" );
|
||||
User turin = new User( "turin", "tiger" );
|
||||
Group g = new Group( "developers" );
|
||||
g.getUsers().put( "gavin", gavin );
|
||||
g.getUsers().put( "turin", turin );
|
||||
session.persist( g );
|
||||
gavin.getSession().put( "foo", new SessionAttribute( "foo", "foo bar baz" ) );
|
||||
gavin.getSession().put( "bar", new SessionAttribute( "bar", "foo bar baz 2" ) );
|
||||
}
|
||||
);
|
||||
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
Group g = session.get( Group.class, "developers" );
|
||||
User gavin = (User) g.getUsers().get( "gavin" );
|
||||
User turin = (User) g.getUsers().get( "turin" );
|
||||
assertNotNull( gavin );
|
||||
assertNotNull( turin );
|
||||
assertNull( g.getUsers().get( "emmanuel" ) );
|
||||
assertFalse( Hibernate.isInitialized( g.getUsers() ) );
|
||||
assertNotNull( gavin.getSession().get( "foo" ) );
|
||||
assertNull( turin.getSession().get( "foo" ) );
|
||||
assertFalse( Hibernate.isInitialized( gavin.getSession() ) );
|
||||
assertFalse( Hibernate.isInitialized( turin.getSession() ) );
|
||||
session.remove( gavin );
|
||||
session.remove( turin );
|
||||
session.remove( g );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveClear(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
User gavin = new User( "gavin", "secret" );
|
||||
User turin = new User( "turin", "tiger" );
|
||||
Group g = new Group( "developers" );
|
||||
g.getUsers().put( "gavin", gavin );
|
||||
g.getUsers().put( "turin", turin );
|
||||
session.persist( g );
|
||||
gavin.getSession().put( "foo", new SessionAttribute( "foo", "foo bar baz" ) );
|
||||
gavin.getSession().put( "bar", new SessionAttribute( "bar", "foo bar baz 2" ) );
|
||||
}
|
||||
);
|
||||
|
||||
User turin = scope.fromTransaction(
|
||||
session -> {
|
||||
Group g = session.get( Group.class, "developers" );
|
||||
User gavin = (User) g.getUsers().get( "gavin" );
|
||||
User t = (User) g.getUsers().get( "turin" );
|
||||
assertFalse( Hibernate.isInitialized( g.getUsers() ) );
|
||||
g.getUsers().clear();
|
||||
gavin.getSession().remove( "foo" );
|
||||
assertTrue( Hibernate.isInitialized( g.getUsers() ) );
|
||||
assertTrue( Hibernate.isInitialized( gavin.getSession() ) );
|
||||
return t;
|
||||
}
|
||||
);
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
Group g = session.get( Group.class, "developers" );
|
||||
assertTrue( g.getUsers().isEmpty() );
|
||||
assertFalse( Hibernate.isInitialized( g.getUsers() ) );
|
||||
User gavin = session.get( User.class, "gavin" );
|
||||
assertFalse( gavin.getSession().containsKey( "foo" ) );
|
||||
assertFalse( Hibernate.isInitialized( gavin.getSession() ) );
|
||||
session.remove( gavin );
|
||||
session.remove( turin );
|
||||
session.remove( g );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIndexFormulaMap(SessionFactoryScope scope) {
|
||||
User user1 = new User( "gavin", "secret" );
|
||||
User user2 = new User( "turin", "tiger" );
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
Group g = new Group( "developers" );
|
||||
g.getUsers().put( "gavin", user1 );
|
||||
g.getUsers().put( "turin", user2 );
|
||||
session.persist( g );
|
||||
user1.getSession().put( "foo", new SessionAttribute( "foo", "foo bar baz" ) );
|
||||
user1.getSession().put( "bar", new SessionAttribute( "bar", "foo bar baz 2" ) );
|
||||
}
|
||||
);
|
||||
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
Group g = session.get( Group.class, "developers" );
|
||||
assertThat( g.getUsers().size(), is( 2 ) );
|
||||
g.getUsers().remove( "turin" );
|
||||
Map smap = ( (User) g.getUsers().get( "gavin" ) ).getSession();
|
||||
assertThat( smap.size(), is( 2 ) );
|
||||
smap.remove( "bar" );
|
||||
}
|
||||
);
|
||||
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
Group g = session.get( Group.class, "developers" );
|
||||
assertThat( g.getUsers().size(), is( 1 ) );
|
||||
Map smap = ( (User) g.getUsers().get( "gavin" ) ).getSession();
|
||||
assertThat( smap.size(), is( 1 ) );
|
||||
User gavin = (User) g.getUsers().put( "gavin", session.merge( user2 ) );
|
||||
session.remove( gavin );
|
||||
assertThat(
|
||||
session.createQuery( "select count(*) from SessionAttribute" ).uniqueResult(),
|
||||
is( 0L )
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
Group g = session.get( Group.class, "developers" );
|
||||
assertThat( g.getUsers().size(), is( 1 ) );
|
||||
User turin = (User) g.getUsers().get( "turin" );
|
||||
Map smap = turin.getSession();
|
||||
assertThat( smap.size(), is( 0 ) );
|
||||
assertThat( session.createQuery( "select count(*) from User" ).uniqueResult(), is( 1L ) );
|
||||
session.remove( g );
|
||||
session.remove( turin );
|
||||
assertThat( session.createQuery( "select count(*) from User" ).uniqueResult(), is( 0L ) );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresDialectFeature(feature = DialectFeatureChecks.DoubleQuoteQuoting.class)
|
||||
public void testSQLQuery(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
User gavin = new User( "gavin", "secret" );
|
||||
User turin = new User( "turin", "tiger" );
|
||||
gavin.getSession().put( "foo", new SessionAttribute( "foo", "foo bar baz" ) );
|
||||
gavin.getSession().put( "bar", new SessionAttribute( "bar", "foo bar baz 2" ) );
|
||||
session.persist( gavin );
|
||||
session.persist( turin );
|
||||
session.flush();
|
||||
session.clear();
|
||||
List results = session.getNamedQuery( "userSessionData" ).setParameter( "uname", "%in" ).list();
|
||||
assertThat( results.size(), is( 2 ) );
|
||||
gavin = (User) results.get( 0 ) ;
|
||||
assertThat( gavin.getName(), is( "gavin" ) );
|
||||
Assert.assertTrue( Hibernate.isInitialized( gavin.getSession()) );
|
||||
assertThat( gavin.getSession().size(), is( 2 ) );
|
||||
session.createQuery( "delete SessionAttribute" ).executeUpdate();
|
||||
session.createQuery( "delete User" ).executeUpdate();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtraLazySet(SessionFactoryScope scope) {
|
||||
User gavin = new User( "gavin", "secret" );
|
||||
Document d1 = new Document( "d1", "blah", gavin );
|
||||
Document d2 = new Document( "d2", "blah blah", gavin );
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
session.persist( gavin );
|
||||
new Document("d3", "blah blah blah", gavin);
|
||||
assertTrue(gavin.getDocuments().size() == 3);
|
||||
}
|
||||
);
|
||||
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
User person = (User) session.get(User.class, gavin.getName());
|
||||
Document d4 = new Document( "d4", "blah blah blah blah", person );
|
||||
assertTrue(person.getDocuments().size() == 4);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresDialectFeature(feature = DialectFeatureChecks.DoubleQuoteQuoting.class)
|
||||
public void testSQLQuery2(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
User gavin = new User( "gavin", "secret" );
|
||||
User turin = new User( "turin", "tiger" );
|
||||
gavin.getSession().put( "foo", new SessionAttribute( "foo", "foo bar baz" ) );
|
||||
gavin.getSession().put( "bar", new SessionAttribute( "bar", "foo bar baz 2" ) );
|
||||
session.persist( gavin );
|
||||
session.persist( turin );
|
||||
session.flush();
|
||||
session.clear();
|
||||
List results = session.getNamedQuery( "userSessionData" ).setParameter( "uname", "%in" ).list();
|
||||
assertThat( results.size(), is( 2 ) );
|
||||
gavin = (User) results.get( 0 );
|
||||
assertThat( gavin.getName(), is( "gavin" ) );
|
||||
assertThat( gavin.getSession().size(), is( 2 ) );
|
||||
session.createMutationQuery( "delete SessionAttribute" ).executeUpdate();
|
||||
session.createMutationQuery( "delete User" ).executeUpdate();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@JiraKey("HHH-4294")
|
||||
public void testMap(SessionFactoryScope scope) {
|
||||
Parent parent = new Parent();
|
||||
Child child = new Child();
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
child.setFirstName( "Ben" );
|
||||
parent.getChildren().put( child.getFirstName(), child );
|
||||
child.setParent( parent );
|
||||
session.persist( parent );
|
||||
}
|
||||
);
|
||||
|
||||
// END PREPARE SECTION
|
||||
scope.inSession(
|
||||
session -> {
|
||||
Parent parent2 = session.get( Parent.class, parent.getId() );
|
||||
Child child2 = parent2.getChildren()
|
||||
.get( child.getFirstName() ); // causes SQLGrammarException because of wrong condition: where child0_.PARENT_ID=? and child0_.null=?
|
||||
assertNotNull( child2 );
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@JiraKey("HHH-10874")
|
||||
public void testWhereClauseOnBidirectionalCollection(SessionFactoryScope scope) {
|
||||
School school = new School( 1 );
|
||||
Student gavin = new Student( "gavin", 4 );
|
||||
Student turin = new Student( "turin", 3 );
|
||||
Student mike = new Student( "mike", 5 );
|
||||
Student fred = new Student( "fred", 2 );
|
||||
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
session.persist( school );
|
||||
|
||||
gavin.setSchool( school );
|
||||
turin.setSchool( school );
|
||||
mike.setSchool( school );
|
||||
fred.setSchool( school );
|
||||
|
||||
session.persist( gavin );
|
||||
session.persist( turin );
|
||||
session.persist( mike );
|
||||
session.persist( fred );
|
||||
}
|
||||
);
|
||||
|
||||
scope.inSession(
|
||||
session -> {
|
||||
School school2 = session.get( School.class, 1 );
|
||||
|
||||
assertThat( school2.getStudents().size(), is( 4 ) );
|
||||
|
||||
assertThat( school2.getTopStudents().size(), is( 2 ) );
|
||||
assertTrue( school2.getTopStudents().contains( gavin ) );
|
||||
assertTrue( school2.getTopStudents().contains( mike ) );
|
||||
|
||||
assertThat( school2.getStudentsMap().size(), is( 2 ) );
|
||||
assertTrue( school2.getStudentsMap().containsKey( gavin.getId() ) );
|
||||
assertTrue( school2.getStudentsMap().containsKey( mike.getId() ) );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@FailureExpected(jiraKey = "HHH-3319")
|
||||
public void testWhereClauseOnUnidirectionalCollection(SessionFactoryScope scope) {
|
||||
Championship championship = new Championship( 1 );
|
||||
Student gavin = new Student( "gavin", 4 );
|
||||
Student turin = new Student( "turin", 3 );
|
||||
Student mike = new Student( "mike", 5 );
|
||||
Student fred = new Student( "fred", 2 );
|
||||
|
||||
scope.inTransaction(
|
||||
session -> {
|
||||
session.persist( championship );
|
||||
|
||||
championship.getStudents().add( gavin );
|
||||
championship.getStudents().add( turin );
|
||||
championship.getStudents().add( mike );
|
||||
championship.getStudents().add( fred );
|
||||
|
||||
session.persist( gavin );
|
||||
session.persist( turin );
|
||||
session.persist( mike );
|
||||
session.persist( fred );
|
||||
}
|
||||
);
|
||||
|
||||
scope.inSession(
|
||||
session -> {
|
||||
Championship championship2 = session.get( Championship.class, 1 );
|
||||
assertThat( championship2.getStudents().size(), is( 2 ) );
|
||||
assertTrue( championship2.getStudents().contains( gavin ) );
|
||||
assertTrue( championship2.getStudents().contains( mike ) );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,35 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
|
||||
//$Id: Group.java 7628 2005-07-24 06:55:01Z oneovthafew $
|
||||
package org.hibernate.orm.test.extralazy;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Gavin King
|
||||
*/
|
||||
public class Group {
|
||||
private String name;
|
||||
private Map users = new HashMap();
|
||||
Group() {}
|
||||
public Group(String n) {
|
||||
name = n;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public Map getUsers() {
|
||||
return users;
|
||||
}
|
||||
public void setUsers(Map users) {
|
||||
this.users = users;
|
||||
}
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.orm.test.extralazy;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
|
||||
public class Parent {
|
||||
|
||||
private String id;
|
||||
|
||||
private Map <String, Child> children = new HashMap<String, Child> ();
|
||||
|
||||
public void setChildren(Map <String, Child> children) {
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
public Map <String, Child> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
|
@ -1,65 +0,0 @@
|
|||
package org.hibernate.orm.test.extralazy;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.annotations.LazyCollection;
|
||||
import org.hibernate.annotations.LazyCollectionOption;
|
||||
import org.hibernate.annotations.SQLRestriction;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.MapKey;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "school")
|
||||
public class School {
|
||||
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
@OneToMany(mappedBy = "school")
|
||||
@LazyCollection(LazyCollectionOption.EXTRA)
|
||||
private Set<Student> students = new HashSet<Student>();
|
||||
|
||||
@OneToMany(mappedBy = "school")
|
||||
@LazyCollection(LazyCollectionOption.EXTRA)
|
||||
@SQLRestriction(" gpa >= 4 ")
|
||||
private Set<Student> topStudents = new HashSet<Student>();
|
||||
|
||||
@OneToMany(mappedBy = "school")
|
||||
@LazyCollection(LazyCollectionOption.EXTRA)
|
||||
@SQLRestriction(" gpa >= 4 ")
|
||||
@MapKey
|
||||
private Map<String, Student> studentsMap = new HashMap<>();
|
||||
|
||||
public School() {}
|
||||
|
||||
public School(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Set<Student> getStudents() {
|
||||
return students;
|
||||
}
|
||||
|
||||
public Set<Student> getTopStudents() {
|
||||
return topStudents;
|
||||
}
|
||||
|
||||
public Map<String, Student> getStudentsMap() {
|
||||
return studentsMap;
|
||||
}
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
|
||||
//$Id: SessionAttribute.java 7628 2005-07-24 06:55:01Z oneovthafew $
|
||||
package org.hibernate.orm.test.extralazy;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author Gavin King
|
||||
*/
|
||||
public class SessionAttribute {
|
||||
private Long id;
|
||||
private String name;
|
||||
private String stringData;
|
||||
private Serializable objectData;
|
||||
SessionAttribute() {}
|
||||
public SessionAttribute(String name, Serializable obj) {
|
||||
this.name = name;
|
||||
this.objectData = obj;
|
||||
}
|
||||
public SessionAttribute(String name, String str) {
|
||||
this.name = name;
|
||||
this.stringData = str;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public Serializable getObjectData() {
|
||||
return objectData;
|
||||
}
|
||||
public void setObjectData(Serializable objectData) {
|
||||
this.objectData = objectData;
|
||||
}
|
||||
public String getStringData() {
|
||||
return stringData;
|
||||
}
|
||||
public void setStringData(String stringData) {
|
||||
this.stringData = stringData;
|
||||
}
|
||||
}
|
|
@ -1,81 +0,0 @@
|
|||
package org.hibernate.orm.test.extralazy;
|
||||
|
||||
import java.util.Objects;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "student")
|
||||
public class Student {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
private School school;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private int gpa;
|
||||
|
||||
public Student() {}
|
||||
|
||||
public Student(String firstName, int gpa) {
|
||||
this.firstName = firstName;
|
||||
this.gpa = gpa;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public School getSchool() {
|
||||
return school;
|
||||
}
|
||||
|
||||
public void setSchool(School school) {
|
||||
this.school = school;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public int getGpa() {
|
||||
return gpa;
|
||||
}
|
||||
|
||||
public void setGpa(int gpa) {
|
||||
this.gpa = gpa;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( !( o instanceof Student ) ) {
|
||||
return false;
|
||||
}
|
||||
Student student = (Student) o;
|
||||
return Objects.equals( getFirstName(), student.getFirstName() );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( getFirstName() );
|
||||
}
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
|
||||
//$Id: User.java 7635 2005-07-24 23:04:30Z oneovthafew $
|
||||
package org.hibernate.orm.test.extralazy;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Gavin King
|
||||
*/
|
||||
public class User {
|
||||
private String name;
|
||||
private String password;
|
||||
private Map session = new HashMap();
|
||||
private Set documents = new HashSet();
|
||||
User() {}
|
||||
public User(String n, String pw) {
|
||||
name=n;
|
||||
password = pw;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
public Map getSession() {
|
||||
return session;
|
||||
}
|
||||
public void setSession(Map session) {
|
||||
this.session = session;
|
||||
}
|
||||
public Set getDocuments() {
|
||||
return documents;
|
||||
}
|
||||
public void setDocuments(Set documents) {
|
||||
this.documents = documents;
|
||||
}
|
||||
}
|
|
@ -8,6 +8,12 @@ package org.hibernate.orm.test.fetching;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.annotations.NaturalId;
|
||||
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
|
@ -16,13 +22,6 @@ import jakarta.persistence.ManyToOne;
|
|||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.OrderColumn;
|
||||
|
||||
import org.hibernate.annotations.LazyCollection;
|
||||
import org.hibernate.annotations.LazyCollectionOption;
|
||||
import org.hibernate.annotations.NaturalId;
|
||||
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
|
||||
/**
|
||||
|
@ -78,7 +77,6 @@ public class LazyCollectionTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@OneToMany(mappedBy = "department", cascade = CascadeType.ALL)
|
||||
@OrderColumn(name = "order_id")
|
||||
@LazyCollection(LazyCollectionOption.EXTRA)
|
||||
private List<Employee> employees = new ArrayList<>();
|
||||
|
||||
//Getters and setters omitted for brevity
|
||||
|
|
|
@ -77,7 +77,7 @@ public class PluralAttributeMappingTests {
|
|||
final MappingMetamodel domainModel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
|
||||
final EntityMappingType containerEntityDescriptor = domainModel.getEntityDescriptor( EntityOfSets.class );
|
||||
|
||||
assertThat( containerEntityDescriptor.getNumberOfAttributeMappings(), is( 12 ) );
|
||||
assertThat( containerEntityDescriptor.getNumberOfAttributeMappings(), is( 11 ) );
|
||||
|
||||
final AttributeMapping setOfBasics = containerEntityDescriptor.findAttributeMapping( "setOfBasics" );
|
||||
assertThat( setOfBasics, notNullValue() );
|
||||
|
@ -100,9 +100,6 @@ public class PluralAttributeMappingTests {
|
|||
final AttributeMapping setOfComponents = containerEntityDescriptor.findAttributeMapping( "setOfComponents" );
|
||||
assertThat( setOfComponents, notNullValue() );
|
||||
|
||||
final AttributeMapping extraLazySetOfComponents = containerEntityDescriptor.findAttributeMapping( "extraLazySetOfComponents" );
|
||||
assertThat( extraLazySetOfComponents, notNullValue() );
|
||||
|
||||
final AttributeMapping setOfOneToMany = containerEntityDescriptor.findAttributeMapping( "setOfOneToMany" );
|
||||
assertThat( setOfOneToMany, notNullValue() );
|
||||
|
||||
|
|
|
@ -13,8 +13,6 @@ import java.util.Set;
|
|||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.annotations.Fetch;
|
||||
import org.hibernate.annotations.FetchMode;
|
||||
import org.hibernate.annotations.LazyCollection;
|
||||
import org.hibernate.annotations.LazyCollectionOption;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.internal.util.SerializationHelper;
|
||||
import org.hibernate.proxy.AbstractLazyInitializer;
|
||||
|
@ -217,7 +215,6 @@ public class EntityProxySerializationTest {
|
|||
}
|
||||
|
||||
@OneToMany(targetEntity = ChildEntity.class, mappedBy = "parent")
|
||||
@LazyCollection(LazyCollectionOption.EXTRA)
|
||||
@Fetch(FetchMode.SELECT)
|
||||
public Set<ChildEntity> getChildren() {
|
||||
return children;
|
||||
|
|
|
@ -10,6 +10,10 @@ import java.util.HashSet;
|
|||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import org.hibernate.annotations.SortComparator;
|
||||
import org.hibernate.annotations.SortNatural;
|
||||
|
||||
import jakarta.persistence.CollectionTable;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Convert;
|
||||
|
@ -23,11 +27,6 @@ import jakarta.persistence.OneToMany;
|
|||
import jakarta.persistence.OrderBy;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import org.hibernate.annotations.LazyCollection;
|
||||
import org.hibernate.annotations.LazyCollectionOption;
|
||||
import org.hibernate.annotations.SortComparator;
|
||||
import org.hibernate.annotations.SortNatural;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
|
@ -95,11 +94,6 @@ public class EntityOfSets {
|
|||
@CollectionTable( name = "EntityOfSet_comp1")
|
||||
private Set<SimpleComponent> setOfComponents;
|
||||
|
||||
@ElementCollection
|
||||
@LazyCollection( LazyCollectionOption.EXTRA )
|
||||
@CollectionTable( name = "EntityOfSet_comp2")
|
||||
private Set<SimpleComponent> extraLazySetOfComponents;
|
||||
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
// Entity associations
|
||||
|
||||
|
@ -286,25 +280,6 @@ public class EntityOfSets {
|
|||
}
|
||||
|
||||
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
// setOfExtraLazyComponents
|
||||
|
||||
public Set<SimpleComponent> getExtraLazySetOfComponents() {
|
||||
return extraLazySetOfComponents;
|
||||
}
|
||||
|
||||
public void setExtraLazySetOfComponents(Set<SimpleComponent> extraLazySetOfComponents) {
|
||||
this.extraLazySetOfComponents = extraLazySetOfComponents;
|
||||
}
|
||||
|
||||
public void addExtraLazyComponent(SimpleComponent value) {
|
||||
if ( extraLazySetOfComponents == null ) {
|
||||
extraLazySetOfComponents = new HashSet<>();
|
||||
}
|
||||
extraLazySetOfComponents.add( value );
|
||||
}
|
||||
|
||||
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
// setOfOneToMany
|
||||
|
||||
|
|
Loading…
Reference in New Issue