下面列出了怎么用org.hibernate.mapping.Value的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 将数据字典解析成Hibernate的字段,不支持级联,不支持一对多
*/
public void parse(MetaProperty property, PersistentClass pclazz) {
// feature
HibernatePropertyFeature feature = property
.getFeature(HibernatePropertyFeature.class);
if (feature == null) {
feature = new HibernatePropertyFeature();
}
// value
Value value;
if (property.isCollection()) {
value = buildCollectionValue(property, feature, pclazz);
} else {
value = buildElement(pclazz, property, feature, pclazz.getTable());
}
// property
Property prop = buildProperty(property, feature, pclazz);
prop.setValue(value);
pclazz.addProperty(prop);
// version
if (feature.isVersion()) {
handleVersion(prop, pclazz);
}
}
public void bindDiscriminatorValue() {
if ( StringHelper.isEmpty( discriminatorValue ) ) {
Value discriminator = persistentClass.getDiscriminator();
if ( discriminator == null ) {
persistentClass.setDiscriminatorValue( name );
}
else if ( "character".equals( discriminator.getType().getName() ) ) {
throw new AnnotationException(
"Using default @DiscriminatorValue for a discriminator of type CHAR is not safe"
);
}
else if ( "integer".equals( discriminator.getType().getName() ) ) {
persistentClass.setDiscriminatorValue( String.valueOf( name.hashCode() ) );
}
else {
persistentClass.setDiscriminatorValue( name ); //Spec compliant
}
}
else {
//persistentClass.getDiscriminator()
persistentClass.setDiscriminatorValue( discriminatorValue );
}
}
public static void createUniqueConstraint(Value value) {
Iterator iter = value.getColumnIterator();
ArrayList cols = new ArrayList();
while ( iter.hasNext() ) {
cols.add( iter.next() );
}
value.getTable().createUniqueKey( cols );
}
private void makeOneToManyMapKeyColumnNullableIfNotInProperty(
final XProperty property) {
final org.hibernate.mapping.Map map = (org.hibernate.mapping.Map) this.collection;
if ( map.isOneToMany() &&
property.isAnnotationPresent( MapKeyColumn.class ) ) {
final Value indexValue = map.getIndex();
if ( indexValue.getColumnSpan() != 1 ) {
throw new AssertionFailure( "Map key mapped by @MapKeyColumn does not have 1 column" );
}
final Selectable selectable = indexValue.getColumnIterator().next();
if ( selectable.isFormula() ) {
throw new AssertionFailure( "Map key mapped by @MapKeyColumn is a Formula" );
}
Column column = (Column) map.getIndex().getColumnIterator().next();
if ( !column.isNullable() ) {
final PersistentClass persistentClass = ( ( OneToMany ) map.getElement() ).getAssociatedClass();
// check if the index column has been mapped by the associated entity to a property;
// @MapKeyColumn only maps a column to the primary table for the one-to-many, so we only
// need to check "un-joined" properties.
if ( !propertyIteratorContainsColumn( persistentClass.getUnjoinedPropertyIterator(), column ) ) {
// The index column is not mapped to an associated entity property so we can
// safely make the index column nullable.
column.setNullable( true );
}
}
}
}
private SingularAttributeMetadataImpl(
Property propertyMapping,
AbstractManagedType<X> ownerType,
Member member,
Attribute.PersistentAttributeType persistentAttributeType) {
super( propertyMapping, ownerType, member, persistentAttributeType );
valueContext = new ValueContext() {
public Value getValue() {
return getPropertyMapping().getValue();
}
public Class getBindableType() {
return getAttributeMetadata().getJavaType();
}
public ValueClassification getValueClassification() {
switch ( getPersistentAttributeType() ) {
case EMBEDDED: {
return ValueClassification.EMBEDDABLE;
}
case BASIC: {
return ValueClassification.BASIC;
}
default: {
return ValueClassification.ENTITY;
}
}
}
public AttributeMetadata getAttributeMetadata() {
return SingularAttributeMetadataImpl.this;
}
};
}
private void prepareValueTypeViaReflection(
MappingDocument sourceDocument,
Value value,
String containingClassName,
String propertyName,
AttributeRole attributeRole) {
if ( StringHelper.isEmpty( propertyName ) ) {
throw new MappingException(
String.format(
Locale.ENGLISH,
"Attribute mapping must define a name attribute: containingClassName=[%s], propertyName=[%s], role=[%s]",
containingClassName,
propertyName,
attributeRole.getFullPath()
),
sourceDocument.getOrigin()
);
}
try {
value.setTypeUsingReflection( containingClassName, propertyName );
}
catch (org.hibernate.MappingException ome) {
throw new MappingException(
String.format(
Locale.ENGLISH,
"Error calling Value#setTypeUsingReflection: containingClassName=[%s], propertyName=[%s], role=[%s]",
containingClassName,
propertyName,
attributeRole.getFullPath()
),
ome,
sourceDocument.getOrigin()
);
}
}
private String columns(Value value) {
final StringBuilder builder = new StringBuilder();
final Iterator<Selectable> selectableItr = value.getColumnIterator();
while ( selectableItr.hasNext() ) {
builder.append( selectableItr.next().getText() );
if ( selectableItr.hasNext() ) {
builder.append( ", " );
}
}
return builder.toString();
}
private String fieldType(Column currentColumn) {
Value value = currentColumn.getValue();
Type type = value.getType();
while ( type.isEntityType() || type.isComponentType() ) {
if ( type.isEntityType() ) {
type = ( (SimpleValue) value ).getMetadata().getIdentifierType( type.getName() );
}
if ( type.isComponentType() ) {
int i = 0;
boolean columnFound = false;
// search which nested property is mapped to the given column
for ( Iterator<Selectable> ci = value.getColumnIterator(); ci.hasNext(); ++i ) {
if ( currentColumn.getName().equals( ci.next().getText() ) ) {
type = ( (ComponentType) type ).getSubtypes()[i];
columnFound = true;
break;
}
}
if ( !columnFound ) {
throw new IllegalArgumentException( "Cannot determine type for column " + currentColumn );
}
}
}
GridType gridType = serviceRegistry.getService( TypeTranslator.class ).getType( type );
if ( gridType instanceof EnumType ) {
return enumFieldType( (EnumType) gridType );
}
if ( gridType instanceof YesNoType ) {
return STRING_CLASS_NAME;
}
if ( gridType instanceof NumericBooleanType ) {
return INTEGER_CLASS_NAME;
}
Class<?> returnedClass = type.getReturnedClass();
if ( Character.class.equals( returnedClass ) ) {
return STRING_CLASS_NAME;
}
return returnedClass.getName();
}
public Value getValue() {
return value;
}
public void setValue(Value value) {
this.value = value;
}
private boolean isToOneValue(Value value) {
return ToOne.class.isInstance( value );
}
public Value getValue() {
return value;
}
private PluralAttributeMetadataImpl(
Property propertyMapping,
AbstractManagedType<X> ownerType,
Member member,
Attribute.PersistentAttributeType persistentAttributeType,
Attribute.PersistentAttributeType elementPersistentAttributeType,
Attribute.PersistentAttributeType keyPersistentAttributeType) {
super( propertyMapping, ownerType, member, persistentAttributeType );
this.attributeCollectionType = determineCollectionType( getJavaType() );
this.elementPersistentAttributeType = elementPersistentAttributeType;
this.keyPersistentAttributeType = keyPersistentAttributeType;
ParameterizedType signatureType = getSignatureType( member );
if ( keyPersistentAttributeType == null ) {
elementJavaType = signatureType != null ?
getClassFromGenericArgument( signatureType.getActualTypeArguments()[0] ) :
Object.class; //FIXME and honor targetEntity?
keyJavaType = null;
}
else {
keyJavaType = signatureType != null ?
getClassFromGenericArgument( signatureType.getActualTypeArguments()[0] ) :
Object.class; //FIXME and honor targetEntity?
elementJavaType = signatureType != null ?
getClassFromGenericArgument( signatureType.getActualTypeArguments()[1] ) :
Object.class; //FIXME and honor targetEntity?
}
this.elementValueContext = new ValueContext() {
public Value getValue() {
return ( (Collection) getPropertyMapping().getValue() ).getElement();
}
public Class getBindableType() {
return elementJavaType;
}
public ValueClassification getValueClassification() {
switch ( PluralAttributeMetadataImpl.this.elementPersistentAttributeType ) {
case EMBEDDED: {
return ValueClassification.EMBEDDABLE;
}
case BASIC: {
return ValueClassification.BASIC;
}
default: {
return ValueClassification.ENTITY;
}
}
}
public AttributeMetadata getAttributeMetadata() {
return PluralAttributeMetadataImpl.this;
}
};
// interpret the key, if one
if ( keyPersistentAttributeType != null ) {
this.keyValueContext = new ValueContext() {
public Value getValue() {
return ( (Map) getPropertyMapping().getValue() ).getIndex();
}
public Class getBindableType() {
return keyJavaType;
}
public ValueClassification getValueClassification() {
switch ( PluralAttributeMetadataImpl.this.keyPersistentAttributeType ) {
case EMBEDDED: {
return ValueClassification.EMBEDDABLE;
}
case BASIC: {
return ValueClassification.BASIC;
}
default: {
return ValueClassification.ENTITY;
}
}
}
public AttributeMetadata getAttributeMetadata() {
return PluralAttributeMetadataImpl.this;
}
};
}
else {
keyValueContext = null;
}
}
@Override
public boolean isSame(Value value) {
return false;
}
/**
* Retrieve the value itself
*
* @return The value
*/
public Value getValue();