Skip to content

HHH-19589 : Test demonstrating that @Converter is ignored when @TypeRegistration is present #10486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.mapping.converted.converter;

import java.util.BitSet;

import org.hibernate.annotations.TypeRegistration;
import org.hibernate.orm.test.mapping.basic.bitset.BitSetHelper;
import org.hibernate.orm.test.mapping.basic.bitset.BitSetUserType;

import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Convert;
import jakarta.persistence.Converter;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

import static org.assertj.core.api.Assertions.assertThat;

/**
* <pre>
* The @Converter should take precedence over @TypeRegistration.
* This test shows that this is not the case.
*
* To ensure that the @Converter is taken into account without @TypeRegistration, you just need to remove the @TypeRegistration.
* </pre>
*
* @author Vincent Bouthinon
*/
@DomainModel(
annotatedClasses = {
ConverterOverrideTypeRegisttrationTest.SimpleEntity.class
}
)
@SessionFactory
@JiraKey(value = "HHH-19589")
public class ConverterOverrideTypeRegisttrationTest {

@Test
void test(SessionFactoryScope scope) {
scope.inTransaction( session -> {
final SimpleEntity object = new SimpleEntity( 77L );
BitSet bitSet = new BitSet();
bitSet.set( 0, true );
object.setBitSet( bitSet );
session.persist( object );
session.flush();
session.clear();
SimpleEntity simpleEntity = session.find( SimpleEntity.class, object.id );
assertThat( simpleEntity.getBitSet().get( 7 ) ).isTrue();
} );
}


@Entity(name = "SimpleEntity")
@TypeRegistration(basicClass = BitSet.class, userType = BitSetUserType.class) // Remove this annotation to test the use of @Converter
public static class SimpleEntity {

@Id
private Long id;
@Convert(converter = BitSetConverter.class)
private BitSet bitSet;

public SimpleEntity() {
}

public SimpleEntity(Long id) {
this.id = id;
}

public BitSet getBitSet() {
return bitSet;
}

public void setBitSet(final BitSet bitSet) {
this.bitSet = bitSet;
}
}

@Converter
public static class BitSetConverter implements AttributeConverter<BitSet, String> {

@Override
public String convertToDatabaseColumn(final BitSet attribute) {
return BitSetHelper.bitSetToString( attribute );
}

@Override
public BitSet convertToEntityAttribute(final String dbData) {
BitSet bitSet = new BitSet();
bitSet.set( 7, true );
return bitSet;
}
}
}