All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vladmihalcea.hibernate.type.array.EnumArrayType Maven / Gradle / Ivy

There is a newer version: 2.21.1
Show newest version
package com.vladmihalcea.hibernate.type.array;

import com.vladmihalcea.hibernate.type.array.internal.AbstractArrayType;
import com.vladmihalcea.hibernate.type.array.internal.AbstractArrayTypeDescriptor;
import com.vladmihalcea.hibernate.type.array.internal.EnumArrayTypeDescriptor;
import com.vladmihalcea.hibernate.type.util.Configuration;
import com.vladmihalcea.hibernate.type.util.ParameterizedParameterType;
import org.hibernate.annotations.Type;
import org.hibernate.usertype.DynamicParameterizedType;

import java.lang.annotation.Annotation;
import java.util.Properties;

/**
 * Maps an {@code Enum[]} array on a database ARRAY type. Multidimensional arrays are supported as well, as explained in this article.
 * 

* The {@code SQL_ARRAY_TYPE} parameter is used to define the enum type name in the database. *

* For more details about how to use it, check out this article on vladmihalcea.com. * * @author Nazir El-Kayssi * @author Vlad Mihalcea */ public class EnumArrayType extends AbstractArrayType { public static final EnumArrayType INSTANCE = new EnumArrayType(); private static final String DEFAULT_TYPE_NAME = "%s_enum_array_type"; private String name; public EnumArrayType() { super(new EnumArrayTypeDescriptor()); } public EnumArrayType(Configuration configuration) { super( new EnumArrayTypeDescriptor(), configuration ); } public EnumArrayType(Class arrayClass, String sqlArrayType) { super(new EnumArrayTypeDescriptor(arrayClass)); Properties parameters = new Properties(); parameters.setProperty(SQL_ARRAY_TYPE, sqlArrayType); parameters.put(DynamicParameterizedType.PARAMETER_TYPE, new ParameterizedParameterType(arrayClass)); setParameterValues(parameters); } public EnumArrayType(org.hibernate.type.spi.TypeBootstrapContext typeBootstrapContext) { this(new Configuration(typeBootstrapContext.getConfigurationSettings())); } public String getName() { return name; } @Override public void setParameterValues(Properties parameters) { DynamicParameterizedType.ParameterType parameterType = (ParameterType) parameters.get(DynamicParameterizedType.PARAMETER_TYPE); Annotation[] annotations = parameterType.getAnnotationsMethod(); if (name == null) { name = String.format(DEFAULT_TYPE_NAME, parameters.getProperty(SQL_ARRAY_TYPE)); } super.setParameterValues(parameters); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy