下面列出了怎么用org.hibernate.engine.Mapping的API类实例代码及写法,或者点击链接到github查看源代码。
public String sqlTemporaryTableCreateString(Dialect dialect, Mapping mapping) throws HibernateException {
StringBuffer buffer = new StringBuffer( dialect.getCreateTemporaryTableString() )
.append( ' ' )
.append( name )
.append( " (" );
Iterator itr = getColumnIterator();
while ( itr.hasNext() ) {
final Column column = (Column) itr.next();
buffer.append( column.getQuotedName( dialect ) ).append( ' ' );
buffer.append( column.getSqlType( dialect, mapping ) );
if ( column.isNullable() ) {
buffer.append( dialect.getNullColumnString() );
}
else {
buffer.append( " not null" );
}
if ( itr.hasNext() ) {
buffer.append( ", " );
}
}
buffer.append( ") " );
buffer.append( dialect.getCreateTemporaryTablePostfix() );
return buffer.toString();
}
public String sqlTemporaryTableCreateString(Dialect dialect, Mapping mapping) throws HibernateException {
StringBuffer buffer = new StringBuffer( dialect.getCreateTemporaryTableString() )
.append( ' ' )
.append( name )
.append( " (" );
Iterator itr = getColumnIterator();
while ( itr.hasNext() ) {
final Column column = (Column) itr.next();
buffer.append( column.getQuotedName( dialect ) ).append( ' ' );
buffer.append( column.getSqlType( dialect, mapping ) );
if ( column.isNullable() ) {
buffer.append( dialect.getNullColumnString() );
}
else {
buffer.append( " not null" );
}
if ( itr.hasNext() ) {
buffer.append( ", " );
}
}
buffer.append( ") " );
buffer.append( dialect.getCreateTemporaryTablePostfix() );
return buffer.toString();
}
public String sqlTemporaryTableCreateString(Dialect dialect, Mapping mapping) throws HibernateException {
StringBuffer buffer = new StringBuffer( dialect.getCreateTemporaryTableString() )
.append( ' ' )
.append( name )
.append( " (" );
Iterator itr = getColumnIterator();
while ( itr.hasNext() ) {
final Column column = (Column) itr.next();
buffer.append( column.getQuotedName( dialect ) ).append( ' ' );
buffer.append( column.getSqlType( dialect, mapping ) );
if ( column.isNullable() ) {
buffer.append( dialect.getNullColumnString() );
}
else {
buffer.append( " not null" );
}
if ( itr.hasNext() ) {
buffer.append( ", " );
}
}
buffer.append( ") " );
buffer.append( dialect.getCreateTemporaryTablePostfix() );
return buffer.toString();
}
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 )
.startsWith( columnInfo.getTypeName().toLowerCase() )
|| columnInfo.getTypeCode() == col.getSqlTypeCode( mapping );
if ( !typesMatch ) {
throw new HibernateException(
"Wrong column type: " + col.getName() +
", expected: " + col.getSqlType( dialect, mapping )
);
}
}
}
}
public String sqlTemporaryTableCreateString(Dialect dialect, Mapping mapping) throws HibernateException {
StringBuffer buffer = new StringBuffer( dialect.getCreateTemporaryTableString() )
.append( ' ' )
.append( name )
.append( " (" );
Iterator itr = getColumnIterator();
while ( itr.hasNext() ) {
final Column column = (Column) itr.next();
buffer.append( column.getQuotedName( dialect ) ).append( ' ' );
buffer.append( column.getSqlType( dialect, mapping ) );
if ( column.isNullable() ) {
buffer.append( dialect.getNullColumnString() );
}
else {
buffer.append( " not null" );
}
if ( itr.hasNext() ) {
buffer.append( ", " );
}
}
buffer.append( ") " );
buffer.append( dialect.getCreateTemporaryTablePostfix() );
return buffer.toString();
}
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 static EntityPersister createClassPersister(
PersistentClass model,
CacheConcurrencyStrategy cache,
SessionFactoryImplementor factory,
Mapping cfg)
throws HibernateException {
Class persisterClass = model.getEntityPersisterClass();
if (persisterClass==null || persisterClass==SingleTableEntityPersister.class) {
return new SingleTableEntityPersister(model, cache, factory, cfg);
}
else if (persisterClass==JoinedSubclassEntityPersister.class) {
return new JoinedSubclassEntityPersister(model, cache, factory, cfg);
}
else if (persisterClass==UnionSubclassEntityPersister.class) {
return new UnionSubclassEntityPersister(model, cache, factory, cfg);
}
else {
return create(persisterClass, model, cache, factory, cfg);
}
}
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()
.startsWith( columnInfo.getTypeName().toLowerCase() )
|| 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() +
", expected: " + col.getSqlType( dialect, mapping )
);
}
}
}
}
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()
.startsWith( columnInfo.getTypeName().toLowerCase() )
|| 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() +
", expected: " + col.getSqlType( dialect, mapping )
);
}
}
}
}
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()
.startsWith( columnInfo.getTypeName().toLowerCase() )
|| 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() +
", expected: " + col.getSqlType( dialect, mapping )
);
}
}
}
}
/**
* The name of the property on the associated entity to which our FK
* refers
*
* @param factory The mappings...
* @return The appropriate property name.
* @throws MappingException Generally, if unable to resolve the associated entity name
*/
public final String getIdentifierOrUniqueKeyPropertyName(Mapping factory)
throws MappingException {
if ( isReferenceToPrimaryKey() ) {
return factory.getIdentifierPropertyName( getAssociatedEntityName() );
}
else {
return uniqueKeyPropertyName;
}
}
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()
.startsWith( columnInfo.getTypeName().toLowerCase() )
|| 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() +
", expected: " + col.getSqlType( dialect, mapping )
);
}
}
}
}
public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema) {
if ( isGenerated( dialect ) ) {
String constraintString = sqlConstraintString( dialect, getName(), defaultCatalog, defaultSchema );
StringBuffer buf = new StringBuffer( "alter table " )
.append( getTable().getQualifiedName( dialect, defaultCatalog, defaultSchema ) )
.append( constraintString );
return buf.toString();
}
else {
return null;
}
}
public void validate(Mapping mapping) throws MappingException {
if ( getKey().isCascadeDeleteEnabled() && ( !isInverse() || !isOneToMany() ) ) {
throw new MappingException(
"only inverse one-to-many associations may use on-delete=\"cascade\": "
+ getRole() );
}
if ( !getKey().isValid( mapping ) ) {
throw new MappingException(
"collection foreign key mapping has wrong number of columns: "
+ getRole()
+ " type: "
+ getKey().getType().getName() );
}
if ( !getElement().isValid( mapping ) ) {
throw new MappingException(
"collection element mapping has wrong number of columns: "
+ getRole()
+ " type: "
+ getElement().getType().getName() );
}
checkColumnDuplication();
if ( elementNodeName!=null && elementNodeName.startsWith("@") ) {
throw new MappingException("element node must not be an attribute: " + elementNodeName );
}
if ( elementNodeName!=null && elementNodeName.equals(".") ) {
throw new MappingException("element node must not be the parent: " + elementNodeName );
}
if ( nodeName!=null && nodeName.indexOf('@')>-1 ) {
throw new MappingException("collection node must not be an attribute: " + elementNodeName );
}
}
public int getColumnSpan(Mapping mapping) throws MappingException {
Type[] types = userType.getPropertyTypes();
int n=0;
for (int i=0; i<types.length; i++) {
n+=types[i].getColumnSpan(mapping);
}
return n;
}
public void validate(Mapping mapping) throws MappingException {
super.validate(mapping);
if ( !getIdentifier().isValid(mapping) ) {
throw new MappingException(
"identifier mapping has wrong number of columns: " +
getEntityName() +
" type: " +
getIdentifier().getType().getName()
);
}
checkCompositeIdentifier();
}
public String sqlCreateString(Dialect dialect, Mapping mapping, String defaultCatalog, String defaultSchema)
throws HibernateException {
return buildSqlCreateIndexString(
dialect,
getName(),
getTable(),
getColumnIterator(),
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 String sqlCreateString(
Dialect dialect,
Mapping p,
String defaultCatalog,
String defaultSchema) throws HibernateException {
return injectCatalogAndSchema( sqlCreateString, defaultCatalog, defaultSchema );
}
/**
* 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() ) {
return getIdentifierType(factory);
}
else {
Type type = factory.getReferencedPropertyType( getAssociatedEntityName(), uniqueKeyPropertyName );
if ( type.isEntityType() ) {
type = ( ( EntityType ) type).getIdentifierOrUniqueKeyType( factory );
}
return type;
}
}
public void prepareTemporaryTables(Mapping mapping, Dialect dialect) {
if ( dialect.supportsTemporaryTables() ) {
temporaryIdTableName = dialect.generateTemporaryTableName( getTable().getName() );
Table table = new Table();
table.setName( temporaryIdTableName );
Iterator itr = getTable().getPrimaryKey().getColumnIterator();
while( itr.hasNext() ) {
Column column = (Column) itr.next();
table.addColumn( (Column) column.clone() );
}
temporaryIdTableDDL = table.sqlTemporaryTableCreateString( dialect, mapping );
}
}
public SingleTableEntityPersister(PersistentClass persistentClass,
EntityRegionAccessStrategy cacheAccessStrategy,
SessionFactoryImplementor factory,
Mapping mapping) throws HibernateException {
super(persistentClass, cacheAccessStrategy, factory, mapping);
this.hasSubselectLoadableCollections = persistentClass.hasSubselectLoadableCollections();
java.util.Iterator i = persistentClass.getSubclassIterator();
while(!this.hasSubselectLoadableCollections && i.hasNext()) {
this.hasSubselectLoadableCollections = ((org.hibernate.mapping.PersistentClass) i.next()).hasSubselectLoadableCollections();
}
}
public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema) {
if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
return super.sqlCreateString( dialect, p, defaultCatalog, defaultSchema );
}
else {
return Index.buildSqlCreateIndexString( dialect, getName(), getTable(), getColumnIterator(), true,
defaultCatalog, defaultSchema );
}
}
private void initIdentifierPropertyPaths(Mapping mapping) throws MappingException {
String idProp = getIdentifierPropertyName();
if ( idProp != null ) {
propertyMapping.initPropertyPaths( idProp, getIdentifierType(), getIdentifierColumnNames(), null, mapping );
}
if ( entityMetamodel.getIdentifierProperty().isEmbedded() ) {
propertyMapping.initPropertyPaths( null, getIdentifierType(), getIdentifierColumnNames(), null, mapping );
}
if ( ! entityMetamodel.hasNonIdentifierPropertyNamedId() ) {
propertyMapping.initPropertyPaths( ENTITY_ID, getIdentifierType(), getIdentifierColumnNames(), null, mapping );
}
}
private void initDiscriminatorPropertyPath(Mapping mapping) throws MappingException {
propertyMapping.initPropertyPaths( ENTITY_CLASS,
getDiscriminatorType(),
new String[]{getDiscriminatorColumnName()},
new String[]{getDiscriminatorFormulaTemplate()},
getFactory() );
}
private boolean hasNonIdentifierPropertyNamedId(final EntityType entityType, final Mapping factory) {
// TODO : would be great to have a Mapping#hasNonIdentifierPropertyNamedId method
// I don't believe that Mapping#getReferencedPropertyType accounts for the identifier property; so
// if it returns for a property named 'id', then we should have a non-id field named id
try {
return factory.getReferencedPropertyType( entityType.getAssociatedEntityName(), EntityPersister.ENTITY_ID ) != null;
}
catch( MappingException e ) {
return false;
}
}
public CompositeElementPropertyMapping(
String[] elementColumns,
String[] elementFormulaTemplates,
AbstractComponentType compositeType,
Mapping factory)
throws MappingException {
this.compositeType = compositeType;
initComponentPropertyPaths(null, compositeType, elementColumns, elementFormulaTemplates, factory);
}
/**
* {@inheritDoc}
*/
public Object fromXMLNode(Node xml, Mapping factory) throws HibernateException {
if ( !isEmbeddedInXML ) {
return getIdentifierType(factory).fromXMLNode(xml, factory);
}
else {
return xml;
}
}
public Type getReturnType(Type columnType, Mapping mapping) throws QueryException {
int[] sqlTypes;
try {
sqlTypes = columnType.sqlTypes( mapping );
}
catch ( MappingException me ) {
throw new QueryException( me );
}
if ( sqlTypes.length != 1 ) throw new QueryException( "multi-column type in avg()" );
return Hibernate.DOUBLE;
}
public Type getReturnType(Type columnType, Mapping mapping) {
//pre H3.2 behavior: super.getReturnType(ct, m);
int[] sqlTypes;
try {
sqlTypes = columnType.sqlTypes( mapping );
}
catch ( MappingException me ) {
throw new QueryException( me );
}
if ( sqlTypes.length != 1 ) throw new QueryException( "multi-column type in sum()" );
int sqlType = sqlTypes[0];
// First allow the actual type to control the return value. (the actual underlying sqltype could actually be different)
if ( columnType == Hibernate.BIG_INTEGER ) {
return Hibernate.BIG_INTEGER;
}
else if ( columnType == Hibernate.BIG_DECIMAL ) {
return Hibernate.BIG_DECIMAL;
}
else if ( columnType == Hibernate.LONG || columnType == Hibernate.SHORT || columnType == Hibernate.INTEGER) {
return Hibernate.LONG;
}
else if ( columnType == Hibernate.FLOAT || columnType == Hibernate.DOUBLE) {
return Hibernate.DOUBLE;
}
// finally use the sqltype if == on Hibernate types did not find a match.
if ( sqlType == Types.NUMERIC ) {
return columnType; //because numeric can be anything
}
else if ( sqlType == Types.FLOAT || sqlType == Types.DOUBLE || sqlType == Types.DECIMAL || sqlType == Types.REAL) {
return Hibernate.DOUBLE;
}
else if ( sqlType == Types.BIGINT || sqlType == Types.INTEGER || sqlType == Types.SMALLINT || sqlType == Types.TINYINT ) {
return Hibernate.LONG;
}
else {
return columnType;
}
}