Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-9615 - Allow AttributeConverter on attributes marked as Lob
- Loading branch information
Showing
4 changed files
with
217 additions
and
5 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
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
71 changes: 71 additions & 0 deletions
71
hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/LobTypeMappings.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,71 @@ | ||
/* | ||
* 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.type.descriptor.sql; | ||
|
||
import java.sql.Types; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
import org.hibernate.internal.util.collections.BoundedConcurrentHashMap; | ||
import org.hibernate.type.descriptor.JdbcTypeNameMapper; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
/** | ||
* @author Steve Ebersole | ||
*/ | ||
public class LobTypeMappings { | ||
private static final Logger log = Logger.getLogger( LobTypeMappings.class ); | ||
|
||
/** | ||
* Singleton access | ||
*/ | ||
public static final LobTypeMappings INSTANCE = new LobTypeMappings(); | ||
|
||
private final Map<Integer,Integer> lobCodeByNonLobCode; | ||
|
||
private LobTypeMappings() { | ||
this.lobCodeByNonLobCode = new BoundedConcurrentHashMap<Integer, Integer>(); | ||
|
||
// BLOB mappings | ||
this.lobCodeByNonLobCode.put( Types.BLOB, Types.BLOB ); | ||
this.lobCodeByNonLobCode.put( Types.BINARY, Types.BLOB ); | ||
this.lobCodeByNonLobCode.put( Types.VARBINARY, Types.BLOB ); | ||
this.lobCodeByNonLobCode.put( Types.LONGVARBINARY, Types.BLOB ); | ||
|
||
// CLOB mappings | ||
this.lobCodeByNonLobCode.put( Types.CLOB, Types.CLOB ); | ||
this.lobCodeByNonLobCode.put( Types.CHAR, Types.CLOB ); | ||
this.lobCodeByNonLobCode.put( Types.VARCHAR, Types.CLOB ); | ||
this.lobCodeByNonLobCode.put( Types.LONGVARCHAR, Types.CLOB ); | ||
|
||
// NCLOB mappings | ||
this.lobCodeByNonLobCode.put( Types.NCLOB, Types.NCLOB ); | ||
this.lobCodeByNonLobCode.put( Types.NCHAR, Types.NCLOB ); | ||
this.lobCodeByNonLobCode.put( Types.NVARCHAR, Types.NCLOB ); | ||
this.lobCodeByNonLobCode.put( Types.LONGNVARCHAR, Types.NCLOB ); | ||
} | ||
|
||
public boolean hasCorrespondingLobCode(int jdbcTypeCode) { | ||
return lobCodeByNonLobCode.containsKey( jdbcTypeCode ); | ||
} | ||
|
||
public int getCorrespondingLobCode(int jdbcTypeCode) { | ||
Integer lobTypeCode = lobCodeByNonLobCode.get( jdbcTypeCode ); | ||
if ( lobTypeCode == null ) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
Locale.ROOT, | ||
"JDBC type-code [%s (%s)] not known to have a corresponding LOB equivalent", | ||
jdbcTypeCode, | ||
JdbcTypeNameMapper.getTypeName( jdbcTypeCode ) | ||
) | ||
); | ||
} | ||
return lobTypeCode; | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
hibernate-core/src/test/java/org/hibernate/test/converter/AndLobTest.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,87 @@ | ||
/* | ||
* 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.converter; | ||
|
||
import java.sql.Types; | ||
import javax.persistence.AttributeConverter; | ||
import javax.persistence.Convert; | ||
import javax.persistence.Converter; | ||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
import javax.persistence.Lob; | ||
|
||
import org.hibernate.boot.Metadata; | ||
import org.hibernate.boot.MetadataSources; | ||
import org.hibernate.boot.registry.StandardServiceRegistry; | ||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; | ||
import org.hibernate.type.Type; | ||
import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter; | ||
|
||
import org.hibernate.testing.junit4.BaseUnitTestCase; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* Test mapping a model with an attribute combining {@code @Lob} with an AttributeConverter. | ||
* <p/> | ||
* Originally developed to diagnose HHH-9615 | ||
* | ||
* @author Steve Ebersole | ||
*/ | ||
public class AndLobTest extends BaseUnitTestCase { | ||
private StandardServiceRegistry ssr; | ||
|
||
@Before | ||
public void before() { | ||
ssr = new StandardServiceRegistryBuilder().build(); | ||
} | ||
|
||
@After | ||
public void after() { | ||
if ( ssr != null ) { | ||
StandardServiceRegistryBuilder.destroy( ssr ); | ||
} | ||
} | ||
|
||
@Test | ||
public void testMappingAttributeWithLobAndAttributeConverter() { | ||
final Metadata metadata = new MetadataSources( ssr ) | ||
.addAnnotatedClass( EntityImpl.class ) | ||
.buildMetadata(); | ||
|
||
final Type type = metadata.getEntityBinding( EntityImpl.class.getName() ).getProperty( "status" ).getType(); | ||
final AttributeConverterTypeAdapter concreteType = assertTyping( AttributeConverterTypeAdapter.class, type ); | ||
assertEquals( Types.BLOB, concreteType.getSqlTypeDescriptor().getSqlType() ); | ||
} | ||
|
||
@Converter | ||
public static class ConverterImpl implements AttributeConverter<String, Integer> { | ||
@Override | ||
public Integer convertToDatabaseColumn(String attribute) { | ||
return attribute.length(); | ||
} | ||
|
||
@Override | ||
public String convertToEntityAttribute(Integer dbData) { | ||
return ""; | ||
} | ||
} | ||
|
||
@Entity | ||
public static class EntityImpl { | ||
@Id | ||
private Integer id; | ||
|
||
@Lob | ||
@Convert(converter = ConverterImpl.class) | ||
private String status; | ||
} | ||
} |