Skip to content

Commit

Permalink
HHH-13243 Respect @ManyToAny.fetch setting to FetchType.EAGER
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlinp authored and dreab8 committed Jan 28, 2025
1 parent 5b62264 commit f899246
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1529,7 +1529,7 @@ private FetchType getJpaFetchType() {
}

if ( manyToAny != null ) {
return LAZY;
return manyToAny.fetch();
}

throw new AssertionFailure(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.any.annotations;

import org.hibernate.LazyInitializationException;
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.Assertions;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@DomainModel(
annotatedPackageNames = "org.hibernate.orm.test.any.annotations",
annotatedClasses = {
StringProperty.class,
IntegerProperty.class,
PropertySet.class,
LazyPropertySet.class,
}
)
@SessionFactory
@JiraKey( "HHH-13243" )
public class AnyFetchEagerTest {
@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createMutationQuery( "delete StringProperty" ).executeUpdate();
session.createMutationQuery( "delete IntegerProperty" ).executeUpdate();
session.createMutationQuery( "delete PropertySet" ).executeUpdate();
session.createMutationQuery( "delete LazyPropertySet" ).executeUpdate();
}
);
}


@Test
public void testManyToAnyFetchEager(SessionFactoryScope scope) {
scope.inTransaction( s -> {
PropertySet set = new PropertySet( "string" );
Property property = new StringProperty( "name", "Alex" );
set.addGeneralProperty( property );
s.persist( set );
} );

PropertySet result = scope.fromTransaction(
s -> s.createQuery( "select s from PropertySet s where name = :name", PropertySet.class )
.setParameter( "name", "string" )
.getSingleResult() );

assertThat( result ).isNotNull();
assertThat( result.getGeneralProperties() ).isNotNull();
assertThat( result.getGeneralProperties().size() ).isEqualTo( 1 );
assertThat( result.getGeneralProperties().get(0).asString() ).isEqualTo( "Alex" );
}

@Test
public void testManyToAnyFetchLazy(SessionFactoryScope scope) {
scope.inTransaction( s -> {
LazyPropertySet set = new LazyPropertySet( "string" );
Property property = new StringProperty( "name", "Alex" );
set.addGeneralProperty( property );
s.persist( set );
} );

LazyPropertySet result = scope.fromTransaction(
s -> s.createQuery( "select s from LazyPropertySet s where name = :name", LazyPropertySet.class )
.setParameter( "name", "string" )
.getSingleResult() );

assertThat( result ).isNotNull();
assertThat( result.getGeneralProperties() ).isNotNull();

try {
result.getGeneralProperties().get(0);
Assertions.fail( "should not get the property string after session closed." );
}
catch (LazyInitializationException e) {
// expected
}
catch (Exception e) {
Assertions.fail( "should not throw exception other than LazyInitializationException." );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package org.hibernate.orm.test.any.annotations;

import jakarta.persistence.JoinTable;
import org.hibernate.annotations.Any;
import org.hibernate.annotations.AnyDiscriminator;
import org.hibernate.annotations.AnyDiscriminatorValue;
Expand All @@ -19,13 +20,18 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.Table;
import org.hibernate.annotations.ManyToAny;

import java.util.ArrayList;
import java.util.List;

@Entity
@Table( name = "lazy_property_set" )
public class LazyPropertySet {
private Integer id;
private String name;
private Property someProperty;
private List<Property> generalProperties = new ArrayList<Property>();

public LazyPropertySet() {
super();
Expand Down Expand Up @@ -68,4 +74,26 @@ public Property getSomeProperty() {
public void setSomeProperty(Property someProperty) {
this.someProperty = someProperty;
}

@ManyToAny(
fetch = FetchType.LAZY )
@Column( name = "property_type" )
@AnyDiscriminator( DiscriminatorType.STRING )
@AnyKeyJavaClass( Integer.class )
@AnyDiscriminatorValue( discriminator = "S", entity = StringProperty.class )
@AnyDiscriminatorValue( discriminator = "I", entity = IntegerProperty.class )
@Cascade( { org.hibernate.annotations.CascadeType.ALL } )
@JoinTable( name = "lazy_obj_properties", joinColumns = @JoinColumn( name = "obj_id" ),
inverseJoinColumns = @JoinColumn( name = "property_id" ) )
public List<Property> getGeneralProperties() {
return generalProperties;
}

public void setGeneralProperties(List<Property> generalProperties) {
this.generalProperties = generalProperties;
}

public void addGeneralProperty(Property property) {
this.generalProperties.add( property );
}
}

0 comments on commit f899246

Please sign in to comment.