KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > mapping > ManyToOne


1 //$Id: ManyToOne.java,v 1.17 2005/07/21 01:11:51 oneovthafew Exp $
2
package org.hibernate.mapping;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Map JavaDoc;
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 /**
14  * A many-to-one association mapping
15  * @author Gavin King
16  */

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         // the case of a foreign key to something other than the pk is handled in createPropertyRefConstraints
38
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 JavaDoc refColumns = new ArrayList JavaDoc();
60                     Iterator JavaDoc 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 JavaDoc 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