1 package org.hibernate.cfg; 3 4 import java.util.Map ; 5 import javax.persistence.DiscriminatorColumn; 6 import javax.persistence.DiscriminatorType; 7 8 import org.hibernate.AssertionFailure; 9 import org.hibernate.annotations.DiscriminatorFormula; 10 import org.hibernate.mapping.Join; 11 12 17 public class Ejb3DiscriminatorColumn extends Ejb3Column { 18 19 20 private static final String DEFAULT_DISCRIMINATOR_COLUMN_NAME = "TYPE"; 21 private static final String DEFAULT_DISCRIMINATOR_TYPE = "string"; 22 23 private String discriminatorTypeName; 24 25 public Ejb3DiscriminatorColumn() { 26 super(); 28 setColumnName(DEFAULT_DISCRIMINATOR_COLUMN_NAME); 29 setNullable(false); 30 setDiscriminatorTypeName(DEFAULT_DISCRIMINATOR_TYPE); 31 } 32 33 public Ejb3DiscriminatorColumn(boolean isImplicit, String sqlType, int length, String name, boolean nullable, boolean unique, boolean insertable, 34 boolean updatable, String secondaryTableName, Map <String , Join> joins, PropertyHolder propertyHolder, 35 String discriminatorTypeName, Mappings mappings) { 36 super(); 37 setImplicit(isImplicit); 38 setSqlType(sqlType); 39 setLength(length); 40 setColumnName(name); 41 setNullable(nullable); 42 setUnique(unique); 43 setInsertable(insertable); 44 setUpdatable(updatable); 45 setSecondaryTableName(secondaryTableName); 46 setJoins(joins); 47 setPropertyHolder(propertyHolder); 48 setMappings(mappings); 49 this.discriminatorTypeName = discriminatorTypeName; 50 bind(); 51 } 52 53 public String getDiscriminatorTypeName() { 54 return discriminatorTypeName; 55 } 56 57 public void setDiscriminatorTypeName(String discriminatorTypeName) { 58 this.discriminatorTypeName = discriminatorTypeName; 59 } 60 61 public static Ejb3DiscriminatorColumn buildDiscriminatorColumn( 62 DiscriminatorType type, DiscriminatorColumn discAnn, DiscriminatorFormula discFormulaAnn, ExtendedMappings mappings 63 ) { 64 Ejb3DiscriminatorColumn discriminatorColumn = new Ejb3DiscriminatorColumn(); 65 discriminatorColumn.setMappings(mappings); 66 discriminatorColumn.setImplicit(true); 67 if (discFormulaAnn != null) { 74 discriminatorColumn.setImplicit(false); 75 discriminatorColumn.setFormula( discFormulaAnn.value() ); 76 } 77 else if (discAnn != null) { 78 discriminatorColumn.setImplicit(false); 79 if (! AnnotationBinder.isDefault( discAnn.columnDefinition() ) ) discriminatorColumn.setSqlType( discAnn.columnDefinition() ); 80 if (! AnnotationBinder.isDefault( discAnn.name() ) ) discriminatorColumn.setColumnName( discAnn.name() ); 81 discriminatorColumn.setNullable( discAnn.nullable() ); 82 } 83 if (DiscriminatorType.CHAR.equals(type) ) { 92 discriminatorColumn.setDiscriminatorTypeName("character"); 93 discriminatorColumn.setImplicit(false); 94 } 95 else if (DiscriminatorType.INTEGER.equals(type) ) { 96 discriminatorColumn.setDiscriminatorTypeName("integer"); 97 discriminatorColumn.setImplicit(false); 98 } 99 else if (DiscriminatorType.STRING.equals(type) || type == null ) { 100 if (discAnn != null) discriminatorColumn.setLength( discAnn.length() ); 101 discriminatorColumn.setDiscriminatorTypeName("string"); 102 } else { 105 throw new AssertionFailure("Unknown discriminator type: " + type); 106 } 107 discriminatorColumn.bind(); 110 return discriminatorColumn; 111 } 112 } 113 | Popular Tags |