1 package org.hibernate.mapping; 3 4 import java.util.ArrayList ; 5 import java.util.Iterator ; 6 import java.util.Map ; 7 8 import org.hibernate.MappingException; 9 import org.hibernate.type.EntityType; 10 import org.hibernate.type.Type; 11 import org.hibernate.type.TypeFactory; 12 13 17 public class ManyToOne extends ToOne { 18 19 private boolean ignoreNotFound; 20 21 public ManyToOne(Table table) { 22 super(table); 23 } 24 25 public Type getType() throws MappingException { 26 return TypeFactory.manyToOne( 27 getReferencedEntityName(), 28 getReferencedPropertyName(), 29 isLazy(), 30 isUnwrapProxy(), 31 isEmbedded(), 32 isIgnoreNotFound() 33 ); 34 } 35 36 public void createForeignKey() throws MappingException { 37 if (referencedPropertyName==null && !hasFormula() ) { 39 createForeignKeyOfEntity( ( (EntityType) getType() ).getAssociatedEntityName() ); 40 } 41 } 42 43 public void createPropertyRefConstraints(Map persistentClasses) { 44 if (referencedPropertyName!=null) { 45 PersistentClass pc = (PersistentClass) persistentClasses.get(getReferencedEntityName() ); 46 47 Property property = pc.getReferencedProperty( getReferencedPropertyName() ); 48 49 if (property==null) { 50 throw new MappingException( 51 "Could not find property " + 52 getReferencedPropertyName() + 53 " on " + 54 getReferencedEntityName() 55 ); 56 } 57 else { 58 if ( !hasFormula() && !"none".equals( getForeignKeyName() ) ) { 59 java.util.List refColumns = new ArrayList (); 60 Iterator iter = property.getColumnIterator(); 61 while ( iter.hasNext() ) { 62 Column col = (Column) iter.next(); 63 refColumns.add( col ); 64 } 65 66 ForeignKey fk = getTable().createForeignKey( 67 getForeignKeyName(), 68 getConstraintColumns(), 69 ( (EntityType) getType() ).getAssociatedEntityName(), 70 refColumns 71 ); 72 fk.setCascadeDeleteEnabled(isCascadeDeleteEnabled() ); 73 } 74 } 75 } 76 } 77 78 public Object accept(ValueVisitor visitor) { 79 return visitor.accept(this); 80 } 81 82 public boolean isIgnoreNotFound() { 83 return ignoreNotFound; 84 } 85 86 public void setIgnoreNotFound(boolean ignoreNotFound) { 87 this.ignoreNotFound = ignoreNotFound; 88 } 89 90 91 } 92 | Popular Tags |