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; |
| 28 import org.hibernate.engine.CascadeStyle; |
27 import org.hibernate.mapping.OneToOne; | 29 import org.hibernate.mapping.OneToOne; |
28 import org.hibernate.mapping.PersistentClass; | 30 import org.hibernate.mapping.PersistentClass; |
29 import org.hibernate.mapping.Property; | 31 import org.hibernate.mapping.Property; |
30 import org.hibernate.shards.cfg.ShardConfiguration; | 32 import org.hibernate.shards.cfg.ShardConfiguration; |
31 import org.hibernate.shards.cfg.ShardedEnvironment; | 33 import org.hibernate.shards.cfg.ShardedEnvironment; |
32 import org.hibernate.shards.session.ShardedSessionFactory; | 34 import org.hibernate.shards.session.ShardedSessionFactory; |
33 import org.hibernate.shards.session.ShardedSessionFactoryImpl; | 35 import org.hibernate.shards.session.ShardedSessionFactoryImpl; |
34 import org.hibernate.shards.strategy.ShardStrategyFactory; | 36 import org.hibernate.shards.strategy.ShardStrategyFactory; |
| 37 import org.hibernate.shards.strategy.selection.Replicated; |
35 import org.hibernate.shards.util.Maps; | 38 import org.hibernate.shards.util.Maps; |
36 import org.hibernate.shards.util.Preconditions; | 39 import org.hibernate.shards.util.Preconditions; |
37 import org.hibernate.shards.util.Sets; | 40 import org.hibernate.shards.util.Sets; |
38 import org.hibernate.util.PropertiesHelper; | 41 import org.hibernate.util.PropertiesHelper; |
39 | 42 |
40 import java.util.Collections; | 43 import java.util.Collections; |
41 import java.util.Iterator; | 44 import java.util.Iterator; |
42 import java.util.List; | 45 import java.util.List; |
43 import java.util.Map; | 46 import java.util.Map; |
44 import java.util.Set; | 47 import java.util.Set; |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 // described by this config | 177 // described by this config |
175 virtualShardIds = shardToVirtualShardIdMap.get(shardId); | 178 virtualShardIds = shardToVirtualShardIdMap.get(shardId); |
176 } | 179 } |
177 sessionFactories.put(buildSessionFactory(), virtualShardIds); | 180 sessionFactories.put(buildSessionFactory(), virtualShardIds); |
178 } | 181 } |
179 final boolean doFullCrossShardRelationshipChecking = | 182 final boolean doFullCrossShardRelationshipChecking = |
180 PropertiesHelper.getBoolean( | 183 PropertiesHelper.getBoolean( |
181 ShardedEnvironment.CHECK_ALL_ASSOCIATED_OBJECTS_FOR_DIFFERENT_SHARDS
, | 184 ShardedEnvironment.CHECK_ALL_ASSOCIATED_OBJECTS_FOR_DIFFERENT_SHARDS
, |
182 prototypeConfiguration.getProperties(), | 185 prototypeConfiguration.getProperties(), |
183 true); | 186 true); |
| 187 · |
| 188 //verify the replication behavior of this mapping when we build the session
factory |
| 189 verifyReplicatedCascadeBehavior(); |
| 190 |
184 return | 191 return |
185 new ShardedSessionFactoryImpl( | 192 new ShardedSessionFactoryImpl( |
186 sessionFactories, | 193 sessionFactories, |
187 shardStrategyFactory, | 194 shardStrategyFactory, |
188 classesWithoutTopLevelSaveSupport, | 195 classesWithoutTopLevelSaveSupport, |
189 doFullCrossShardRelationshipChecking); | 196 doFullCrossShardRelationshipChecking); |
190 } | 197 } |
191 | 198 |
| 199 |
| 200 /** |
| 201 * Verify the replication behavior between non-replicated classes and replica
ted classes. |
| 202 * Specifically there should be no cascades from classes that are not replica
ted to· |
| 203 * classes that are replicated. |
| 204 *· |
| 205 * This is an issue because a replicated class exists on all shards, so any c
hange or addition |
| 206 * of a replicated class needs happen across all shards, and a cascade from a
n entity that exists |
| 207 * on one shard would not facilitate this.· |
| 208 */ |
| 209 protected void verifyReplicatedCascadeBehavior() { |
| 210 //ensure that the mappings are built before proceding |
| 211 this.prototypeConfiguration.buildMappings(); |
| 212 @SuppressWarnings("unchecked") |
| 213 Iterator<PersistentClass> persistentClasses = (Iterator<PersistentClass>) pr
ototypeConfiguration.getClassMappings(); |
| 214 while (persistentClasses.hasNext()) { |
| 215 PersistentClass ps = persistentClasses.next(); |
| 216 Class psClass = ps.getMappedClass(); |
| 217 if (psClass.isAnnotationPresent(Replicated.class)) { |
| 218 continue; |
| 219 } |
| 220 @SuppressWarnings("unchecked") |
| 221 Iterator<Property> properties = ps.getPropertyIterator(); |
| 222 ·················· |
| 223 while (properties.hasNext()) { |
| 224 Property property = properties.next(); |
| 225 String cascade = property.getCascade(); |
| 226 ············ |
| 227 //skip check if there is no cascade. |
| 228 if (cascade==null || CascadeStyle.NONE.equals(CascadeStyle.getCascadeSty
le(cascade))) { |
| 229 continue; |
| 230 } |
| 231 Class returnType = property.getValue().getType().getReturnedClass(); |
| 232 if (returnType.isAnnotationPresent(Replicated.class)) { |
| 233 throw new InvalidMappingException("Illegal cascade from non-replicated
class("+ps.getClassName()+") to replicated class(property = "+property.getName(
)+", class="+returnType+")",psClass.toString()); |
| 234 } |
| 235 } |
| 236 } |
| 237 } |
| 238 |
192 /** | 239 /** |
193 * @return the Set of mapped classes that don't support top level saves | 240 * @return the Set of mapped classes that don't support top level saves |
194 */ | 241 */ |
195 @SuppressWarnings("unchecked") | 242 @SuppressWarnings("unchecked") |
196 private Set<Class<?>> determineClassesWithoutTopLevelSaveSupport(Configuration
config) { | 243 private Set<Class<?>> determineClassesWithoutTopLevelSaveSupport(Configuration
config) { |
197 Set<Class<?>> classesWithoutTopLevelSaveSupport = Sets.newHashSet(); | 244 Set<Class<?>> classesWithoutTopLevelSaveSupport = Sets.newHashSet(); |
198 for(Iterator<PersistentClass> pcIter = config.getClassMappings(); pcIter.has
Next(); ) { | 245 for(Iterator<PersistentClass> pcIter = config.getClassMappings(); pcIter.has
Next(); ) { |
199 PersistentClass pc = pcIter.next(); | 246 PersistentClass pc = pcIter.next(); |
200 for(Iterator<Property> propIter = pc.getPropertyIterator(); propIter.hasNe
xt(); ) { | 247 for(Iterator<Property> propIter = pc.getPropertyIterator(); propIter.hasNe
xt(); ) { |
201 if(doesNotSupportTopLevelSave(propIter.next())) { | 248 if(doesNotSupportTopLevelSave(propIter.next())) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 } | 291 } |
245 } | 292 } |
246 | 293 |
247 /** | 294 /** |
248 * Helper function that creates an actual SessionFactory. | 295 * Helper function that creates an actual SessionFactory. |
249 */ | 296 */ |
250 private SessionFactoryImplementor buildSessionFactory() { | 297 private SessionFactoryImplementor buildSessionFactory() { |
251 return (SessionFactoryImplementor) prototypeConfiguration.buildSessionFactor
y(); | 298 return (SessionFactoryImplementor) prototypeConfiguration.buildSessionFactor
y(); |
252 } | 299 } |
253 } | 300 } |
OLD | NEW |