From 81fd2088983bfb96f3719d4f0cbbb3e1e416f11e Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Tue, 9 Feb 2010 19:54:39 +0000 Subject: [PATCH] HHH-4894 - Process composite-id sub-generators PersistentIdentifierGenerator contract(s) git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18748 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../idclassgeneratedvalue/Multiple.java | 6 ++- .../java/org/hibernate/cfg/Configuration.java | 4 ++ ...ompositeNestedGeneratedValueGenerator.java | 21 ++++++++- .../id/IdentifierGeneratorAggregator.java | 44 +++++++++++++++++++ .../java/org/hibernate/mapping/Component.java | 15 +++---- 5 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 core/src/main/java/org/hibernate/id/IdentifierGeneratorAggregator.java diff --git a/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/Multiple.java b/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/Multiple.java index 4e3d194e8c..e8dbecfc36 100644 --- a/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/Multiple.java +++ b/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/Multiple.java @@ -27,8 +27,10 @@ import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.IdClass; +import javax.persistence.SequenceGenerator; import org.hibernate.annotations.GenericGenerator; @@ -48,8 +50,8 @@ public class Multiple implements Serializable private Long id1; @Id - @GenericGenerator(name = "increment2", strategy = "increment") - @GeneratedValue(generator = "increment2") + @GeneratedValue(generator = "MULTIPLE_SEQ", strategy = GenerationType.SEQUENCE) + @SequenceGenerator( name = "MULTIPLE_SEQ", sequenceName = "MULTIPLE_SEQ") private Long id2; @Id diff --git a/core/src/main/java/org/hibernate/cfg/Configuration.java b/core/src/main/java/org/hibernate/cfg/Configuration.java index a022cbc019..a77c8d5f19 100644 --- a/core/src/main/java/org/hibernate/cfg/Configuration.java +++ b/core/src/main/java/org/hibernate/cfg/Configuration.java @@ -67,6 +67,7 @@ import org.hibernate.MappingNotFoundException; import org.hibernate.SessionFactory; import org.hibernate.SessionFactoryObserver; import org.hibernate.DuplicateMappingException; +import org.hibernate.id.IdentifierGeneratorAggregator; import org.hibernate.tuple.entity.EntityTuplizerFactory; import org.hibernate.dialect.Dialect; import org.hibernate.dialect.MySQLDialect; @@ -800,6 +801,9 @@ public class Configuration implements Serializable { if ( ig instanceof PersistentIdentifierGenerator ) { generators.put( ( (PersistentIdentifierGenerator) ig ).generatorKey(), ig ); } + else if ( ig instanceof IdentifierGeneratorAggregator ) { + ( (IdentifierGeneratorAggregator) ig ).registerPersistentGenerators( generators ); + } } } diff --git a/core/src/main/java/org/hibernate/id/CompositeNestedGeneratedValueGenerator.java b/core/src/main/java/org/hibernate/id/CompositeNestedGeneratedValueGenerator.java index 5c728e4c94..a39cde5879 100644 --- a/core/src/main/java/org/hibernate/id/CompositeNestedGeneratedValueGenerator.java +++ b/core/src/main/java/org/hibernate/id/CompositeNestedGeneratedValueGenerator.java @@ -27,6 +27,7 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Map; import org.hibernate.HibernateException; import org.hibernate.engine.SessionImplementor; @@ -61,7 +62,7 @@ import org.hibernate.engine.SessionImplementor; * * @author Steve Ebersole */ -public class CompositeNestedGeneratedValueGenerator implements IdentifierGenerator, Serializable { +public class CompositeNestedGeneratedValueGenerator implements IdentifierGenerator, Serializable, IdentifierGeneratorAggregator { /** * Contract for declaring how to locate the context for sub-value injection. */ @@ -91,6 +92,14 @@ public class CompositeNestedGeneratedValueGenerator implements IdentifierGenerat * @param injectionContext The context into which the generated value can be injected */ public void execute(SessionImplementor session, Object incomingObject, Object injectionContext); + + /** + * Register any sub generators which implement {@link PersistentIdentifierGenerator} by their + * {@link PersistentIdentifierGenerator#generatorKey generatorKey}. + * + * @param generatorMap The map of generators. + */ + public void registerPersistentGenerators(Map generatorMap); } private final GenerationContextLocator generationContextLocator; @@ -116,4 +125,14 @@ public class CompositeNestedGeneratedValueGenerator implements IdentifierGenerat return context; } + /** + * {@inheritDoc} + */ + public void registerPersistentGenerators(Map generatorMap) { + final Iterator itr = generationPlans.iterator(); + while ( itr.hasNext() ) { + final GenerationPlan plan = (GenerationPlan) itr.next(); + plan.registerPersistentGenerators( generatorMap ); + } + } } diff --git a/core/src/main/java/org/hibernate/id/IdentifierGeneratorAggregator.java b/core/src/main/java/org/hibernate/id/IdentifierGeneratorAggregator.java new file mode 100644 index 0000000000..c4eba75830 --- /dev/null +++ b/core/src/main/java/org/hibernate/id/IdentifierGeneratorAggregator.java @@ -0,0 +1,44 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2010, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.id; + +import java.util.Map; + +/** + * Identifies {@link IdentifierGenerator generators} which potentially aggregate other + * {@link PersistentIdentifierGenerator} generators. + *

+ * Initially this is limited to {@link CompositeNestedGeneratedValueGenerator} + * + * @author Steve Ebersole + */ +public interface IdentifierGeneratorAggregator { + /** + * Register any sub generators which implement {@link PersistentIdentifierGenerator} by their + * {@link PersistentIdentifierGenerator#generatorKey generatorKey}. + * + * @param generatorMap The map of generators. + */ + public void registerPersistentGenerators(Map generatorMap); +} diff --git a/core/src/main/java/org/hibernate/mapping/Component.java b/core/src/main/java/org/hibernate/mapping/Component.java index b36862c57a..3c6725c521 100644 --- a/core/src/main/java/org/hibernate/mapping/Component.java +++ b/core/src/main/java/org/hibernate/mapping/Component.java @@ -24,26 +24,19 @@ package org.hibernate.mapping; import java.io.Serializable; -import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import org.hibernate.EntityMode; -import org.hibernate.HibernateException; import org.hibernate.MappingException; import org.hibernate.dialect.Dialect; -import org.hibernate.engine.EntityEntry; import org.hibernate.engine.SessionImplementor; import org.hibernate.id.CompositeNestedGeneratedValueGenerator; import org.hibernate.id.IdentifierGenerator; +import org.hibernate.id.PersistentIdentifierGenerator; import org.hibernate.id.factory.IdentifierGeneratorFactory; -import org.hibernate.property.Getter; -import org.hibernate.property.PropertyAccessor; import org.hibernate.property.Setter; import org.hibernate.tuple.component.ComponentMetamodel; import org.hibernate.type.ComponentType; @@ -444,6 +437,12 @@ public class Component extends SimpleValue implements MetaAttributable { final Object generatedValue = subGenerator.generate( session, incomingObject ); injector.set( injectionContext, generatedValue, session.getFactory() ); } + + public void registerPersistentGenerators(Map generatorMap) { + if ( PersistentIdentifierGenerator.class.isInstance( subGenerator ) ) { + generatorMap.put( ( (PersistentIdentifierGenerator) subGenerator ).generatorKey(), subGenerator ); + } + } } }