HHH-17262 Add test for issue

This commit is contained in:
Andrea Boriero 2023-10-09 12:44:37 +02:00 committed by Christian Beikov
parent 8e4ce4ea5c
commit 697b74a807
7 changed files with 186 additions and 0 deletions

View File

@ -0,0 +1,52 @@
package org.hibernate.orm.test.mapping.usertypes.xmlmapping;
import org.hibernate.annotations.Type;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Account {
private Long id;
private String name;
private CurrencyUnit currency;
public Account() {
}
public Account(String name, CurrencyUnit currency) {
this.name = name;
this.currency = currency;
}
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Type( value = CurrencyUnitUserType.class )
@Column( length = 3 )
public CurrencyUnit getCurrency() {
return currency;
}
public void setCurrency(CurrencyUnit currency) {
this.currency = currency;
}
}

View File

@ -0,0 +1,25 @@
package org.hibernate.orm.test.mapping.usertypes.xmlmapping;
public class AccountCurrencyUnit implements CurrencyUnit {
private String code;
private int numericCode;
public AccountCurrencyUnit() {
}
public AccountCurrencyUnit(String code, int numericCode) {
this.code = code;
this.numericCode = numericCode;
}
@Override
public String getCurrencyCode() {
return null;
}
@Override
public int getNumericCode() {
return 0;
}
}

View File

@ -0,0 +1,7 @@
package org.hibernate.orm.test.mapping.usertypes.xmlmapping;
public interface CurrencyUnit{
String getCurrencyCode();
int getNumericCode();
}

View File

@ -0,0 +1,56 @@
package org.hibernate.orm.test.mapping.usertypes.xmlmapping;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.AbstractClassJavaType;
public class CurrencyUnitTypeDescriptor extends AbstractClassJavaType<CurrencyUnit> {
public static final CurrencyUnitTypeDescriptor INSTANCE = new CurrencyUnitTypeDescriptor();
public CurrencyUnitTypeDescriptor() {
super( CurrencyUnit.class );
}
@Override
public String toString(CurrencyUnit value) {
return value.getCurrencyCode();
}
@Override
public CurrencyUnit fromString(CharSequence currencyCode) {
return new CurrencyUnit() {
@Override
public String getCurrencyCode() {
return currencyCode.toString();
}
@Override
public int getNumericCode() {
return 0;
}
};
}
@Override
@SuppressWarnings("unchecked")
public <X> X unwrap(CurrencyUnit currencyUnit, Class<X> type, WrapperOptions options) { // NOSONAR
if ( currencyUnit == null ) {
return null;
}
if ( String.class.isAssignableFrom( type ) ) {
return (X) currencyUnit.getCurrencyCode();
}
throw unknownUnwrap( type );
}
@Override
public <X> CurrencyUnit wrap(X currencyCode, WrapperOptions options) {
if ( currencyCode == null ) {
return null;
}
if ( currencyCode instanceof String ) {
return new AccountCurrencyUnit( currencyCode.toString(), 0 );
}
throw unknownWrap( currencyCode.getClass() );
}
}

View File

@ -0,0 +1,16 @@
package org.hibernate.orm.test.mapping.usertypes.xmlmapping;
import java.util.function.BiConsumer;
import org.hibernate.type.descriptor.java.BasicJavaType;
import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.descriptor.jdbc.VarcharJdbcType;
import org.hibernate.usertype.BaseUserTypeSupport;
public class CurrencyUnitUserType extends BaseUserTypeSupport<CurrencyUnit> {
@Override
protected void resolve(BiConsumer<BasicJavaType<CurrencyUnit>, JdbcType> resolutionConsumer) {
resolutionConsumer.accept( CurrencyUnitTypeDescriptor.INSTANCE, VarcharJdbcType.INSTANCE );
}
}

View File

@ -0,0 +1,23 @@
package org.hibernate.orm.test.mapping.usertypes.xmlmapping;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.Test;
@Jpa(
xmlMappings = "org/hibernate/orm/test/mapping/usertypes/xmlmapping/entities.xml"
)
@JiraKey("HHH-17262")
public class UserTypeTest {
@Test
public void testTypeAnnotationIsDetected(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
Account account = new Account( "first", new AccountCurrencyUnit( "2", 0 ) );
entityManager.persist( account );
}
);
}
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="https://jakarta.ee/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence/orm https://jakarta.ee/xml/ns/persistence/orm/orm_3_1.xsd"
version="3.1">
<entity class="org.hibernate.orm.test.mapping.usertypes.xmlmapping.Account"/>
</entity-mappings>