HHH-11409 - Bind registered collection types using their type handler

Try to replicate issue
This commit is contained in:
Yordan Gigov 2017-01-19 10:08:56 +02:00 committed by Vlad Mihalcea
parent 7f34ac128b
commit d9e5edc9c0
4 changed files with 210 additions and 0 deletions

View File

@ -0,0 +1,9 @@
package org.hibernate.test.type.contributor;
import java.util.ArrayList;
/**
* @author Vlad Mihalcea
*/
public class Array extends ArrayList<String> {
}

View File

@ -0,0 +1,36 @@
package org.hibernate.test.type.contributor;
import org.hibernate.dialect.Dialect;
import org.hibernate.type.AbstractSingleColumnStandardBasicType;
import org.hibernate.type.DiscriminatorType;
import org.hibernate.type.descriptor.sql.VarcharTypeDescriptor;
/**
* @author Vlad Mihalcea
*/
public class ArrayType
extends AbstractSingleColumnStandardBasicType<Array>
implements DiscriminatorType<Array> {
public static final ArrayType INSTANCE = new ArrayType();
public ArrayType() {
super( VarcharTypeDescriptor.INSTANCE, ArrayTypeDescriptor.INSTANCE );
}
@Override
public Array stringToObject(String xml) throws Exception {
return fromString( xml );
}
@Override
public String objectToSQLString(Array value, Dialect dialect) throws Exception {
return toString( value );
}
@Override
public String getName() {
return "comma-separated-array";
}
}

View File

@ -0,0 +1,95 @@
/*
* 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.type.contributor;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Type;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.test.collection.custom.basic.MyList;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertTrue;
/**
* @author Vlad Mihalcea
*/
@TestForIssue( jiraKey = "HHH-11409" )
public class ArrayTypeContributorTest extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { CorporateUser.class };
}
@Override
protected Configuration constructAndConfigureConfiguration() {
Configuration configuration = super.constructAndConfigureConfiguration();
configuration.registerTypeContributor( (typeContributions, serviceRegistry) -> {
typeContributions.contributeType( ArrayType.INSTANCE,
new String[] {
MyList.class.getName(),
ArrayType.INSTANCE.getName()
}
);
} );
return configuration;
}
@Test
public void test() {
doInHibernate( this::sessionFactory, session -> {
CorporateUser user = new CorporateUser();
user.setUserName( "Vlad" );
session.persist( user );
user.getEmailAddresses().add( "vlad@hibernate.info" );
user.getEmailAddresses().add( "vlad@hibernate.net" );
} );
doInHibernate( this::sessionFactory, session -> {
List<CorporateUser> users = session.createQuery(
"select u from CorporateUser u where u.emailAddresses = :address", CorporateUser.class )
.setParameter( "address", new Array(), ArrayType.INSTANCE )
.getResultList();
assertTrue( users.isEmpty() );
} );
}
@Entity(name = "CorporateUser")
public static class CorporateUser {
@Id
private String userName;
@Type(type = "comma-separated-array")
private Array emailAddresses = new Array();
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Array getEmailAddresses() {
return emailAddresses;
}
}
}

View File

@ -0,0 +1,70 @@
package org.hibernate.test.type.contributor;
import java.util.Arrays;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.AbstractTypeDescriptor;
/**
* @author Vlad Mihalcea
*/
public class ArrayTypeDescriptor extends AbstractTypeDescriptor<Array> {
private static final String DELIMITER = ",";
public static final ArrayTypeDescriptor INSTANCE = new ArrayTypeDescriptor();
public ArrayTypeDescriptor() {
super( Array.class );
}
@Override
public String toString(Array value) {
StringBuilder builder = new StringBuilder();
for ( String token : value ) {
if ( builder.length() > 0 ) {
builder.append( DELIMITER );
}
builder.append( token );
}
return builder.toString();
}
@Override
public Array fromString(String string) {
if ( string == null || string.isEmpty() ) {
return null;
}
String[] tokens = string.split( DELIMITER );
Array array = new Array();
array.addAll( Arrays.asList(tokens) );
return array;
}
@SuppressWarnings({"unchecked"})
public <X> X unwrap(Array value, Class<X> type, WrapperOptions options) {
if ( value == null ) {
return null;
}
if ( Array.class.isAssignableFrom( type ) ) {
return (X) value;
}
if ( String.class.isAssignableFrom( type ) ) {
return (X) toString( value);
}
throw unknownUnwrap( type );
}
public <X> Array wrap(X value, WrapperOptions options) {
if ( value == null ) {
return null;
}
if ( String.class.isInstance( value ) ) {
return fromString( (String) value );
}
if ( Array.class.isInstance( value ) ) {
return (Array) value;
}
throw unknownWrap( value.getClass() );
}
}