Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-11076 - Lazy collections ignore filters when allowLoadOutsideTran…
…saction is true
- Loading branch information
1 parent
87e69c9
commit ae40de1
Showing
3 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
hibernate-core/src/test/java/org/hibernate/jpa/test/hibernateFilters/AccountGroup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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.jpa.test.hibernateFilters; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.OneToMany; | ||
|
||
import org.hibernate.annotations.Filter; | ||
|
||
/** | ||
* @author Vlad Mihalcea | ||
*/ | ||
@Entity | ||
public class AccountGroup { | ||
|
||
@Id | ||
private Long id; | ||
|
||
@OneToMany | ||
@JoinColumn(name = "group_id") | ||
@Filter( name = "byRegion", condition = "region_cd = :region" ) | ||
private List<Account> accounts = new ArrayList<>(); | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public List<Account> getAccounts() { | ||
return accounts; | ||
} | ||
|
||
public void setAccounts(List<Account> accounts) { | ||
this.accounts = accounts; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
.../hibernate/jpa/test/hibernateFilters/ProxyPreservingFiltersOutsideInitialSessionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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.jpa.test.hibernateFilters; | ||
|
||
import java.util.Map; | ||
|
||
import org.hibernate.Session; | ||
import org.hibernate.cfg.AvailableSettings; | ||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author Vlad Mihalcea | ||
*/ | ||
public class ProxyPreservingFiltersOutsideInitialSessionTest | ||
extends BaseEntityManagerFunctionalTestCase { | ||
@Override | ||
public Class[] getAnnotatedClasses() { | ||
return new Class[] { | ||
AccountGroup.class, | ||
Account.class | ||
}; | ||
} | ||
|
||
@Override | ||
protected Map buildSettings() { | ||
Map settings = super.buildSettings(); | ||
settings.put( AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, "true" ); | ||
return settings; | ||
} | ||
|
||
@Test | ||
public void testPreserveFilters() { | ||
|
||
doInJPA( this::entityManagerFactory, entityManager -> { | ||
AccountGroup accountGroup = new AccountGroup(); | ||
accountGroup.setId( 1L ); | ||
entityManager.persist( accountGroup ); | ||
|
||
Account account = new Account(); | ||
account.setName( "A1" ); | ||
account.setRegionCode( "Europe" ); | ||
entityManager.persist( account ); | ||
accountGroup.getAccounts().add( account ); | ||
|
||
account = new Account(); | ||
account.setName( "A2" ); | ||
account.setRegionCode( "Europe" ); | ||
entityManager.persist( account ); | ||
accountGroup.getAccounts().add( account ); | ||
|
||
account = new Account(); | ||
account.setName( "A3" ); | ||
account.setRegionCode( "US" ); | ||
entityManager.persist( account ); | ||
accountGroup.getAccounts().add( account ); | ||
} ); | ||
|
||
AccountGroup group = doInJPA( this::entityManagerFactory, entityManager -> { | ||
entityManager.unwrap( Session.class ).enableFilter( "byRegion" ).setParameter( "region", "US" ); | ||
return entityManager.find( AccountGroup.class, 1L ); | ||
} ); | ||
|
||
assertEquals(1, group.getAccounts().size()); | ||
} | ||
} |