1 /*
2 * Hibernate, Relational Persistence for Idiomatic Java
3 *
4 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5 * indicated by the @author tags or express copyright attribution
6 * statements applied by the authors. All third-party contributions are
7 * distributed under license by Red Hat Middleware LLC.
8 *
9 * This copyrighted material is made available to anyone wishing to use, modify,
10 * copy, or redistribute it subject to the terms and conditions of the GNU
11 * Lesser General Public License, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this distribution; if not, write to:
20 * Free Software Foundation, Inc.
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301 USA
23 *
24 */
25 package org.hibernate.cfg;
26
27 import java.util.Collections;
28 import java.util.Iterator;
29 import java.util.Map;
30
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.hibernate.MappingException;
34 import org.hibernate.mapping.Collection;
35 import org.hibernate.mapping.IndexedCollection;
36 import org.hibernate.mapping.OneToMany;
37 import org.hibernate.mapping.Selectable;
38 import org.hibernate.mapping.Value;
39
40 /**
41 * Collection second pass
42 *
43 * @author Emmanuel Bernard
44 */
45 public abstract class CollectionSecondPass implements SecondPass {
46 private static Logger log = LoggerFactory.getLogger( CollectionSecondPass.class );
47 Mappings mappings;
48 Collection collection;
49 private Map localInheritedMetas;
50
51 public CollectionSecondPass(Mappings mappings, Collection collection, java.util.Map inheritedMetas) {
52 this.collection = collection;
53 this.mappings = mappings;
54 this.localInheritedMetas = inheritedMetas;
55 }
56
57 public CollectionSecondPass(Mappings mappings, Collection collection) {
58 this(mappings, collection, Collections.EMPTY_MAP);
59 }
60
61 public void doSecondPass(java.util.Map persistentClasses)
62 throws MappingException {
63 if ( log.isDebugEnabled() )
64 log.debug( "Second pass for collection: " + collection.getRole() );
65
66 secondPass( persistentClasses, localInheritedMetas ); // using local since the inheritedMetas at this point is not the correct map since it is always the empty map
67 collection.createAllKeys();
68
69 if ( log.isDebugEnabled() ) {
70 String msg = "Mapped collection key: " + columns( collection.getKey() );
71 if ( collection.isIndexed() )
72 msg += ", index: " + columns( ( (IndexedCollection) collection ).getIndex() );
73 if ( collection.isOneToMany() ) {
74 msg += ", one-to-many: "
75 + ( (OneToMany) collection.getElement() ).getReferencedEntityName();
76 }
77 else {
78 msg += ", element: " + columns( collection.getElement() );
79 }
80 log.debug( msg );
81 }
82 }
83
84 abstract public void secondPass(java.util.Map persistentClasses, java.util.Map inheritedMetas)
85 throws MappingException;
86
87 private static String columns(Value val) {
88 StringBuffer columns = new StringBuffer();
89 Iterator iter = val.getColumnIterator();
90 while ( iter.hasNext() ) {
91 columns.append( ( (Selectable) iter.next() ).getText() );
92 if ( iter.hasNext() ) columns.append( ", " );
93 }
94 return columns.toString();
95 }
96 }