Skip to content

Commit

Permalink
Fix CollectionLoaderBatchKey parameters binding error
Browse files Browse the repository at this point in the history
  • Loading branch information
dreab8 committed May 27, 2021
1 parent 629b318 commit 21c607c
Show file tree
Hide file tree
Showing 21 changed files with 418 additions and 355 deletions.
Expand Up @@ -326,7 +326,7 @@ public Object[] getCollectionBatch(
}

if ( checkForEnd && i == end ) {
return keys; //the first key found after the given key
return resize( keys, i ); //the first key found after the given key
}

final boolean isEqual = collectionPersister.getKeyType().isEqual(
Expand All @@ -352,7 +352,15 @@ else if ( !isCached( ce.getLoadedKey(), collectionPersister ) ) {
}
}
}
return keys; //we ran out of keys to try
return resize( keys, i ); //we ran out of keys to try
}

private Object[] resize(Object[] keys, int size) {
Object[] result = new Object[size];
for ( int i = 0; i < size; i++ ) {
result[i] = keys[i];
}
return result;
}

private boolean isCached(Object collectionKey, CollectionPersister persister) {
Expand Down
Expand Up @@ -174,36 +174,37 @@ private void batchLoad(
jdbcParameters,
session
);

jdbcServices.getJdbcSelectExecutor().list(
jdbcSelect,
jdbcParameterBindings,
new ExecutionContext() {
@Override
public SharedSessionContractImplementor getSession() {
return session;
}

@Override
public QueryOptions getQueryOptions() {
return QueryOptions.NONE;
}

@Override
public QueryParameterBindings getQueryParameterBindings() {
return QueryParameterBindings.NO_PARAM_BINDINGS;
}

@Override
public Callback getCallback() {
return null;
}
},
RowTransformerPassThruImpl.instance(),
true
);
}

jdbcServices.getJdbcSelectExecutor().list(
jdbcSelect,
jdbcParameterBindings,
new ExecutionContext() {
@Override
public SharedSessionContractImplementor getSession() {
return session;
}

@Override
public QueryOptions getQueryOptions() {
return QueryOptions.NONE;
}

@Override
public QueryParameterBindings getQueryParameterBindings() {
return QueryParameterBindings.NO_PARAM_BINDINGS;
}

@Override
public Callback getCallback() {
return null;
}
},
RowTransformerPassThruImpl.instance(),
true
);


assert offset == jdbcParameters.size();

// prepare for the next round...
Expand Down
Expand Up @@ -78,7 +78,6 @@ public void cleanupTest(EntityManagerFactoryScope scope) throws Exception {

@Test
@TestForIssue(jiraKey = "HHH-11144")
@FailureExpected( jiraKey = "HHH-11144" )
public void testInitializingSecondCollection(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
Expand Down
Expand Up @@ -4,7 +4,7 @@
* 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.test.manytomany;
package org.hibernate.orm.test.manytomany;

import org.hibernate.annotations.Where;

Expand Down
Expand Up @@ -4,7 +4,7 @@
* 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.test.manytomany;
package org.hibernate.orm.test.manytomany;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
Expand Down
Expand Up @@ -6,7 +6,7 @@
*/

//$Id: Group.java 7085 2005-06-08 17:59:47Z oneovthafew $
package org.hibernate.test.manytomany;
package org.hibernate.orm.test.manytomany;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
Expand Down
Expand Up @@ -4,11 +4,10 @@
* 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.test.manytomany;
package org.hibernate.orm.test.manytomany;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
Expand All @@ -17,29 +16,37 @@
import javax.persistence.ManyToMany;

import org.hibernate.annotations.NaturalId;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;

import org.hibernate.testing.FailureExpected;
import org.junit.Test;

import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.FailureExpected;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

/**
* @author Vlad Mihalcea
*/
public class ManyToManyBidirectionalTest extends BaseEntityManagerFunctionalTestCase {

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Person.class,
Address.class,
};
@Jpa(
annotatedClasses = {
ManyToManyBidirectionalTest.Person.class,
ManyToManyBidirectionalTest.Address.class,
}
)
public class ManyToManyBidirectionalTest {

@AfterEach
public void tearDown(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
entityManager.createQuery( "delete from Person" ).executeUpdate();
entityManager.createQuery( "delete from Address" ).executeUpdate();
}
);
}

@Test
public void testRemoveOwnerSide() {
Person _person1 = doInJPA( this::entityManagerFactory, entityManager -> {
public void testRemoveOwnerSide(EntityManagerFactoryScope scope) {
Person _person1 = scope.fromTransaction( entityManager -> {
Person person1 = new Person( "ABC-123" );
Person person2 = new Person( "DEF-456" );

Expand All @@ -57,17 +64,17 @@ public void testRemoveOwnerSide() {
return person1;
} );

doInJPA( this::entityManagerFactory, entityManager -> {
scope.inTransaction( entityManager -> {
Person person1 = entityManager.find( Person.class, _person1.id );

entityManager.remove( person1 );
} );
}

@Test
@FailureExpected( jiraKey = "HHH-12239")
public void testRemoveMappedBySide() {
Address _address1 = doInJPA( this::entityManagerFactory, entityManager -> {
@FailureExpected(jiraKey = "HHH-12239")
public void testRemoveMappedBySide(EntityManagerFactoryScope scope) {
Address _address1 = scope.fromTransaction( entityManager -> {
Person person1 = new Person( "ABC-123" );
Person person2 = new Person( "DEF-456" );

Expand All @@ -85,7 +92,7 @@ public void testRemoveMappedBySide() {
return address1;
} );

doInJPA( this::entityManagerFactory, entityManager -> {
scope.inTransaction( entityManager -> {
Address address1 = entityManager.find( Address.class, _address1.id );

entityManager.remove( address1 );
Expand All @@ -102,7 +109,7 @@ public static class Person {
@NaturalId
private String registrationNumber;

@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
private List<Address> addresses = new ArrayList<>();

public Person() {
Expand Down
Expand Up @@ -4,66 +4,67 @@
* 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.test.manytomany;
package org.hibernate.orm.test.manytomany;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.JoinType;

import org.hibernate.Hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;

import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @author Gavin King
*/
public class ManyToManyTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] { "manytomany/UserGroup.hbm.xml" };
}

@Override
public void configure(Configuration cfg) {
cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false");
}
@DomainModel(
xmlMappings = "org/hibernate/orm/test/manytomany/UserGroup.hbm.xml"
)
@SessionFactory
@ServiceRegistry(
settings = @Setting(name = Environment.USE_SECOND_LEVEL_CACHE, value = "false")
)
public class ManyToManyTest {

@Test
public void testManyToManyWithFormula() {
User user = new User("gavin", "jboss");
Group seamGroup = new Group("seam", "jboss");
Group hbGroup = new Group("hibernate", "jboss");
inTransaction(
public void testManyToManyWithFormula(SessionFactoryScope scope) {
User user = new User( "gavin", "jboss" );
Group seamGroup = new Group( "seam", "jboss" );
Group hbGroup = new Group( "hibernate", "jboss" );
scope.inTransaction(
s -> {
user.getGroups().add(seamGroup);
user.getGroups().add(hbGroup);
seamGroup.getUsers().add(user);
hbGroup.getUsers().add(user);
s.persist(user);
s.persist(seamGroup);
s.persist(hbGroup);
user.getGroups().add( seamGroup );
user.getGroups().add( hbGroup );
seamGroup.getUsers().add( user );
hbGroup.getUsers().add( user );
s.persist( user );
s.persist( seamGroup );
s.persist( hbGroup );
}
);

inTransaction(
scope.inTransaction(
s -> {
User gavin = s.get(User.class, user);
User gavin = s.get( User.class, user );
assertFalse( Hibernate.isInitialized( gavin.getGroups() ) );
assertEquals( 2, gavin.getGroups().size() );
Group hb = s.get(Group.class, hbGroup);
Group hb = s.get( Group.class, hbGroup );
assertFalse( Hibernate.isInitialized( hb.getUsers() ) );
assertEquals( 1, hb.getUsers().size() );
}
);

inTransaction(
scope.inTransaction(
s -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<User> criteria = criteriaBuilder.createQuery( User.class );
Expand All @@ -82,7 +83,7 @@ public void testManyToManyWithFormula() {
}
);

inTransaction(
scope.inTransaction(
s -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<User> criteria = criteriaBuilder.createQuery( User.class );
Expand All @@ -102,9 +103,10 @@ public void testManyToManyWithFormula() {
}
);

inTransaction(
scope.inTransaction(
s -> {
User gavin = (User) s.createQuery("from User u join fetch u.groups g join fetch g.users").uniqueResult();
User gavin = (User) s.createQuery( "from User u join fetch u.groups g join fetch g.users" )
.uniqueResult();
assertTrue( Hibernate.isInitialized( gavin.getGroups() ) );
assertEquals( 2, gavin.getGroups().size() );
Group group = (Group) gavin.getGroups().iterator().next();
Expand All @@ -113,27 +115,28 @@ public void testManyToManyWithFormula() {
}
);

inTransaction(
scope.inTransaction(
s -> {
User gavin = s.get(User.class, user);
Group hb = s.get(Group.class, hbGroup);
gavin.getGroups().remove(hb);
User gavin = s.get( User.class, user );
Group hb = s.get( Group.class, hbGroup );
gavin.getGroups().remove( hb );
}
);

inTransaction(
scope.inTransaction(
s -> {
User gavin = s.get(User.class, user);
assertEquals( gavin.getGroups().size(), 1 );
Group hb = s.get(Group.class, hbGroup);
assertEquals( hb.getUsers().size(), 0 );
User gavin = s.get( User.class, user );
assertEquals( 1, gavin.getGroups().size() );
Group hb = s.get( Group.class, hbGroup );
assertEquals( 0, hb.getUsers().size() );
}
);
inTransaction(

scope.inTransaction(
s -> {
s.delete(user);
s.delete( user );
s.flush();
s.createQuery("delete from Group").executeUpdate();
s.createQuery( "delete from Group" ).executeUpdate();
}
);
}
Expand Down

0 comments on commit 21c607c

Please sign in to comment.