KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > cfg > annotations > MapBinder


1 //$Id: MapBinder.java,v 1.2 2005/07/22 22:36:07 epbernard Exp $
2
package org.hibernate.cfg.annotations;
3
4 import java.util.Map JavaDoc;
5
6 import org.hibernate.mapping.Collection;
7 import org.hibernate.mapping.PersistentClass;
8 import org.hibernate.mapping.Property;
9 import org.hibernate.cfg.SecondPass;
10 import org.hibernate.cfg.ExtendedMappings;
11 import org.hibernate.cfg.Ejb3JoinColumn;
12 import org.hibernate.cfg.BinderHelper;
13 import org.hibernate.FetchMode;
14 import org.hibernate.MappingException;
15 import org.hibernate.AssertionFailure;
16 import org.hibernate.AnnotationException;
17
18 /**
19  * Implementation to bind a Map
20  *
21  * @author Emmanuel Bernard
22  */

23 public class MapBinder extends CollectionBinder {
24
25     protected Collection createCollection(PersistentClass persistentClass) {
26         return new org.hibernate.mapping.Map(persistentClass);
27     }
28
29     public SecondPass getSecondPass(final ExtendedMappings mappings,
30                                               final Ejb3JoinColumn[] keyColumns,
31                                               final Ejb3JoinColumn[] inverseColumns,
32                                               final String JavaDoc collType,
33                                               final FetchMode fetchMode,
34                                               final boolean unique) {
35         if (inverseColumns != null) {
36             return new SecondPass(mappings, MapBinder.this.collection) {
37                 public void secondPass(Map JavaDoc persistentClasses, Map JavaDoc inheritedMetas)
38                         throws MappingException {
39                     bindManyToManySecondPass(MapBinder.this.collection,
40                             persistentClasses,
41                             keyColumns,
42                             inverseColumns,
43                             collType,
44                             fetchMode,
45                             unique,
46                             cascadeDeleteEnabled, (ExtendedMappings) mappings);
47                     bindKeyFromAssociationTable(collType, persistentClasses, mapKeyPropertyName, mappings);
48                 }
49             };
50         }
51         else {
52             return new SecondPass(mappings, collection) {
53                 public void secondPass(Map JavaDoc persistentClasses, Map JavaDoc inheritedMetas)
54                         throws MappingException {
55                     bindCollectionSecondPass(MapBinder.this.collection,
56                             persistentClasses,
57                             keyColumns,
58                             cascadeDeleteEnabled, hqlOrderBy, (ExtendedMappings) mappings);
59                     if ( MapBinder.this.collection.isOneToMany() ) {
60                         org.hibernate.mapping.OneToMany oneToMany = (org.hibernate.mapping.OneToMany)
61                                 MapBinder.this.collection.getElement();
62                         String JavaDoc collType = oneToMany.getReferencedEntityName();
63                         bindKeyFromAssociationTable(collType, persistentClasses, mapKeyPropertyName, mappings);
64                     }
65                     else {
66                         throw new AssertionFailure("Should be a one to many");
67                     }
68                 }
69             };
70         }
71     }
72
73     private void bindKeyFromAssociationTable(
74             String JavaDoc collType, Map JavaDoc persistentClasses, String JavaDoc mapKeyPropertyName, ExtendedMappings mappings
75             ) {
76         if (mapKeyPropertyName == null) throw new AnnotationException("A Map must declare a @MapKey element");
77         PersistentClass associatedClass = (PersistentClass) persistentClasses.get(collType);
78         if (associatedClass == null) throw new AnnotationException("Associated class not found: " + collType);
79         Property property = BinderHelper.findPropertyByName( associatedClass, mapKeyPropertyName);
80         if (property == null) throw new AnnotationException("Map key property not found: " + collType + "." + mapKeyPropertyName);
81         org.hibernate.mapping.Map map = (org.hibernate.mapping.Map) this.collection;
82         map.setIndex( property.getValue() );
83     }
84 }
85
Popular Tags