Skip to content

Instantly share code, notes, and snippets.

@CarlosPanarello
Created June 8, 2021 20:33
Show Gist options
  • Save CarlosPanarello/408c8147ddbb8a7d48ba13dd999d310b to your computer and use it in GitHub Desktop.
Save CarlosPanarello/408c8147ddbb8a7d48ba13dd999d310b to your computer and use it in GitHub Desktop.
SimNaoType para hibernate
import java.io.Serializable;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
import org.hibernate.dialect.Dialect;
import org.hibernate.type.AbstractSingleColumnStandardBasicType;
import org.hibernate.type.DiscriminatorType;
import org.hibernate.type.PrimitiveType;
import org.hibernate.type.StringType;
import org.hibernate.type.descriptor.java.BooleanTypeDescriptor;
import org.hibernate.type.descriptor.sql.CharTypeDescriptor;
// Para usar colocar essa anotacao na entity
//@TypeDefs({@TypeDef(name = "sim_nao", typeClass = SimNaoType.class) })
public class SimNaoType extends AbstractSingleColumnStandardBasicType<Boolean> implements
PrimitiveType<Boolean>, DiscriminatorType<Boolean> {
public static final SimNaoType INSTANCE = new SimNaoType();
public SimNaoType() {
super(CharTypeDescriptor.INSTANCE, new BooleanTypeDescriptor('S', 'N'));
}
public String getName() {
return "sim_nao";
}
public Class getPrimitiveClass() {
return Boolean.TYPE;
}
public Boolean stringToObject(String xml) throws Exception {
return (Boolean)this.fromString(xml);
}
public Serializable getDefaultValue() {
return Boolean.FALSE;
}
public String objectToSQLString(Boolean value, Dialect dialect) throws Exception {
return StringType.INSTANCE.objectToSQLString(value ? "S" : "N", dialect);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment