本文整理匯總了Java中javax.persistence.DiscriminatorValue類的典型用法代碼示例。如果您正苦於以下問題:Java DiscriminatorValue類的具體用法?Java DiscriminatorValue怎麽用?Java DiscriminatorValue使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
DiscriminatorValue類屬於javax.persistence包,在下文中一共展示了DiscriminatorValue類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: buildInheritance
import javax.persistence.DiscriminatorValue; //導入依賴的package包/類
/**
* Determine the inheritance type and discriminator properties.
*/
private void buildInheritance() {
// Check, if we've got an explicit inheritance type
final Inheritance inheritance = this.entityClass.getAnnotation(Inheritance.class);
if (inheritance != null) {
this.inheritanceType = inheritance.strategy();
}
// Find the root of our hierarchy
this.hierarchyRoot = this;
findHierarchyRoot(this.entityClass.getSuperclass());
// We scan only classes that we are about to write
// So we don't know, that there is a subclass entity - until we find one
// This could be to late for InheritanceType.SINGLE_TABLE - the defaault type
// That's why we build a discriminator, if one of the inheritance annotations exists
if (this.inheritanceType == null && this.entityClass.isAnnotationPresent(DiscriminatorColumn.class)
|| this.entityClass.isAnnotationPresent(DiscriminatorValue.class)) {
this.inheritanceType = InheritanceType.SINGLE_TABLE;
}
buildDiscriminator();
}
示例2: getDiscriminatorValue
import javax.persistence.DiscriminatorValue; //導入依賴的package包/類
private DiscriminatorValue getDiscriminatorValue(Element tree, XMLContext.Default defaults) {
Element element = tree != null ? tree.element( "discriminator-value" ) : null;
if ( element != null ) {
AnnotationDescriptor ad = new AnnotationDescriptor( DiscriminatorValue.class );
copyStringElement( element, ad, "value" );
return AnnotationFactory.create( ad );
}
else if ( defaults.canUseJavaAnnotations() ) {
return getPhysicalAnnotation( DiscriminatorValue.class );
}
else {
return null;
}
}
示例3: testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheClassNameWithSingleTableInheritance
import javax.persistence.DiscriminatorValue; //導入依賴的package包/類
@Test
public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheClassNameWithSingleTableInheritance() throws Exception {
final String simpleClassNameBase = "EntityClass";
final String simpleClassNameA = "SubEntityClassA";
final String simpleClassNameB = "SubEntityClassB";
final JPackage jp = jCodeModel.rootPackage();
final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase);
jBaseClass.annotate(Entity.class);
jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.SINGLE_TABLE);
jBaseClass.annotate(DiscriminatorColumn.class).param("name", "TYPE");
final JDefinedClass jSubclassA = jp._class(JMod.PUBLIC, simpleClassNameA)._extends(jBaseClass);
jSubclassA.annotate(Entity.class);
jSubclassA.annotate(DiscriminatorValue.class).param("value", "A");
final JDefinedClass jSubclassB = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass);
jSubclassB.annotate(Entity.class);
jSubclassB.annotate(DiscriminatorValue.class).param("value", "B");
buildModel(testFolder.getRoot(), jCodeModel);
compileModel(testFolder.getRoot());
final Class<?> baseClass = loadClass(testFolder.getRoot(), jBaseClass.name());
final Class<?> subClassA = loadClass(testFolder.getRoot(), jSubclassA.name());
final Class<?> subClassB = loadClass(testFolder.getRoot(), jSubclassB.name());
final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(simpleClassNameBase),
Arrays.asList(baseClass, subClassA, subClassB));
assertThat(clazz, equalTo(baseClass));
}
示例4: testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheTableAnnotationWithSingleTableInheritance
import javax.persistence.DiscriminatorValue; //導入依賴的package包/類
@Test
public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheTableAnnotationWithSingleTableInheritance() throws Exception {
final String simpleClassNameBase = "EntityClass";
final String simpleClassNameA = "SubEntityClassA";
final String simpleClassNameB = "SubEntityClassB";
final String nodeLabel = "ENTITY_CLASS";
final JPackage jp = jCodeModel.rootPackage();
final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase);
jBaseClass.annotate(Entity.class);
jBaseClass.annotate(Table.class).param("name", nodeLabel);
jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.SINGLE_TABLE);
jBaseClass.annotate(DiscriminatorColumn.class).param("name", "TYPE");
final JDefinedClass jSubclassA = jp._class(JMod.PUBLIC, simpleClassNameA)._extends(jBaseClass);
jSubclassA.annotate(Entity.class);
jSubclassA.annotate(DiscriminatorValue.class).param("value", "A");
final JDefinedClass jSubclassB = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass);
jSubclassB.annotate(Entity.class);
jSubclassB.annotate(DiscriminatorValue.class).param("value", "B");
buildModel(testFolder.getRoot(), jCodeModel);
compileModel(testFolder.getRoot());
final Class<?> baseClass = loadClass(testFolder.getRoot(), jBaseClass.name());
final Class<?> subClassA = loadClass(testFolder.getRoot(), jSubclassA.name());
final Class<?> subClassB = loadClass(testFolder.getRoot(), jSubclassB.name());
final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(nodeLabel),
Arrays.asList(baseClass, subClassA, subClassB));
assertThat(clazz, equalTo(baseClass));
}
示例5: buildDiscriminator
import javax.persistence.DiscriminatorValue; //導入依賴的package包/類
private ColumnExpression buildDiscriminator(final EntityClass<?> c, final DiscriminatorColumn column) {
final DiscriminatorType type;
final int maxLength;
if (column == null) {
type = DiscriminatorType.STRING;
final int defaultMaxLength = 31;
maxLength = defaultMaxLength;
} else {
type = column.discriminatorType();
maxLength = column.length();
}
final DiscriminatorValue value = this.entityClass.getAnnotation(DiscriminatorValue.class);
if (type == DiscriminatorType.INTEGER) {
return PrimitiveColumnExpression.create(
value == null ? c.getEntityName().hashCode() : Integer.parseInt(value.value()),
getContext().getDialect());
}
final String v = value == null ? c.getEntityName() : value.value();
if (StringUtils.isEmpty(v)) {
throw new IllegalArgumentException("Missing discriminator value for: " + c.getEntityClass());
}
if (type == DiscriminatorType.STRING) {
return PrimitiveColumnExpression.create(v.length() <= maxLength ? v : v.substring(0, maxLength),
getContext().getDialect());
} else if (type == DiscriminatorType.CHAR) {
return PrimitiveColumnExpression.create(v.substring(0, 1), getContext().getDialect());
}
throw new IllegalArgumentException("Unknown discriminator type: " + type);
}
示例6: getDiscriminatorValue
import javax.persistence.DiscriminatorValue; //導入依賴的package包/類
public String getDiscriminatorValue()
{
final DiscriminatorValue annotation = clazz.getAnnotation(DiscriminatorValue.class);
if (annotation != null)
return annotation.value();
else
return clazz.getName();
}
示例7: processSingleTableDiscriminatorProperties
import javax.persistence.DiscriminatorValue; //導入依賴的package包/類
/**
* Process all discriminator-related metadata per rules for "single table" inheritance
*/
private static Ejb3DiscriminatorColumn processSingleTableDiscriminatorProperties(
XClass clazzToProcess,
Mappings mappings,
InheritanceState inheritanceState,
EntityBinder entityBinder) {
final boolean isRoot = !inheritanceState.hasParents();
Ejb3DiscriminatorColumn discriminatorColumn = null;
javax.persistence.DiscriminatorColumn discAnn = clazzToProcess.getAnnotation(
javax.persistence.DiscriminatorColumn.class
);
DiscriminatorType discriminatorType = discAnn != null
? discAnn.discriminatorType()
: DiscriminatorType.STRING;
org.hibernate.annotations.DiscriminatorFormula discFormulaAnn = clazzToProcess.getAnnotation(
org.hibernate.annotations.DiscriminatorFormula.class
);
if ( isRoot ) {
discriminatorColumn = Ejb3DiscriminatorColumn.buildDiscriminatorColumn(
discriminatorType,
discAnn,
discFormulaAnn,
mappings
);
}
if ( discAnn != null && !isRoot ) {
LOG.invalidDiscriminatorAnnotation( clazzToProcess.getName() );
}
final String discriminatorValue = clazzToProcess.isAnnotationPresent( DiscriminatorValue.class )
? clazzToProcess.getAnnotation( DiscriminatorValue.class ).value()
: null;
entityBinder.setDiscriminatorValue( discriminatorValue );
DiscriminatorOptions discriminatorOptions = clazzToProcess.getAnnotation( DiscriminatorOptions.class );
if ( discriminatorOptions != null) {
entityBinder.setForceDiscriminator( discriminatorOptions.force() );
entityBinder.setInsertableDiscriminator( discriminatorOptions.insert() );
}
return discriminatorColumn;
}
示例8: processJoinedDiscriminatorProperties
import javax.persistence.DiscriminatorValue; //導入依賴的package包/類
/**
* Process all discriminator-related metadata per rules for "joined" inheritance
*/
private static Ejb3DiscriminatorColumn processJoinedDiscriminatorProperties(
XClass clazzToProcess,
Mappings mappings,
InheritanceState inheritanceState,
EntityBinder entityBinder) {
if ( clazzToProcess.isAnnotationPresent( DiscriminatorFormula.class ) ) {
throw new MappingException( "@DiscriminatorFormula on joined inheritance not supported at this time" );
}
// DiscriminatorValue handling ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
final DiscriminatorValue discriminatorValueAnnotation = clazzToProcess.getAnnotation( DiscriminatorValue.class );
final String discriminatorValue = discriminatorValueAnnotation != null
? clazzToProcess.getAnnotation( DiscriminatorValue.class ).value()
: null;
entityBinder.setDiscriminatorValue( discriminatorValue );
// DiscriminatorColumn handling ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
final DiscriminatorColumn discriminatorColumnAnnotation = clazzToProcess.getAnnotation( DiscriminatorColumn.class );
if ( !inheritanceState.hasParents() ) {
// we want to process the discriminator column if either:
// 1) There is an explicit DiscriminatorColumn annotation && we are not told to ignore them
// 2) There is not an explicit DiscriminatorColumn annotation && we are told to create them implicitly
final boolean generateDiscriminatorColumn;
if ( discriminatorColumnAnnotation != null ) {
if ( mappings.ignoreExplicitDiscriminatorColumnForJoinedInheritance() ) {
LOG.debugf( "Ignoring explicit DiscriminatorColumn annotation on ", clazzToProcess.getName() );
generateDiscriminatorColumn = false;
}
else {
LOG.applyingExplicitDiscriminatorColumnForJoined(
clazzToProcess.getName(),
AvailableSettings.IGNORE_EXPLICIT_DISCRIMINATOR_COLUMNS_FOR_JOINED_SUBCLASS
);
generateDiscriminatorColumn = true;
}
}
else {
if ( mappings.useImplicitDiscriminatorColumnForJoinedInheritance() ) {
LOG.debug( "Applying implicit DiscriminatorColumn using DiscriminatorColumn defaults" );
generateDiscriminatorColumn = true;
}
else {
LOG.debug( "Ignoring implicit (absent) DiscriminatorColumn" );
generateDiscriminatorColumn = false;
}
}
if ( generateDiscriminatorColumn ) {
final DiscriminatorType discriminatorType = discriminatorColumnAnnotation != null
? discriminatorColumnAnnotation.discriminatorType()
: DiscriminatorType.STRING;
return Ejb3DiscriminatorColumn.buildDiscriminatorColumn(
discriminatorType,
discriminatorColumnAnnotation,
null,
mappings
);
}
}
else {
if ( discriminatorColumnAnnotation != null ) {
LOG.invalidDiscriminatorAnnotation( clazzToProcess.getName() );
}
}
return null;
}