Enumeration and custom data type

In this section, we see how to map "constants" to enumerated types. Constants are considered a small finite set of data elements, such as gender (Male, Female) or days of the week, such as Monday, Tuesday, and so on. Hibernate supports using enumerated types for mapping database columns containing data in a finite closed set. (The definition of a closed set comes straight from set theory.)

If you don't wish to use enumerated types, you can create a custom data type to map your data to a primitive type in Java, and you'll see that after we discuss the enumerated type.

Enumerated type

You can define an enumeration in Java and let Hibernate handle the mapping of "constants". JPA supports two kinds of enum: ORDINAL and STRING. If you use the ORDINAL enumeration type, the data in the mapped column is assumed to be an integer. You can guess how the STRING enumeration type behaves:

public enum WeekDay {
  Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}


@Entity
public class WorkSchedule {
  @Id
  @GeneratedValue
  private long id;
  
  private Timestamp startTime;
  private Timestamp endTime;

  @Enumerated(EnumType.STRING)
  private WeekDay workDay;

  // getters and setters
}

The ORDINAL enumeration type starts from zero; Monday is 0 and Sunday is 6.

Custom data type mapping

As we discussed in the previous chapter, Hibernate loads type handlers at startup for a specific dialect. You can further create custom maps to map data in the database to primitive types in Java.

Hibernate has built-in types that map the true_false or yes_no values to Java types, such as Boolean. You can create your own user-defined types and use them in your application. Let's assume that the data in the database is for Spanish-speaking users, "Y" and "N" (for Yes and No) are stored as "S" and "N" (for Sí and No), and you wish to map this to a Boolean.

To define the type, implement a new class, as follows:

public class SpanishBoolean  extends AbstractSingleColumnStandardBasicType<Boolean> 
              implements PrimitiveType<Boolean>, DiscriminatorType<Boolean> 
{
    private static final long serialVersionUID = 1L;
    public static final SpanishBoolean INSTANCE = new SpanishBoolean(); 


    public SpanishBoolean() { 
            super( CharTypeDescriptor.INSTANCE, new BooleanTypeDescriptor('S', 'N') ); 
    } 
    @Override 
    public String getName() { 
            return "si_no"; 
    } 
    @Override 
    public Class getPrimitiveClass() { 
            return boolean.class; 
    } 
    @Override 
    public Boolean stringToObject(String xml) throws Exception { 
            return fromString( xml ); 
    } 
    @Override 
    public Serializable getDefaultValue() { 
            return Boolean.FALSE; 
    } 
    @Override 
    public String objectToSQLString(Boolean value, Dialect dialect) throws Exception { 
            return StringType.INSTANCE.objectToSQLString( value ? "S" : "N", dialect ); 
    } 
}

Then, at startup, register the new type with the configuration:

Configuration configuration = new Configuration().configure();
configuration.registerTypeOverride(new SpanishBoolean());

Now, you are ready to use this in your entity:

@Entity
public class Persona {
  …
  @Type(type="com.package.type.SpanishBoolean")
  private Boolean maestro;
}

When you save this entity, the maestro column will have a value of S, if true, or N, if false.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.206.254