Skip to content

Commit

Permalink
HHH-11216 - @ElementCollection for enumerations is failing when enabl…
Browse files Browse the repository at this point in the history
…ing insert ordering
  • Loading branch information
dreab8 authored and vladmihalcea committed Nov 23, 2016
1 parent 801974f commit aa5f893
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1146,8 +1146,12 @@ private void addParentChildEntityNames(AbstractEntityInsertAction action, BatchI
}
else if ( type.isCollectionType() && value != null ) {
CollectionType collectionType = (CollectionType) type;
String entityName = collectionType.getAssociatedEntityName( ( (SessionImplementor) action.getSession() ).getSessionFactory() );
batchIdentifier.getChildEntityNames().add( entityName );
final SessionFactoryImplementor sessionFactory = ( (SessionImplementor) action.getSession() )
.getSessionFactory();
if ( collectionType.getElementType( sessionFactory ).isEntityType() ) {
String entityName = collectionType.getAssociatedEntityName( sessionFactory );
batchIdentifier.getChildEntityNames().add( entityName );
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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.test.insertordering;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.cfg.Environment;

import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider;
import org.junit.Test;

import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;

/**
* @author Andrea Boriero
*/
@TestForIssue(jiraKey = "HHH-11216")
public class ElementCollectionTest extends BaseNonConfigCoreFunctionalTestCase {

@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {Task.class};
}

private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider();

@Override
protected void addSettings(Map settings) {
settings.put( Environment.ORDER_INSERTS, "true" );
settings.put( Environment.STATEMENT_BATCH_SIZE, "10" );
settings.put(
org.hibernate.cfg.AvailableSettings.CONNECTION_PROVIDER,
connectionProvider
);
}

@Test
public void testBatchOrdering() {
doInHibernate( this::sessionFactory, session -> {
Task task = new Task();
task.addCategory(Category.A);
session.persist( task );

Task task1 = new Task();
task1.addCategory(Category.A);
session.persist( task1 );
} );
}

@Override
public void releaseResources() {
super.releaseResources();
connectionProvider.stop();
}

@Entity
@Table(name = "TASK")
public static class Task {

@Id
@SequenceGenerator(name = "ID", sequenceName = "TASK_SEQ")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ID")
private int id;

@ElementCollection(targetClass = Category.class)
@CollectionTable(name = "TASK_CATEGORY", joinColumns = {@JoinColumn(name = "TASK_ID")})
@Enumerated(EnumType.STRING)
private final Set<Category> categories = new HashSet<>();

public void addCategory(Category c) {
categories.add( c );
}
}

public enum Category {

A( "CAT1" ), B( "CAT2" );

private String name;

Category(final String name) {
this.name = name;
}

public String getName() {
return name;
}
}
}

0 comments on commit aa5f893

Please sign in to comment.