下面列出了怎么用org.hibernate.engine.spi.Mapping的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public boolean[] toColumnNullness(Object value, Mapping mapping) {
boolean[] result = new boolean[getColumnSpan( mapping )];
if ( value == null ) {
return result;
}
Object[] values = getPropertyValues( value, EntityMode.POJO ); //TODO!!!!!!!
int loc = 0;
Type[] propertyTypes = getSubtypes();
for ( int i = 0; i < propertyTypes.length; i++ ) {
boolean[] propertyNullness = propertyTypes[i].toColumnNullness( values[i], mapping );
System.arraycopy( propertyNullness, 0, result, loc, propertyNullness.length );
loc += propertyNullness.length;
}
return result;
}
public void validate(Mapping mapping) throws MappingException {
Iterator iter = getPropertyIterator();
while ( iter.hasNext() ) {
Property prop = (Property) iter.next();
if ( !prop.isValid( mapping ) ) {
throw new MappingException(
"property mapping has wrong number of columns: " +
StringHelper.qualify( getEntityName(), prop.getName() ) +
" type: " +
prop.getType().getName()
);
}
}
checkPropertyDuplication();
checkColumnDuplication();
}
public int getSqlTypeCode(Mapping mapping) throws MappingException {
org.hibernate.type.Type type = getValue().getType();
try {
int sqlTypeCode = type.sqlTypes( mapping )[getTypeIndex()];
if ( getSqlTypeCode() != null && getSqlTypeCode() != sqlTypeCode ) {
throw new MappingException( "SQLType code's does not match. mapped as " + sqlTypeCode + " but is " + getSqlTypeCode() );
}
return sqlTypeCode;
}
catch (Exception e) {
throw new MappingException(
"Could not determine type for column " +
name +
" of type " +
type.getClass().getName() +
": " +
e.getClass().getName(),
e
);
}
}
public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema) {
if ( isGenerated( dialect ) ) {
// Certain dialects (ex: HANA) don't support FKs as expected, but other constraints can still be created.
// If that's the case, hasAlterTable() will be true, but getAddForeignKeyConstraintString will return
// empty string. Prevent blank "alter table" statements.
String constraintString = sqlConstraintString( dialect, getName(), defaultCatalog, defaultSchema );
if ( !StringHelper.isEmpty( constraintString ) ) {
final String tableName = getTable().getQualifiedName( dialect, defaultCatalog, defaultSchema );
return dialect.getAlterTableString( tableName ) + " " + constraintString;
}
}
return null;
}
@Override
public int getColumnSpan(Mapping mapping) throws MappingException {
Type[] types = userType.getPropertyTypes();
int n = 0;
for ( Type type : types ) {
n += type.getColumnSpan( mapping );
}
return n;
}
public void validate(Mapping mapping) throws MappingException {
super.validate( mapping );
assert getElement() != null : "IdentifierCollection identifier not bound : " + getRole();
if ( !getIdentifier().isValid(mapping) ) {
throw new MappingException(
"collection id mapping has wrong number of columns: " +
getRole() +
" type: " +
getIdentifier().getType().getName()
);
}
}
public String sqlCreateString(Dialect dialect, Mapping mapping, String defaultCatalog, String defaultSchema)
throws HibernateException {
return buildSqlCreateIndexString(
dialect,
getQuotedName( dialect ),
getTable(),
getColumnIterator(),
columnOrderMap,
false,
defaultCatalog,
defaultSchema
);
}
public void validate(Mapping mapping) throws MappingException {
super.validate(mapping);
if ( key!=null && !key.isValid(mapping) ) {
throw new MappingException(
"subclass key mapping has wrong number of columns: " +
getEntityName() +
" type: " +
key.getType().getName()
);
}
}
public void validateColumns(Dialect dialect, Mapping mapping, TableMetadata tableInfo) {
Iterator iter = getColumnIterator();
while ( iter.hasNext() ) {
Column col = (Column) iter.next();
ColumnMetadata columnInfo = tableInfo.getColumnMetadata( col.getName() );
if ( columnInfo == null ) {
throw new HibernateException( "Missing column: " + col.getName() + " in " + Table.qualify( tableInfo.getCatalog(), tableInfo.getSchema(), tableInfo.getName()));
}
else {
final boolean typesMatch = col.getSqlType( dialect, mapping ).toLowerCase(Locale.ROOT)
.startsWith( columnInfo.getTypeName().toLowerCase(Locale.ROOT) )
|| columnInfo.getTypeCode() == col.getSqlTypeCode( mapping );
if ( !typesMatch ) {
throw new HibernateException(
"Wrong column type in " +
Table.qualify( tableInfo.getCatalog(), tableInfo.getSchema(), tableInfo.getName()) +
" for column " + col.getName() +
". Found: " + columnInfo.getTypeName().toLowerCase(Locale.ROOT) +
", expected: " + col.getSqlType( dialect, mapping )
);
}
}
}
}
@Override
public String sqlCreateString(
Dialect dialect,
Mapping p,
String defaultCatalog,
String defaultSchema) {
return null;
// return dialect.getUniqueDelegate().getAlterTableToAddUniqueKeyCommand(
// this, defaultCatalog, defaultSchema
// );
}
private void initOrdinaryPropertyPaths(Mapping mapping) throws MappingException {
for ( int i = 0; i < getSubclassPropertyNameClosure().length; i++ ) {
propertyMapping.initPropertyPaths(
getSubclassPropertyNameClosure()[i],
getSubclassPropertyTypeClosure()[i],
getSubclassPropertyColumnNameClosure()[i],
getSubclassPropertyColumnReaderClosure()[i],
getSubclassPropertyColumnReaderTemplateClosure()[i],
getSubclassPropertyFormulaTemplateClosure()[i],
mapping
);
}
}
protected Type requireIdentifierOrUniqueKeyType(Mapping mapping) {
final Type fkTargetType = getIdentifierOrUniqueKeyType( mapping );
if ( fkTargetType == null ) {
throw new MappingException(
"Unable to determine FK target Type for many-to-one or one-to-one mapping: " +
"referenced-entity-name=[" + getAssociatedEntityName() +
"], referenced-entity-attribute-name=[" + getLHSPropertyName() + "]"
);
}
return fkTargetType;
}
/**
* Get the columns of the owning entity which are to be used in the join
*
* @param type The type representing the join
* @param property The property index for the association type
* @param begin ?
* @param lhsPersister The persister for the left-hand-side of the join
* @param mapping The mapping object (typically the SessionFactory)
*
* @return The columns for the left-hand-side of the join
*/
public static String[] getLHSColumnNames(
AssociationType type,
int property,
int begin,
OuterJoinLoadable lhsPersister,
Mapping mapping) {
if ( type.useLHSPrimaryKey() ) {
//return lhsPersister.getSubclassPropertyColumnNames(property);
return lhsPersister.getIdentifierColumnNames();
}
else {
final String propertyName = type.getLHSPropertyName();
if ( propertyName == null ) {
//slice, to get the columns for this component
//property
return ArrayHelper.slice(
property < 0
? lhsPersister.getIdentifierColumnNames()
: lhsPersister.getSubclassPropertyColumnNames( property ),
begin,
type.getColumnSpan( mapping )
);
}
else {
//property-refs for associations defined on a
//component are not supported, so no need to slice
return lhsPersister.getPropertyColumnNames( propertyName );
}
}
}
/**
* Determine the type of either (1) the identifier if we reference the
* associated entity's PK or (2) the unique key to which we refer (i.e.
* the property-ref).
*
* @param factory The mappings...
*
* @return The appropriate type.
*
* @throws MappingException Generally, if unable to resolve the associated entity name
* or unique key property name.
*/
public final Type getIdentifierOrUniqueKeyType(Mapping factory) throws MappingException {
if ( isReferenceToPrimaryKey() || uniqueKeyPropertyName == null ) {
return getIdentifierType( factory );
}
else {
Type type = factory.getReferencedPropertyType( getAssociatedEntityName(), uniqueKeyPropertyName );
if ( type.isEntityType() ) {
type = ( (EntityType) type ).getIdentifierOrUniqueKeyType( factory );
}
return type;
}
}
protected final int determineJdbcTypeCode(Type type, Mapping mapping) throws QueryException {
try {
final int[] jdbcTypeCodes = type.sqlTypes( mapping );
if ( jdbcTypeCodes.length != 1 ) {
throw new QueryException( "multiple-column type in sum()" );
}
return jdbcTypeCodes[0];
}
catch ( MappingException me ) {
throw new QueryException( me );
}
}
@Override
public int getColumnSpan(Mapping mapping) throws MappingException {
int span = 0;
for ( int i = 0; i < propertySpan; i++ ) {
span += propertyTypes[i].getColumnSpan( mapping );
}
return span;
}
@Override
public Size[] defaultSizes(Mapping mapping) throws MappingException {
//Not called at runtime so doesn't matter if its slow :)
final Size[] sizes = new Size[getColumnSpan( mapping )];
int soFar = 0;
for ( Type propertyType : propertyTypes ) {
final Size[] propertySizes = propertyType.defaultSizes( mapping );
System.arraycopy( propertySizes, 0, sizes, soFar, propertySizes.length );
soFar += propertySizes.length;
}
return sizes;
}
@Override
public Size[] defaultSizes(Mapping mapping) throws MappingException {
//Not called at runtime so doesn't matter if its slow :)
final Size[] sizes = new Size[getColumnSpan( mapping )];
int soFar = 0;
for ( Type propertyType : userType.getPropertyTypes() ) {
final Size[] propertySizes = propertyType.defaultSizes( mapping );
System.arraycopy( propertySizes, 0, sizes, soFar, propertySizes.length );
soFar += propertySizes.length;
}
return sizes;
}
@Override
public Type getReturnType(Type columnType, Mapping mapping) throws QueryException {
return new BooleanType();
}
public boolean isValid(Mapping mapping) throws MappingException {
if (referencedEntityName==null) {
throw new MappingException("association must specify the referenced entity");
}
return super.isValid( mapping );
}
public boolean isValid(Mapping mapping) throws MappingException {
return true;
}
@Override
public int getColumnSpan(Mapping session) {
return 2;
}
@Override
public Size[] defaultSizes(Mapping mapping) throws MappingException {
return new Size[] { LEGACY_DEFAULT_SIZE };
}
@Override
public Size[] dictatedSizes(Mapping mapping) throws MappingException {
return columnMapper.getHibernateType().dictatedSizes(mapping);
}
@Override
public Type getReturnType(Type columnType, Mapping mapping)
throws QueryException {
return FloatType.INSTANCE;
}
@Override
public Size[] dictatedSizes(Mapping mapping) throws MappingException {
return new Size[] { LEGACY_DICTATED_SIZE };
}
@Override
public Size[] dictatedSizes(Mapping mapping) throws MappingException {
return baseType.dictatedSizes( mapping );
}
@Override
public int getColumnSpan(Mapping mapping) throws MappingException {
return 1;
}
@Override
public Object fromXMLNode(Node xml, Mapping factory) throws HibernateException {
Method valueOfMethod = ReflectionUtils.getMethodOrNull(clazz, "valueOf", String.class);
return valueOfMethod != null ? ReflectionUtils.invokeStaticMethod(valueOfMethod, xml.getText()) : null;
}
@Override
public Size[] defaultSizes(Mapping mapping) throws MappingException {
return baseType.defaultSizes( mapping );
}