Search in sources :

Example 1 with CustomType

use of org.hibernate.type.CustomType in project hibernate-orm by hibernate.

the class BasicTypeRegistryTest method testRegisteringUserTypes.

@Test
public void testRegisteringUserTypes() {
    BasicTypeRegistry registry = new BasicTypeRegistry();
    registry.register(new TotallyIrrelevantUserType(), new String[] { "key" });
    BasicType type = registry.getRegisteredType("key");
    assertNotNull(type);
    assertEquals(CustomType.class, type.getClass());
    assertEquals(TotallyIrrelevantUserType.class, ((CustomType) type).getUserType().getClass());
    registry.register(new TotallyIrrelevantCompositeUserType(), new String[] { "key" });
    type = registry.getRegisteredType("key");
    assertNotNull(type);
    assertEquals(CompositeCustomType.class, type.getClass());
    assertEquals(TotallyIrrelevantCompositeUserType.class, ((CompositeCustomType) type).getUserType().getClass());
    type = registry.getRegisteredType(UUID.class.getName());
    assertSame(UUIDBinaryType.INSTANCE, type);
    registry.register(new TotallyIrrelevantUserType(), new String[] { UUID.class.getName() });
    type = registry.getRegisteredType(UUID.class.getName());
    assertNotSame(UUIDBinaryType.INSTANCE, type);
    assertEquals(CustomType.class, type.getClass());
}
Also used : CustomType(org.hibernate.type.CustomType) CompositeCustomType(org.hibernate.type.CompositeCustomType) BasicType(org.hibernate.type.BasicType) AbstractSingleColumnStandardBasicType(org.hibernate.type.AbstractSingleColumnStandardBasicType) CompositeCustomType(org.hibernate.type.CompositeCustomType) BasicTypeRegistry(org.hibernate.type.BasicTypeRegistry) UUID(java.util.UUID) Test(org.junit.Test)

Example 2 with CustomType

use of org.hibernate.type.CustomType in project hibernate-orm by hibernate.

the class EnumeratedWithMappedSuperclassTest method testHHH10128.

@Test
public void testHHH10128() {
    final Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(Entity.class).addAnnotatedClass(DescriptionEntity.class).addAnnotatedClass(AddressLevel.class).buildMetadata();
    final PersistentClass addressLevelBinding = metadata.getEntityBinding(AddressLevel.class.getName());
    final Property natureProperty = addressLevelBinding.getProperty("nature");
    CustomType customType = assertTyping(CustomType.class, natureProperty.getType());
    EnumType enumType = assertTyping(EnumType.class, customType.getUserType());
    assertEquals(Types.VARCHAR, enumType.sqlTypes()[0]);
    SessionFactoryImplementor sf = (SessionFactoryImplementor) metadata.buildSessionFactory();
    try {
        EntityPersister p = sf.getEntityPersister(AddressLevel.class.getName());
        CustomType runtimeType = assertTyping(CustomType.class, p.getPropertyType("nature"));
        EnumType runtimeEnumType = assertTyping(EnumType.class, runtimeType.getUserType());
        assertEquals(Types.VARCHAR, runtimeEnumType.sqlTypes()[0]);
    } finally {
        sf.close();
    }
}
Also used : CustomType(org.hibernate.type.CustomType) EntityPersister(org.hibernate.persister.entity.EntityPersister) EnumType(org.hibernate.type.EnumType) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) Property(org.hibernate.mapping.Property) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test)

Example 3 with CustomType

use of org.hibernate.type.CustomType in project hibernate-orm by hibernate.

the class OrmXmlEnumTypeTest method testOrmXmlDefinedEnumType.

@Test
public void testOrmXmlDefinedEnumType() {
    StandardServiceRegistry ssr = ServiceRegistryBuilder.buildServiceRegistry();
    try {
        MetadataSources ms = new MetadataSources(ssr);
        ms.addResource("org/hibernate/test/annotations/enumerated/ormXml/orm.xml");
        Metadata metadata = ms.buildMetadata();
        Type bindingPropertyType = metadata.getEntityBinding(BookWithOrmEnum.class.getName()).getProperty("bindingStringEnum").getType();
        CustomType customType = ExtraAssertions.assertTyping(CustomType.class, bindingPropertyType);
        EnumType enumType = ExtraAssertions.assertTyping(EnumType.class, customType.getUserType());
        assertFalse(enumType.isOrdinal());
    } finally {
        ServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : CustomType(org.hibernate.type.CustomType) CustomType(org.hibernate.type.CustomType) EnumType(org.hibernate.type.EnumType) Type(org.hibernate.type.Type) EnumType(org.hibernate.type.EnumType) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 4 with CustomType

use of org.hibernate.type.CustomType in project hibernate-orm by hibernate.

the class EnumeratedSmokeTest method validateEnumMapping.

private void validateEnumMapping(Property property, EnumType expectedJpaEnumType) {
    assertThat(property.getType(), instanceOf(CustomType.class));
    final CustomType customType = (CustomType) property.getType();
    assertThat(customType.getUserType(), instanceOf(org.hibernate.type.EnumType.class));
    final org.hibernate.type.EnumType hibernateMappingEnumType = (org.hibernate.type.EnumType) customType.getUserType();
    assertThat(hibernateMappingEnumType.isOrdinal(), is(expectedJpaEnumType == EnumType.ORDINAL));
    assertThat(hibernateMappingEnumType.sqlTypes().length, is(1));
    assertThat(hibernateMappingEnumType.sqlTypes()[0], is(expectedJpaEnumType == EnumType.ORDINAL ? Types.INTEGER : Types.VARCHAR));
}
Also used : CustomType(org.hibernate.type.CustomType) EnumType(javax.persistence.EnumType)

Example 5 with CustomType

use of org.hibernate.type.CustomType in project dhis2-core by dhis2.

the class HibernatePropertyIntrospector method createProperty.

private Property createProperty(Class<?> klass, org.hibernate.mapping.Property hibernateProperty, MetamodelImplementor metamodelImplementor) {
    Property property = new Property(klass);
    property.setRequired(false);
    property.setPersisted(true);
    property.setWritable(true);
    property.setOwner(true);
    Type type = hibernateProperty.getType();
    property.setName(hibernateProperty.getName());
    property.setFieldName(hibernateProperty.getName());
    property.setCascade(hibernateProperty.getCascade());
    property.setCollection(type.isCollectionType());
    property.setSetterMethod(hibernateProperty.getSetter(klass).getMethod());
    property.setGetterMethod(hibernateProperty.getGetter(klass).getMethod());
    if (property.isCollection()) {
        initCollectionProperty(metamodelImplementor, property, (CollectionType) type);
    }
    if (type instanceof SingleColumnType || type instanceof CustomType || type instanceof ManyToOneType) {
        Column column = (Column) hibernateProperty.getColumnIterator().next();
        property.setUnique(column.isUnique());
        property.setRequired(!column.isNullable());
        property.setMin(0d);
        property.setMax((double) column.getLength());
        property.setLength(column.getLength());
        if (type instanceof TextType) {
            property.setMin(0d);
            property.setMax((double) Integer.MAX_VALUE);
            property.setLength(Integer.MAX_VALUE);
        } else if (type instanceof IntegerType) {
            property.setMin((double) Integer.MIN_VALUE);
            property.setMax((double) Integer.MAX_VALUE);
            property.setLength(Integer.MAX_VALUE);
        } else if (type instanceof LongType) {
            property.setMin((double) Long.MIN_VALUE);
            property.setMax((double) Long.MAX_VALUE);
            property.setLength(Integer.MAX_VALUE);
        } else if (type instanceof DoubleType) {
            property.setMin(-Double.MAX_VALUE);
            property.setMax(Double.MAX_VALUE);
            property.setLength(Integer.MAX_VALUE);
        }
    }
    if (type instanceof ManyToOneType) {
        property.setManyToOne(true);
        property.setRequired(property.isRequired() && !property.isCollection());
        if (property.isOwner()) {
            property.setOwningRole(klass.getName() + "." + property.getName());
        } else {
            property.setInverseRole(klass.getName() + "." + property.getName());
        }
    } else if (type instanceof OneToOneType) {
        property.setOneToOne(true);
    } else if (type instanceof OneToMany) {
        property.setOneToMany(true);
    }
    return property;
}
Also used : CustomType(org.hibernate.type.CustomType) IntegerType(org.hibernate.type.IntegerType) CustomType(org.hibernate.type.CustomType) CollectionType(org.hibernate.type.CollectionType) ManyToOneType(org.hibernate.type.ManyToOneType) IntegerType(org.hibernate.type.IntegerType) TextType(org.hibernate.type.TextType) LongType(org.hibernate.type.LongType) SingleColumnType(org.hibernate.type.SingleColumnType) OneToOneType(org.hibernate.type.OneToOneType) SetType(org.hibernate.type.SetType) DoubleType(org.hibernate.type.DoubleType) Type(org.hibernate.type.Type) LongType(org.hibernate.type.LongType) SingleColumnType(org.hibernate.type.SingleColumnType) ManyToOneType(org.hibernate.type.ManyToOneType) Column(org.hibernate.mapping.Column) DoubleType(org.hibernate.type.DoubleType) OneToMany(org.hibernate.mapping.OneToMany) Property(org.hisp.dhis.schema.Property) TextType(org.hibernate.type.TextType) OneToOneType(org.hibernate.type.OneToOneType)

Aggregations

CustomType (org.hibernate.type.CustomType)10 Test (org.junit.Test)5 Metadata (org.hibernate.boot.Metadata)4 MetadataSources (org.hibernate.boot.MetadataSources)4 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)3 Column (org.hibernate.mapping.Column)3 PersistentClass (org.hibernate.mapping.PersistentClass)3 Property (org.hibernate.mapping.Property)3 EnumType (javax.persistence.EnumType)2 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 Collection (org.hibernate.mapping.Collection)2 Component (org.hibernate.mapping.Component)2 SimpleValue (org.hibernate.mapping.SimpleValue)2 Value (org.hibernate.mapping.Value)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 EnumType (org.hibernate.type.EnumType)2 Type (org.hibernate.type.Type)2 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1