Left: | ||
Right: |
OLD | NEW |
---|---|
1 /** | 1 /** |
2 * Copyright (C) 2007 Google Inc. | 2 * Copyright (C) 2007 Google Inc. |
3 * | 3 * |
4 * This library is free software; you can redistribute it and/or | 4 * This library is free software; you can redistribute it and/or |
5 * modify it under the terms of the GNU Lesser General Public | 5 * modify it under the terms of the GNU Lesser General Public |
6 * License as published by the Free Software Foundation; either | 6 * License as published by the Free Software Foundation; either |
7 * version 2.1 of the License, or (at your option) any later version. | 7 * version 2.1 of the License, or (at your option) any later version. |
8 | 8 |
9 * This library is distributed in the hope that it will be useful, | 9 * This library is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 * Lesser General Public License for more details. | 12 * Lesser General Public License for more details. |
13 | 13 |
14 * You should have received a copy of the GNU Lesser General Public | 14 * You should have received a copy of the GNU Lesser General Public |
15 * License along with this library; if not, write to the Free Software | 15 * License along with this library; if not, write to the Free Software |
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US A | 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US A |
17 */ | 17 */ |
18 | 18 |
19 package org.hibernate.shards; | 19 package org.hibernate.shards; |
20 | 20 |
21 import org.apache.commons.logging.Log; | 21 import org.apache.commons.logging.Log; |
22 import org.apache.commons.logging.LogFactory; | 22 import org.apache.commons.logging.LogFactory; |
23 import org.hibernate.SessionFactory; | 23 import org.hibernate.SessionFactory; |
24 import org.hibernate.InvalidMappingException; | |
24 import org.hibernate.cfg.Configuration; | 25 import org.hibernate.cfg.Configuration; |
25 import org.hibernate.cfg.Environment; | 26 import org.hibernate.cfg.Environment; |
26 import org.hibernate.engine.SessionFactoryImplementor; | 27 import org.hibernate.engine.SessionFactoryImplementor; |
27 import org.hibernate.mapping.OneToOne; | 28 import org.hibernate.mapping.OneToOne; |
28 import org.hibernate.mapping.PersistentClass; | 29 import org.hibernate.mapping.PersistentClass; |
29 import org.hibernate.mapping.Property; | 30 import org.hibernate.mapping.Property; |
30 import org.hibernate.shards.cfg.ShardConfiguration; | 31 import org.hibernate.shards.cfg.ShardConfiguration; |
31 import org.hibernate.shards.cfg.ShardedEnvironment; | 32 import org.hibernate.shards.cfg.ShardedEnvironment; |
32 import org.hibernate.shards.session.ShardedSessionFactory; | 33 import org.hibernate.shards.session.ShardedSessionFactory; |
33 import org.hibernate.shards.session.ShardedSessionFactoryImpl; | 34 import org.hibernate.shards.session.ShardedSessionFactoryImpl; |
34 import org.hibernate.shards.strategy.ShardStrategyFactory; | 35 import org.hibernate.shards.strategy.ShardStrategyFactory; |
36 import org.hibernate.shards.strategy.selection.Replicated; | |
35 import org.hibernate.shards.util.Maps; | 37 import org.hibernate.shards.util.Maps; |
36 import org.hibernate.shards.util.Preconditions; | 38 import org.hibernate.shards.util.Preconditions; |
37 import org.hibernate.shards.util.Sets; | 39 import org.hibernate.shards.util.Sets; |
38 import org.hibernate.util.PropertiesHelper; | 40 import org.hibernate.util.PropertiesHelper; |
39 | 41 |
40 import java.util.Collections; | 42 import java.util.Collections; |
41 import java.util.Iterator; | 43 import java.util.Iterator; |
42 import java.util.List; | 44 import java.util.List; |
43 import java.util.Map; | 45 import java.util.Map; |
44 import java.util.Set; | 46 import java.util.Set; |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 // described by this config | 176 // described by this config |
175 virtualShardIds = shardToVirtualShardIdMap.get(shardId); | 177 virtualShardIds = shardToVirtualShardIdMap.get(shardId); |
176 } | 178 } |
177 sessionFactories.put(buildSessionFactory(), virtualShardIds); | 179 sessionFactories.put(buildSessionFactory(), virtualShardIds); |
178 } | 180 } |
179 final boolean doFullCrossShardRelationshipChecking = | 181 final boolean doFullCrossShardRelationshipChecking = |
180 PropertiesHelper.getBoolean( | 182 PropertiesHelper.getBoolean( |
181 ShardedEnvironment.CHECK_ALL_ASSOCIATED_OBJECTS_FOR_DIFFERENT_SHARDS , | 183 ShardedEnvironment.CHECK_ALL_ASSOCIATED_OBJECTS_FOR_DIFFERENT_SHARDS , |
182 prototypeConfiguration.getProperties(), | 184 prototypeConfiguration.getProperties(), |
183 true); | 185 true); |
186 · | |
187 //verify the replication behavior of this mapping when we build the session factory | |
188 verifyReplicatedCascadeBehavior(); | |
189 | |
184 return | 190 return |
185 new ShardedSessionFactoryImpl( | 191 new ShardedSessionFactoryImpl( |
186 sessionFactories, | 192 sessionFactories, |
187 shardStrategyFactory, | 193 shardStrategyFactory, |
188 classesWithoutTopLevelSaveSupport, | 194 classesWithoutTopLevelSaveSupport, |
189 doFullCrossShardRelationshipChecking); | 195 doFullCrossShardRelationshipChecking); |
190 } | 196 } |
191 | 197 |
198 | |
199 /** | |
200 * Verify the replication behavior between non-replicated classes and replica ted classes. | |
201 * Specifically there should be no cascades from classes that are not replica ted, and classes that are replicated. | |
Max Ross
2008/11/16 21:58:47
Please explain why such cascades are problematic.
| |
202 */ | |
203 protected void verifyReplicatedCascadeBehavior() { | |
204 //ensure that the mappings are built before proceding | |
205 this.prototypeConfiguration.buildMappings(); | |
206 @SuppressWarnings("unchecked") | |
207 Iterator<PersistentClass> persistentClasses = (Iterator<PersistentClass>) th is.prototypeConfiguration.getClassMappings(); | |
Max Ross
2008/11/16 21:58:47
nit: no need for 'this' prefix. We just do this i
| |
208 while (persistentClasses.hasNext()) { | |
209 PersistentClass ps = persistentClasses.next(); | |
210 Class psClass = ps.getMappedClass(); | |
211 if (psClass.isAnnotationPresent(Replicated.class)) continue; | |
Max Ross
2008/11/16 21:58:47
Please use curly braces even for one line conditio
| |
212 @SuppressWarnings("unchecked") | |
213 Iterator<Property> properties = ps.getPropertyIterator(); | |
214 ·················· | |
215 while (properties.hasNext()) { | |
216 Property property = properties.next(); | |
217 String cascade = property.getCascade(); | |
218 if (cascade==null || cascade.equalsIgnoreCase("NONE")) continue; | |
Max Ross
2008/11/16 21:58:47
see above style comment
Max Ross
2008/11/16 21:58:47
Is there really no constant in Hibernate core that
| |
219 Class returnType = property.getValue().getType().getReturnedClass(); | |
220 if (returnType.isAnnotationPresent(Replicated.class)) { | |
221 throw new InvalidMappingException("Illegal cascade from non-replicated class(property="+ps.getClassName()+") to replilcated class("+property.getName() +")",psClass.toString(), (String)null); | |
Max Ross
2008/11/16 21:58:47
replilcated -> replicated
Max Ross
2008/11/16 21:58:47
If you just throw MappingException you can skip th
| |
222 } | |
223 } | |
224 } | |
225 } | |
226 | |
192 /** | 227 /** |
193 * @return the Set of mapped classes that don't support top level saves | 228 * @return the Set of mapped classes that don't support top level saves |
194 */ | 229 */ |
195 @SuppressWarnings("unchecked") | 230 @SuppressWarnings("unchecked") |
196 private Set<Class<?>> determineClassesWithoutTopLevelSaveSupport(Configuration config) { | 231 private Set<Class<?>> determineClassesWithoutTopLevelSaveSupport(Configuration config) { |
197 Set<Class<?>> classesWithoutTopLevelSaveSupport = Sets.newHashSet(); | 232 Set<Class<?>> classesWithoutTopLevelSaveSupport = Sets.newHashSet(); |
198 for(Iterator<PersistentClass> pcIter = config.getClassMappings(); pcIter.has Next(); ) { | 233 for(Iterator<PersistentClass> pcIter = config.getClassMappings(); pcIter.has Next(); ) { |
199 PersistentClass pc = pcIter.next(); | 234 PersistentClass pc = pcIter.next(); |
200 for(Iterator<Property> propIter = pc.getPropertyIterator(); propIter.hasNe xt(); ) { | 235 for(Iterator<Property> propIter = pc.getPropertyIterator(); propIter.hasNe xt(); ) { |
201 if(doesNotSupportTopLevelSave(propIter.next())) { | 236 if(doesNotSupportTopLevelSave(propIter.next())) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
244 } | 279 } |
245 } | 280 } |
246 | 281 |
247 /** | 282 /** |
248 * Helper function that creates an actual SessionFactory. | 283 * Helper function that creates an actual SessionFactory. |
249 */ | 284 */ |
250 private SessionFactoryImplementor buildSessionFactory() { | 285 private SessionFactoryImplementor buildSessionFactory() { |
251 return (SessionFactoryImplementor) prototypeConfiguration.buildSessionFactor y(); | 286 return (SessionFactoryImplementor) prototypeConfiguration.buildSessionFactor y(); |
252 } | 287 } |
253 } | 288 } |
OLD | NEW |