下面列出了org.hibernate.mapping.PersistentClass#addProperty ( ) 实例代码,或者点击链接到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);
}
}
protected void createBackReferences() {
if ( collectionBinding.isOneToMany()
&& !collectionBinding.isInverse()
&& !collectionBinding.getKey().isNullable() ) {
// for non-inverse one-to-many, with a not-null fk, add a backref!
String entityName = ( (OneToMany) collectionBinding.getElement() ).getReferencedEntityName();
PersistentClass referenced = mappingDocument.getMetadataCollector().getEntityBinding( entityName );
Backref prop = new Backref();
prop.setName( '_' + collectionBinding.getOwnerEntityName() + "." + pluralAttributeSource.getName() + "Backref" );
prop.setUpdateable( false );
prop.setSelectable( false );
prop.setCollectionRole( collectionBinding.getRole() );
prop.setEntityName( collectionBinding.getOwner().getEntityName() );
prop.setValue( collectionBinding.getKey() );
referenced.addProperty( prop );
log.debugf(
"Added virtual backref property [%s] : %s",
prop.getName(),
pluralAttributeSource.getAttributeRole().getFullPath()
);
}
}
private void createIndexBackRef(
MappingDocument mappingDocument,
IndexedPluralAttributeSource pluralAttributeSource,
IndexedCollection collectionBinding) {
if ( collectionBinding.isOneToMany()
&& !collectionBinding.getKey().isNullable()
&& !collectionBinding.isInverse() ) {
final String entityName = ( (OneToMany) collectionBinding.getElement() ).getReferencedEntityName();
final PersistentClass referenced = mappingDocument.getMetadataCollector().getEntityBinding( entityName );
final IndexBackref ib = new IndexBackref();
ib.setName( '_' + collectionBinding.getOwnerEntityName() + "." + pluralAttributeSource.getName() + "IndexBackref" );
ib.setUpdateable( false );
ib.setSelectable( false );
ib.setCollectionRole( collectionBinding.getRole() );
ib.setEntityName( collectionBinding.getOwner().getEntityName() );
ib.setValue( collectionBinding.getIndex() );
referenced.addProperty( ib );
}
}
@Override
protected void createBackReferences() {
super.createBackReferences();
boolean indexIsFormula = false;
Iterator itr = getCollectionBinding().getIndex().getColumnIterator();
while ( itr.hasNext() ) {
if ( ( (Selectable) itr.next() ).isFormula() ) {
indexIsFormula = true;
}
}
if ( getCollectionBinding().isOneToMany()
&& !getCollectionBinding().getKey().isNullable()
&& !getCollectionBinding().isInverse()
&& !indexIsFormula ) {
final String entityName = ( (OneToMany) getCollectionBinding().getElement() ).getReferencedEntityName();
final PersistentClass referenced = getMappingDocument().getMetadataCollector().getEntityBinding( entityName );
final IndexBackref ib = new IndexBackref();
ib.setName( '_' + getCollectionBinding().getOwnerEntityName() + "." + getPluralAttributeSource().getName() + "IndexBackref" );
ib.setUpdateable( false );
ib.setSelectable( false );
ib.setCollectionRole( getCollectionBinding().getRole() );
ib.setEntityName( getCollectionBinding().getOwner().getEntityName() );
ib.setValue( getCollectionBinding().getIndex() );
referenced.addProperty( ib );
}
}
/**
* Called for Lists, arrays, primitive arrays
*/
public static void bindListSecondPass(Element node, List list, java.util.Map classes,
Mappings mappings, java.util.Map inheritedMetas) throws MappingException {
bindCollectionSecondPass( node, list, classes, mappings, inheritedMetas );
Element subnode = node.element( "list-index" );
if ( subnode == null ) subnode = node.element( "index" );
SimpleValue iv = new SimpleValue( list.getCollectionTable() );
bindSimpleValue(
subnode,
iv,
list.isOneToMany(),
IndexedCollection.DEFAULT_INDEX_COLUMN_NAME,
mappings
);
iv.setTypeName( "integer" );
list.setIndex( iv );
String baseIndex = subnode.attributeValue( "base" );
if ( baseIndex != null ) list.setBaseIndex( Integer.parseInt( baseIndex ) );
list.setIndexNodeName( subnode.attributeValue("node") );
if ( list.isOneToMany() && !list.getKey().isNullable() && !list.isInverse() ) {
String entityName = ( (OneToMany) list.getElement() ).getReferencedEntityName();
PersistentClass referenced = mappings.getClass( entityName );
IndexBackref ib = new IndexBackref();
ib.setName( '_' + node.attributeValue( "name" ) + "IndexBackref" );
ib.setUpdateable( false );
ib.setSelectable( false );
ib.setCollectionRole( list.getRole() );
ib.setEntityName( list.getOwner().getEntityName() );
ib.setValue( list.getIndex() );
// ( (Column) ( (SimpleValue) ic.getIndex() ).getColumnIterator().next()
// ).setNullable(false);
referenced.addProperty( ib );
}
}
protected void bindOneToManySecondPass(
Collection collection,
Map persistentClasses,
Ejb3JoinColumn[] fkJoinColumns,
XClass collectionType,
boolean cascadeDeleteEnabled,
boolean ignoreNotFound,
MetadataBuildingContext buildingContext,
Map<XClass, InheritanceState> inheritanceStatePerClass) {
final boolean debugEnabled = LOG.isDebugEnabled();
if ( debugEnabled ) {
LOG.debugf( "Binding a OneToMany: %s.%s through a foreign key", propertyHolder.getEntityName(), propertyName );
}
if ( buildingContext == null ) {
throw new AssertionFailure(
"CollectionSecondPass for oneToMany should not be called with null mappings"
);
}
org.hibernate.mapping.OneToMany oneToMany = new org.hibernate.mapping.OneToMany( buildingContext, collection.getOwner() );
collection.setElement( oneToMany );
oneToMany.setReferencedEntityName( collectionType.getName() );
oneToMany.setIgnoreNotFound( ignoreNotFound );
String assocClass = oneToMany.getReferencedEntityName();
PersistentClass associatedClass = (PersistentClass) persistentClasses.get( assocClass );
if ( jpaOrderBy != null ) {
final String orderByFragment = buildOrderByClauseFromHql(
jpaOrderBy.value(),
associatedClass,
collection.getRole()
);
if ( StringHelper.isNotEmpty( orderByFragment ) ) {
collection.setOrderBy( orderByFragment );
}
}
Map<String, Join> joins = buildingContext.getMetadataCollector().getJoins( assocClass );
if ( associatedClass == null ) {
throw new MappingException(
String.format("Association [%s] for entity [%s] references unmapped class [%s]",
propertyName, propertyHolder.getClassName(), assocClass)
);
}
oneToMany.setAssociatedClass( associatedClass );
for (Ejb3JoinColumn column : fkJoinColumns) {
column.setPersistentClass( associatedClass, joins, inheritanceStatePerClass );
column.setJoins( joins );
collection.setCollectionTable( column.getTable() );
}
if ( debugEnabled ) {
LOG.debugf( "Mapping collection: %s -> %s", collection.getRole(), collection.getCollectionTable().getName() );
}
bindFilters( false );
bindCollectionSecondPass( collection, null, fkJoinColumns, cascadeDeleteEnabled, property, propertyHolder, buildingContext );
if ( !collection.isInverse()
&& !collection.getKey().isNullable() ) {
// for non-inverse one-to-many, with a not-null fk, add a backref!
String entityName = oneToMany.getReferencedEntityName();
PersistentClass referenced = buildingContext.getMetadataCollector().getEntityBinding( entityName );
Backref prop = new Backref();
prop.setName( '_' + fkJoinColumns[0].getPropertyName() + '_' + fkJoinColumns[0].getLogicalColumnName() + "Backref" );
prop.setUpdateable( false );
prop.setSelectable( false );
prop.setCollectionRole( collection.getRole() );
prop.setEntityName( collection.getOwner().getEntityName() );
prop.setValue( collection.getKey() );
referenced.addProperty( prop );
}
}
private void bindIndex(final MetadataBuildingContext buildingContext) {
if ( !indexColumn.isImplicit() ) {
PropertyHolder valueHolder = PropertyHolderBuilder.buildPropertyHolder(
this.collection,
StringHelper.qualify( this.collection.getRole(), "key" ),
null,
null,
propertyHolder,
getBuildingContext()
);
List list = (List) this.collection;
if ( !list.isOneToMany() ) indexColumn.forceNotNull();
indexColumn.setPropertyHolder( valueHolder );
SimpleValueBinder value = new SimpleValueBinder();
value.setColumns( new Ejb3Column[] { indexColumn } );
value.setExplicitType( "integer" );
value.setBuildingContext( getBuildingContext() );
SimpleValue indexValue = value.make();
indexColumn.linkWithValue( indexValue );
list.setIndex( indexValue );
list.setBaseIndex( indexColumn.getBase() );
if ( list.isOneToMany() && !list.getKey().isNullable() && !list.isInverse() ) {
String entityName = ( (OneToMany) list.getElement() ).getReferencedEntityName();
PersistentClass referenced = buildingContext.getMetadataCollector().getEntityBinding( entityName );
IndexBackref ib = new IndexBackref();
ib.setName( '_' + propertyName + "IndexBackref" );
ib.setUpdateable( false );
ib.setSelectable( false );
ib.setCollectionRole( list.getRole() );
ib.setEntityName( list.getOwner().getEntityName() );
ib.setValue( list.getIndex() );
referenced.addProperty( ib );
}
}
else {
Collection coll = this.collection;
throw new AnnotationException(
"List/array has to be annotated with an @OrderColumn (or @IndexColumn): "
+ coll.getRole()
);
}
}
public static void bindCompositeId(Element node, Component component,
PersistentClass persistentClass, String propertyName, Mappings mappings,
java.util.Map inheritedMetas) throws MappingException {
component.setKey( true );
String path = StringHelper.qualify(
persistentClass.getEntityName(),
propertyName == null ? "id" : propertyName );
bindComponent(
node,
component,
persistentClass.getClassName(),
propertyName,
path,
false,
node.attribute( "class" ) == null
&& propertyName == null,
mappings,
inheritedMetas,
false
);
if ( "true".equals( node.attributeValue("mapped") ) ) {
if ( propertyName!=null ) {
throw new MappingException("cannot combine mapped=\"true\" with specified name");
}
Component mapper = new Component(persistentClass);
bindComponent(
node,
mapper,
persistentClass.getClassName(),
null,
path,
false,
true,
mappings,
inheritedMetas,
true
);
persistentClass.setIdentifierMapper(mapper);
Property property = new Property();
property.setName("_identifierMapper");
property.setNodeName("id");
property.setUpdateable(false);
property.setInsertable(false);
property.setValue(mapper);
property.setPropertyAccessorName( "embedded" );
persistentClass.addProperty(property);
}
}
/**
* Called for Maps
*/
public static void bindMapSecondPass(Element node, Map map, java.util.Map classes,
Mappings mappings, java.util.Map inheritedMetas) throws MappingException {
bindCollectionSecondPass( node, map, classes, mappings, inheritedMetas );
Iterator iter = node.elementIterator();
while ( iter.hasNext() ) {
Element subnode = (Element) iter.next();
String name = subnode.getName();
if ( "index".equals( name ) || "map-key".equals( name ) ) {
SimpleValue value = new SimpleValue( map.getCollectionTable() );
bindSimpleValue(
subnode,
value,
map.isOneToMany(),
IndexedCollection.DEFAULT_INDEX_COLUMN_NAME,
mappings
);
if ( !value.isTypeSpecified() ) {
throw new MappingException( "map index element must specify a type: "
+ map.getRole() );
}
map.setIndex( value );
map.setIndexNodeName( subnode.attributeValue("node") );
}
else if ( "index-many-to-many".equals( name ) || "map-key-many-to-many".equals( name ) ) {
ManyToOne mto = new ManyToOne( map.getCollectionTable() );
bindManyToOne(
subnode,
mto,
IndexedCollection.DEFAULT_INDEX_COLUMN_NAME,
map.isOneToMany(),
mappings
);
map.setIndex( mto );
}
else if ( "composite-index".equals( name ) || "composite-map-key".equals( name ) ) {
Component component = new Component( map );
bindComposite(
subnode,
component,
map.getRole() + ".index",
map.isOneToMany(),
mappings,
inheritedMetas
);
map.setIndex( component );
}
else if ( "index-many-to-any".equals( name ) ) {
Any any = new Any( map.getCollectionTable() );
bindAny( subnode, any, map.isOneToMany(), mappings );
map.setIndex( any );
}
}
// TODO: this is a bit of copy/paste from IndexedCollection.createPrimaryKey()
boolean indexIsFormula = false;
Iterator colIter = map.getIndex().getColumnIterator();
while ( colIter.hasNext() ) {
if ( ( (Selectable) colIter.next() ).isFormula() ) indexIsFormula = true;
}
if ( map.isOneToMany() && !map.getKey().isNullable() && !map.isInverse() && !indexIsFormula ) {
String entityName = ( (OneToMany) map.getElement() ).getReferencedEntityName();
PersistentClass referenced = mappings.getClass( entityName );
IndexBackref ib = new IndexBackref();
ib.setName( '_' + node.attributeValue( "name" ) + "IndexBackref" );
ib.setUpdateable( false );
ib.setSelectable( false );
ib.setCollectionRole( map.getRole() );
ib.setEntityName( map.getOwner().getEntityName() );
ib.setValue( map.getIndex() );
// ( (Column) ( (SimpleValue) ic.getIndex() ).getColumnIterator().next()
// ).setNullable(false);
referenced.addProperty( ib );
}
}