KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > engine > JoinHelper


1 //$Id: JoinHelper.java,v 1.10 2005/07/21 01:11:50 oneovthafew Exp $
2
package org.hibernate.engine;
3
4 import org.hibernate.persister.entity.Joinable;
5 import org.hibernate.persister.entity.OuterJoinLoadable;
6 import org.hibernate.persister.entity.PropertyMapping;
7 import org.hibernate.type.AssociationType;
8 import org.hibernate.util.ArrayHelper;
9 import org.hibernate.util.StringHelper;
10
11 /**
12  * @author Gavin King
13  */

14 public final class JoinHelper {
15     
16     private JoinHelper() {}
17     
18     /**
19      * Get the aliased columns of the owning entity which are to
20      * be used in the join
21      */

22     public static String JavaDoc[] getAliasedLHSColumnNames(
23             AssociationType type,
24             String JavaDoc alias,
25             int property,
26             OuterJoinLoadable lhsPersister,
27             Mapping mapping
28     ) {
29         return getAliasedLHSColumnNames(type, alias, property, 0, lhsPersister, mapping);
30     }
31     
32     /**
33      * Get the columns of the owning entity which are to
34      * be used in the join
35      */

36     public static String JavaDoc[] getLHSColumnNames(
37             AssociationType type,
38             int property,
39             OuterJoinLoadable lhsPersister,
40             Mapping mapping
41     ) {
42         return getLHSColumnNames(type, property, 0, lhsPersister, mapping);
43     }
44     
45     /**
46      * Get the aliased columns of the owning entity which are to
47      * be used in the join
48      */

49     public static String JavaDoc[] getAliasedLHSColumnNames(
50             AssociationType type,
51             String JavaDoc alias,
52             int property,
53             int begin,
54             OuterJoinLoadable lhsPersister,
55             Mapping mapping
56     ) {
57         if ( type.useLHSPrimaryKey() ) {
58             return StringHelper.qualify( alias, lhsPersister.getIdentifierColumnNames() );
59         }
60         else {
61             String JavaDoc propertyName = type.getLHSPropertyName();
62             if (propertyName==null) {
63                 return ArrayHelper.slice(
64                         lhsPersister.toColumns(alias, property),
65                         begin,
66                         type.getColumnSpan(mapping)
67                     );
68             }
69             else {
70                 return ( (PropertyMapping) lhsPersister ).toColumns(alias, propertyName); //bad cast
71
}
72         }
73     }
74     
75     /**
76      * Get the columns of the owning entity which are to
77      * be used in the join
78      */

79     public static String JavaDoc[] getLHSColumnNames(
80             AssociationType type,
81             int property,
82             int begin,
83             OuterJoinLoadable lhsPersister,
84             Mapping mapping
85     ) {
86         if ( type.useLHSPrimaryKey() ) {
87             //return lhsPersister.getSubclassPropertyColumnNames(property);
88
return lhsPersister.getIdentifierColumnNames();
89         }
90         else {
91             String JavaDoc propertyName = type.getLHSPropertyName();
92             if (propertyName==null) {
93                 //slice, to get the columns for this component
94
//property
95
return ArrayHelper.slice(
96                         lhsPersister.getSubclassPropertyColumnNames(property),
97                         begin,
98                         type.getColumnSpan(mapping)
99                     );
100             }
101             else {
102                 //property-refs for associations defined on a
103
//component are not supported, so no need to slice
104
return lhsPersister.getPropertyColumnNames(propertyName);
105             }
106         }
107     }
108     
109     public static String JavaDoc getLHSTableName(
110         AssociationType type,
111         int property,
112         OuterJoinLoadable lhsPersister
113     ) {
114         if ( type.useLHSPrimaryKey() ) {
115             return lhsPersister.getTableName();
116         }
117         else {
118             String JavaDoc propertyName = type.getLHSPropertyName();
119             if (propertyName==null) {
120                 //if there is no property-ref, assume the join
121
//is to the subclass table (ie. the table of the
122
//subclass that the association belongs to)
123
return lhsPersister.getSubclassPropertyTableName(property);
124             }
125             else {
126                 //handle a property-ref
127
String JavaDoc propertyRefTable = lhsPersister.getPropertyTableName(propertyName);
128                 if (propertyRefTable==null) {
129                     //it is possible that the tree-walking in OuterJoinLoader can get to
130
//an association defined by a subclass, in which case the property-ref
131
//might refer to a property defined on a subclass of the current class
132
//in this case, the table name is not known - this temporary solution
133
//assumes that the property-ref refers to a property of the subclass
134
//table that the association belongs to (a reasonable guess)
135
//TODO: fix this, add: OuterJoinLoadable.getSubclassPropertyTableName(String propertyName)
136
propertyRefTable = lhsPersister.getSubclassPropertyTableName(property);
137                 }
138                 return propertyRefTable;
139             }
140         }
141     }
142     
143     /**
144      * Get the columns of the associated table which are to
145      * be used in the join
146      */

147     public static String JavaDoc[] getRHSColumnNames(AssociationType type, SessionFactoryImplementor factory) {
148         String JavaDoc uniqueKeyPropertyName = type.getRHSUniqueKeyPropertyName();
149         Joinable joinable = type.getAssociatedJoinable(factory);
150         if (uniqueKeyPropertyName==null) {
151             return joinable.getKeyColumnNames();
152         }
153         else {
154             return ( (OuterJoinLoadable) joinable ).getPropertyColumnNames(uniqueKeyPropertyName);
155         }
156     }
157 }
158
Popular Tags