org.hibernate.id.IdentifierGenerator Java Examples

The following examples show how to use org.hibernate.id.IdentifierGenerator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ForgotPasswordServlet.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
    * Generates the unique key used for the forgot password request
    *
    * @return a unique key
    * @throws HibernateException
    * @throws FileUtilException
    * @throws IOException
    */
   public static String generateUniqueKey() throws HibernateException {
Properties props = new Properties();

IdentifierGenerator uuidGen = new UUIDGenerator();
((Configurable) uuidGen).configure(StringType.INSTANCE, props, null);

return ((String) uuidGen.generate(null, null)).toLowerCase();
   }
 
Example #2
Source File: IdentifierProperty.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Construct a non-virtual identifier property.
 *
 * @param name The name of the property representing the identifier within
 * its owning entity.
 * @param node The node name to use for XML-based representation of this
 * property.
 * @param type The Hibernate Type for the identifier property.
 * @param embedded Is this an embedded identifier.
 * @param unsavedValue The value which, if found as the value on the identifier
 * property, represents new (i.e., un-saved) instances of the owning entity.
 * @param identifierGenerator The generator to use for id value generation.
 */
public IdentifierProperty(
		String name,
		String node,
		Type type,
		boolean embedded,
		IdentifierValue unsavedValue,
		IdentifierGenerator identifierGenerator) {
	super(name, node, type);
	this.virtual = false;
	this.embedded = embedded;
	this.hasIdentifierMapper = false;
	this.unsavedValue = unsavedValue;
	this.identifierGenerator = identifierGenerator;
	this.identifierAssignedByInsert = identifierGenerator instanceof PostInsertIdentifierGenerator;
}
 
Example #3
Source File: Component.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public IdentifierGenerator createIdentifierGenerator(
		IdentifierGeneratorFactory identifierGeneratorFactory,
		Dialect dialect,
		String defaultCatalog,
		String defaultSchema,
		RootClass rootClass) throws MappingException {
	if ( builtIdentifierGenerator == null ) {
		builtIdentifierGenerator = buildIdentifierGenerator(
				identifierGeneratorFactory,
				dialect,
				defaultCatalog,
				defaultSchema,
				rootClass
		);
	}
	return builtIdentifierGenerator;
}
 
Example #4
Source File: IdentifierGeneration.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Determine the name of the sequence (or table if this resolves to a physical table)
 * to use.
 *
 * @param params The params supplied in the generator config (plus some standard useful extras).
 * @return The sequence name
 */
static QualifiedName determineSequenceName(Properties params, ServiceRegistry serviceRegistry) {
	final String sequencePerEntitySuffix = ConfigurationHelper.getString( SequenceStyleGenerator.CONFIG_SEQUENCE_PER_ENTITY_SUFFIX, params, SequenceStyleGenerator.DEF_SEQUENCE_SUFFIX );

	String fallbackSequenceName = SequenceStyleGenerator.DEF_SEQUENCE_NAME;
	final Boolean preferGeneratorNameAsDefaultName = serviceRegistry.getService( ConfigurationService.class )
			.getSetting( Settings.PREFER_GENERATOR_NAME_AS_DEFAULT_SEQUENCE_NAME, StandardConverters.BOOLEAN, true );
	if ( preferGeneratorNameAsDefaultName ) {
		final String generatorName = params.getProperty( IdentifierGenerator.GENERATOR_NAME );
		if ( StringHelper.isNotEmpty( generatorName ) ) {
			fallbackSequenceName = generatorName;
		}
	}

	// JPA_ENTITY_NAME value honors <class ... entity-name="..."> (HBM) and @Entity#name (JPA) overrides.
	final String defaultSequenceName = ConfigurationHelper.getBoolean( SequenceStyleGenerator.CONFIG_PREFER_SEQUENCE_PER_ENTITY, params, false )
			? params.getProperty( SequenceStyleGenerator.JPA_ENTITY_NAME ) + sequencePerEntitySuffix
			: fallbackSequenceName;

	final String sequenceName = ConfigurationHelper.getString( SequenceStyleGenerator.SEQUENCE_PARAM, params, defaultSequenceName );
	if ( sequenceName.contains( "." ) ) {
		return QualifiedNameParser.INSTANCE.parse( sequenceName );
	}
	else {
		JdbcEnvironment jdbcEnvironment = serviceRegistry.getService( JdbcEnvironment.class );
		// todo : need to incorporate implicit catalog and schema names
		final Identifier catalog = jdbcEnvironment.getIdentifierHelper().toIdentifier(
				ConfigurationHelper.getString( CATALOG, params )
		);
		final Identifier schema =  jdbcEnvironment.getIdentifierHelper().toIdentifier(
				ConfigurationHelper.getString( SCHEMA, params )
		);
		return new QualifiedNameParser.NameParts(
				catalog,
				schema,
				jdbcEnvironment.getIdentifierHelper().toIdentifier( sequenceName )
		);
	}
}
 
Example #5
Source File: InFlightMetadataCollectorImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void handleIdentifierValueBinding(
		KeyValue identifierValueBinding,
		Dialect dialect,
		String defaultCatalog,
		String defaultSchema,
		RootClass entityBinding) {
	// todo : store this result (back into the entity or into the KeyValue, maybe?)
	// 		This process of instantiating the id-generator is called multiple times.
	//		It was done this way in the old code too, so no "regression" here; but
	//		it could be done better
	try {
		final IdentifierGenerator ig = identifierValueBinding.createIdentifierGenerator(
				getIdentifierGeneratorFactory(),
				dialect,
				defaultCatalog,
				defaultSchema,
				entityBinding
		);

		if ( ig instanceof ExportableProducer ) {
			( (ExportableProducer) ig ).registerExportables( getDatabase() );
		}
	}
	catch (MappingException e) {
		// ignore this for now.  The reasoning being "non-reflective" binding as needed
		// by tools.  We want to hold off requiring classes being present until we
		// try to build a SF.  Here, just building the Metadata, it is "ok" for an
		// exception to occur, the same exception will happen later as we build the SF.
		log.debugf( "Ignoring exception thrown when trying to build IdentifierGenerator as part of Metadata building", e );
	}
}
 
Example #6
Source File: FileUtil.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static String generateUniqueContentFolderID() {
	IdentifierGenerator uuidGen = new UUIDGenerator();
	((Configurable) uuidGen).configure(StringType.INSTANCE, new Properties(), null);

//	Serializable generate(SharedSessionContractImplementor session, Object object)
	
	// lowercase to resolve OS issues
	return ((String) uuidGen.generate(null, null)).toLowerCase();
    }
 
Example #7
Source File: DefaultIdentifierGeneratorFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public IdentifierGenerator createIdentifierGenerator(String strategy, Type type, Properties config) {
	try {
		Class clazz = getIdentifierGeneratorClass( strategy );
		IdentifierGenerator identifierGenerator = ( IdentifierGenerator ) clazz.newInstance();
		if ( identifierGenerator instanceof Configurable ) {
			( ( Configurable ) identifierGenerator ).configure( type, config, serviceRegistry );
		}
		return identifierGenerator;
	}
	catch ( Exception e ) {
		final String entityName = config.getProperty( IdentifierGenerator.ENTITY_NAME );
		throw new MappingException( String.format( "Could not instantiate id generator [entity-name=%s]", entityName ), e );
	}
}
 
Example #8
Source File: TableGenerator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determine the table name to use for the generator values.
 * <p/>
 * Called during {@link #configure configuration}.
 *
 * @see #getTableName()
 * @param params The params supplied in the generator config (plus some standard useful extras).
 * @param jdbcEnvironment The JDBC environment
 * @return The table name to use.
 */
@SuppressWarnings({"UnusedParameters", "WeakerAccess"})
protected QualifiedName determineGeneratorTableName(Properties params, JdbcEnvironment jdbcEnvironment, ServiceRegistry serviceRegistry) {

	String fallbackTableName = DEF_TABLE;

	final Boolean preferGeneratorNameAsDefaultName = serviceRegistry.getService( ConfigurationService.class )
			.getSetting( AvailableSettings.PREFER_GENERATOR_NAME_AS_DEFAULT_SEQUENCE_NAME, StandardConverters.BOOLEAN, true );
	if ( preferGeneratorNameAsDefaultName ) {
		final String generatorName = params.getProperty( IdentifierGenerator.GENERATOR_NAME );
		if ( StringHelper.isNotEmpty( generatorName ) ) {
			fallbackTableName = generatorName;
		}
	}


	String tableName = ConfigurationHelper.getString( TABLE_PARAM, params, fallbackTableName );

	if ( tableName.contains( "." ) ) {
		return QualifiedNameParser.INSTANCE.parse( tableName );
	}
	else {
		// todo : need to incorporate implicit catalog and schema names
		final Identifier catalog = jdbcEnvironment.getIdentifierHelper().toIdentifier(
				ConfigurationHelper.getString( CATALOG, params )
		);
		final Identifier schema = jdbcEnvironment.getIdentifierHelper().toIdentifier(
				ConfigurationHelper.getString( SCHEMA, params )
		);
		return new QualifiedNameParser.NameParts(
				catalog,
				schema,
				jdbcEnvironment.getIdentifierHelper().toIdentifier( tableName )
		);
	}
}
 
Example #9
Source File: PropertyFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates the attribute representation of the identifier for a given entity mapping.
 *
 * @param mappedEntity The mapping definition of the entity.
 * @param generator The identifier value generator to use for this identifier.
 *
 * @return The appropriate IdentifierProperty definition.
 */
public static IdentifierProperty buildIdentifierAttribute(
		PersistentClass mappedEntity,
		IdentifierGenerator generator) {
	String mappedUnsavedValue = mappedEntity.getIdentifier().getNullValue();
	Type type = mappedEntity.getIdentifier().getType();
	Property property = mappedEntity.getIdentifierProperty();

	IdentifierValue unsavedValue = UnsavedValueFactory.getUnsavedIdentifierValue(
			mappedUnsavedValue,
			getGetter( property ),
			type,
			getConstructor( mappedEntity )
	);

	if ( property == null ) {
		// this is a virtual id property...
		return new IdentifierProperty(
				type,
				mappedEntity.hasEmbeddedIdentifier(),
				mappedEntity.hasIdentifierMapper(),
				unsavedValue,
				generator
		);
	}
	else {
		return new IdentifierProperty(
				property.getName(),
				type,
				mappedEntity.hasEmbeddedIdentifier(),
				unsavedValue,
				generator
		);
	}
}
 
Example #10
Source File: IdentifierProperty.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a virtual IdentifierProperty.
 *
 * @param type The Hibernate Type for the identifier property.
 * @param embedded Is this an embedded identifier.
 * @param unsavedValue The value which, if found as the value on the identifier
 * property, represents new (i.e., un-saved) instances of the owning entity.
 * @param identifierGenerator The generator to use for id value generation.
 */
public IdentifierProperty(
		Type type,
		boolean embedded,
		boolean hasIdentifierMapper,
		IdentifierValue unsavedValue,
		IdentifierGenerator identifierGenerator) {
	super( null, type );
	this.virtual = true;
	this.embedded = embedded;
	this.hasIdentifierMapper = hasIdentifierMapper;
	this.unsavedValue = unsavedValue;
	this.identifierGenerator = identifierGenerator;
	this.identifierAssignedByInsert = identifierGenerator instanceof PostInsertIdentifierGenerator;
}
 
Example #11
Source File: IdentifierProperty.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a non-virtual identifier property.
 *
 * @param name The name of the property representing the identifier within
 * its owning entity.
 * @param node The node name to use for XML-based representation of this
 * property.
 * @param type The Hibernate Type for the identifier property.
 * @param embedded Is this an embedded identifier.
 * @param unsavedValue The value which, if found as the value on the identifier
 * property, represents new (i.e., un-saved) instances of the owning entity.
 * @param identifierGenerator The generator to use for id value generation.
 */
public IdentifierProperty(
		String name,
		Type type,
		boolean embedded,
		IdentifierValue unsavedValue,
		IdentifierGenerator identifierGenerator) {
	super( name, type );
	this.virtual = false;
	this.embedded = embedded;
	this.hasIdentifierMapper = false;
	this.unsavedValue = unsavedValue;
	this.identifierGenerator = identifierGenerator;
	this.identifierAssignedByInsert = identifierGenerator instanceof PostInsertIdentifierGenerator;
}
 
Example #12
Source File: IdentifierProperty.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Construct a virtual IdentifierProperty.
 *
 * @param type The Hibernate Type for the identifier property.
 * @param embedded Is this an embedded identifier.
 * @param unsavedValue The value which, if found as the value on the identifier
 * property, represents new (i.e., un-saved) instances of the owning entity.
 * @param identifierGenerator The generator to use for id value generation.
 */
public IdentifierProperty(
        Type type,
        boolean embedded,
		boolean hasIdentifierMapper,
		IdentifierValue unsavedValue,
        IdentifierGenerator identifierGenerator) {
	super(null, null, type);
	this.virtual = true;
	this.embedded = embedded;
	this.hasIdentifierMapper = hasIdentifierMapper;
	this.unsavedValue = unsavedValue;
	this.identifierGenerator = identifierGenerator;
	this.identifierAssignedByInsert = identifierGenerator instanceof PostInsertIdentifierGenerator;
}
 
Example #13
Source File: PropertyFactory.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Generates an IdentifierProperty representation of the for a given entity mapping.
 *
 * @param mappedEntity The mapping definition of the entity.
 * @param generator The identifier value generator to use for this identifier.
 * @return The appropriate IdentifierProperty definition.
 */
public static IdentifierProperty buildIdentifierProperty(PersistentClass mappedEntity, IdentifierGenerator generator) {

	String mappedUnsavedValue = mappedEntity.getIdentifier().getNullValue();
	Type type = mappedEntity.getIdentifier().getType();
	Property property = mappedEntity.getIdentifierProperty();
	
	IdentifierValue unsavedValue = UnsavedValueFactory.getUnsavedIdentifierValue(
			mappedUnsavedValue,
			getGetter( property ),
			type,
			getConstructor(mappedEntity)
		);

	if ( property == null ) {
		// this is a virtual id property...
		return new IdentifierProperty(
		        type,
				mappedEntity.hasEmbeddedIdentifier(),
				mappedEntity.hasIdentifierMapper(),
				unsavedValue,
				generator
			);
	}
	else {
		return new IdentifierProperty(
				property.getName(),
				property.getNodeName(),
				type,
				mappedEntity.hasEmbeddedIdentifier(),
				unsavedValue,
				generator
			);
	}
}
 
Example #14
Source File: IdentifierGeneration.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
static QualifiedName determineTableName(Properties params, ServiceRegistry serviceRegistry) {
	String fallbackTableName = TableGenerator.DEF_TABLE;
	final Boolean preferGeneratorNameAsDefaultName = serviceRegistry.getService( ConfigurationService.class )
			.getSetting( Settings.PREFER_GENERATOR_NAME_AS_DEFAULT_SEQUENCE_NAME, StandardConverters.BOOLEAN, true );
	if ( preferGeneratorNameAsDefaultName ) {
		final String generatorName = params.getProperty( IdentifierGenerator.GENERATOR_NAME );
		if ( StringHelper.isNotEmpty( generatorName ) ) {
			fallbackTableName = generatorName;
		}
	}

	String tableName = ConfigurationHelper.getString( TableGenerator.TABLE_PARAM, params, fallbackTableName );

	QualifiedNameParser.NameParts qualifiedTableName;
	if ( tableName.contains( "." ) ) {
		qualifiedTableName = QualifiedNameParser.INSTANCE.parse( tableName );
	}
	else {
		JdbcEnvironment jdbcEnvironment = serviceRegistry.getService( JdbcEnvironment.class );
		// todo : need to incorporate implicit catalog and schema names
		final Identifier catalog = jdbcEnvironment.getIdentifierHelper().toIdentifier(
				ConfigurationHelper.getString( CATALOG, params )
		);
		final Identifier schema = jdbcEnvironment.getIdentifierHelper().toIdentifier(
				ConfigurationHelper.getString( SCHEMA, params )
		);
		qualifiedTableName = new QualifiedNameParser.NameParts(
				catalog,
				schema,
				jdbcEnvironment.getIdentifierHelper().toIdentifier( tableName )
		);
	}
	return qualifiedTableName;
}
 
Example #15
Source File: ReactiveIdentifierGeneratorFactory.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static IdentifierGenerator augmentWithReactiveGenerator(IdentifierGenerator generator, Type type, Properties params, ServiceRegistryImplementor serviceRegistry) {
	ReactiveIdentifierGenerator<?> reactiveGenerator;
	if (generator instanceof SequenceStyleGenerator) {
		DatabaseStructure structure = ((SequenceStyleGenerator) generator).getDatabaseStructure();
		if (structure instanceof TableStructure) {
			reactiveGenerator = new TableReactiveIdentifierGenerator(true);
		}
		else if (structure instanceof SequenceStructure) {
			reactiveGenerator = new SequenceReactiveIdentifierGenerator();
		}
		else {
			throw new IllegalStateException("unknown structure type");
		}
	}
	else if (generator instanceof TableGenerator) {
		reactiveGenerator = new TableReactiveIdentifierGenerator(false);
	}
	else if (generator instanceof SequenceGenerator) {
		reactiveGenerator = new SequenceReactiveIdentifierGenerator();
	}
	else if (generator instanceof SelectGenerator) {
		//TODO: this is easy to fix!
		throw new HibernateException("SelectGenerator is not yet supported in Hibernate Reactive");
	}
	else {
		//nothing to do
		return generator;
	}

	((Configurable) reactiveGenerator).configure( type, params, serviceRegistry );

	return new ReactiveGeneratorWrapper<>( reactiveGenerator, generator );
}
 
Example #16
Source File: QuarkusMutableIdentifierGeneratorFactory.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public Class getIdentifierGeneratorClass(final String strategy) {
    Class<? extends IdentifierGenerator> aClass = typeCache.get(strategy);
    if (aClass != null)
        return aClass;
    aClass = original.getIdentifierGeneratorClass(strategy);
    storeCache(strategy, aClass);
    return aClass;
}
 
Example #17
Source File: QuarkusMutableIdentifierGeneratorFactory.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private void storeCache(final String strategy, final Class<? extends IdentifierGenerator> generatorClass) {
    if (strategy == null || generatorClass == null)
        return;
    final String className = generatorClass.getName();
    //Store for access both via short and long names:
    typeCache.put(strategy, generatorClass);
    if (!className.equals(strategy)) {
        typeCache.put(className, generatorClass);
    }
}
 
Example #18
Source File: AbstractEntityPersister.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public IdentifierGenerator getIdentifierGenerator() throws HibernateException {
	return entityMetamodel.getIdentifierProperty().getIdentifierGenerator();
}
 
Example #19
Source File: SessionFactoryWrapper.java    From lemon with Apache License 2.0 4 votes vote down vote up
public IdentifierGenerator getIdentifierGenerator(String rootEntityName) {
    return sessionFactoryImplementor.getIdentifierGenerator(rootEntityName);
}
 
Example #20
Source File: ReactiveGeneratorWrapper.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
public ReactiveGeneratorWrapper(ReactiveIdentifierGenerator<T> reactiveGenerator, IdentifierGenerator generator) {
	this.reactiveGenerator = reactiveGenerator;
	this.generator = generator;
}
 
Example #21
Source File: BulkManipulationTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testInsertWithGeneratedTimestampVersion() {
	// Make sure the env supports bulk inserts with generated ids...
	EntityPersister persister = sfi().getEntityPersister( TimestampVersioned.class.getName() );
	IdentifierGenerator generator = persister.getIdentifierGenerator();
	if ( !HqlSqlWalker.supportsIdGenWithBulkInsertion( generator ) ) {
		return;
	}

	Session s = openSession();
	Transaction t = s.beginTransaction();

	TimestampVersioned entity = new TimestampVersioned( "int-vers" );
	s.save( entity );
	s.createQuery( "select id, name, version from TimestampVersioned" ).list();
	t.commit();
	s.close();

	Long initialId = entity.getId();
	//Date initialVersion = entity.getVersion();

	s = openSession();
	t = s.beginTransaction();
	int count = s.createQuery( "insert into TimestampVersioned ( name ) select name from TimestampVersioned" ).executeUpdate();
	t.commit();
	s.close();

	assertEquals( "unexpected insertion count", 1, count );

	s = openSession();
	t = s.beginTransaction();
	TimestampVersioned created = ( TimestampVersioned ) s.createQuery( "from TimestampVersioned where id <> :initialId" )
			.setLong( "initialId", initialId.longValue() )
			.uniqueResult();
	t.commit();
	s.close();

	assertNotNull( created.getVersion() );
	//assertEquals( "version was not seeded", initialVersion, created.getVersion() );

	s = openSession();
	t = s.beginTransaction();
	s.createQuery( "delete TimestampVersioned" ).executeUpdate();
	t.commit();
	s.close();
}
 
Example #22
Source File: KeyValue.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public IdentifierGenerator createIdentifierGenerator(
Dialect dialect, 
String defaultCatalog, 
String defaultSchema, 
RootClass rootClass) throws MappingException;
 
Example #23
Source File: SimpleValue.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public IdentifierGenerator createIdentifierGenerator(
		Dialect dialect, 
		String defaultCatalog, 
		String defaultSchema, 
		RootClass rootClass) 
throws MappingException {
	
	Properties params = new Properties();
	
	//if the hibernate-mapping did not specify a schema/catalog, use the defaults
	//specified by properties - but note that if the schema/catalog were specified
	//in hibernate-mapping, or as params, they will already be initialized and
	//will override the values set here (they are in identifierGeneratorProperties)
	if ( defaultSchema!=null ) {
		params.setProperty(PersistentIdentifierGenerator.SCHEMA, defaultSchema);
	}
	if ( defaultCatalog!=null ) {
		params.setProperty(PersistentIdentifierGenerator.CATALOG, defaultCatalog);
	}
	
	//pass the entity-name, if not a collection-id
	if (rootClass!=null) {
		params.setProperty( IdentifierGenerator.ENTITY_NAME, rootClass.getEntityName() );
	}
	
	//init the table here instead of earlier, so that we can get a quoted table name
	//TODO: would it be better to simply pass the qualified table name, instead of
	//      splitting it up into schema/catalog/table names
	String tableName = getTable().getQuotedName(dialect);
	params.setProperty( PersistentIdentifierGenerator.TABLE, tableName );
	
	//pass the column name (a generated id almost always has a single column)
	String columnName = ( (Column) getColumnIterator().next() ).getQuotedName(dialect);
	params.setProperty( PersistentIdentifierGenerator.PK, columnName );
	
	if (rootClass!=null) {
		StringBuffer tables = new StringBuffer();
		Iterator iter = rootClass.getIdentityTables().iterator();
		while ( iter.hasNext() ) {
			Table table= (Table) iter.next();
			tables.append( table.getQuotedName(dialect) );
			if ( iter.hasNext() ) tables.append(", ");
		}
		params.setProperty( PersistentIdentifierGenerator.TABLES, tables.toString() );
	}
	else {
		params.setProperty( PersistentIdentifierGenerator.TABLES, tableName );
	}

	if (identifierGeneratorProperties!=null) {
		params.putAll(identifierGeneratorProperties);
	}
	
	return IdentifierGeneratorFactory.create(
			identifierGeneratorStrategy,
			getType(),
			params,
			dialect
		);
	
}
 
Example #24
Source File: SessionFactoryImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public IdentifierGenerator getIdentifierGenerator(String rootEntityName) {
	return (IdentifierGenerator) identifierGenerators.get(rootEntityName);
}
 
Example #25
Source File: BulkManipulationTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testInsertWithGeneratedVersionAndId() {
	// Make sure the env supports bulk inserts with generated ids...
	EntityPersister persister = sfi().getEntityPersister( IntegerVersioned.class.getName() );
	IdentifierGenerator generator = persister.getIdentifierGenerator();
	if ( !HqlSqlWalker.supportsIdGenWithBulkInsertion( generator ) ) {
		return;
	}

	Session s = openSession();
	Transaction t = s.beginTransaction();

	IntegerVersioned entity = new IntegerVersioned( "int-vers" );
	s.save( entity );
	s.createQuery( "select id, name, version from IntegerVersioned" ).list();
	t.commit();
	s.close();

	Long initialId = entity.getId();
	int initialVersion = entity.getVersion();

	s = openSession();
	t = s.beginTransaction();
	int count = s.createQuery( "insert into IntegerVersioned ( name ) select name from IntegerVersioned" ).executeUpdate();
	t.commit();
	s.close();

	assertEquals( "unexpected insertion count", 1, count );

	s = openSession();
	t = s.beginTransaction();
	IntegerVersioned created = ( IntegerVersioned ) s.createQuery( "from IntegerVersioned where id <> :initialId" )
			.setLong( "initialId", initialId.longValue() )
			.uniqueResult();
	t.commit();
	s.close();

	assertEquals( "version was not seeded", initialVersion, created.getVersion() );

	s = openSession();
	t = s.beginTransaction();
	s.createQuery( "delete IntegerVersioned" ).executeUpdate();
	t.commit();
	s.close();
}
 
Example #26
Source File: AbstractCollectionPersister.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public IdentifierGenerator getIdentifierGenerator() {
	return identifierGenerator;
}
 
Example #27
Source File: BulkManipulationTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testInsertWithGeneratedId() {
	// Make sure the env supports bulk inserts with generated ids...
	EntityPersister persister = sfi().getEntityPersister( PettingZoo.class.getName() );
	IdentifierGenerator generator = persister.getIdentifierGenerator();
	if ( !HqlSqlWalker.supportsIdGenWithBulkInsertion( generator ) ) {
		return;
	}

	// create a Zoo
	Zoo zoo = new Zoo();
	zoo.setName( "zoo" );

	Session s = openSession();
	Transaction t = s.beginTransaction();
	s.save( zoo );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	int count = s.createQuery( "insert into PettingZoo (name) select name from Zoo" ).executeUpdate();
	t.commit();
	s.close();

	assertEquals( "unexpected insertion count", 1, count );

	s = openSession();
	t = s.beginTransaction();
	PettingZoo pz = ( PettingZoo ) s.createQuery( "from PettingZoo" ).uniqueResult();
	t.commit();
	s.close();

	assertEquals( zoo.getName(), pz.getName() );
	assertTrue( zoo.getId() != pz.getId() );

	s = openSession();
	t = s.beginTransaction();
	s.createQuery( "delete Zoo" ).executeUpdate();
	t.commit();
	s.close();
}
 
Example #28
Source File: CustomPersister.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * @see EntityPersister#getIdentifierGenerator()
 */
public IdentifierGenerator getIdentifierGenerator()
throws HibernateException {
	return GENERATOR;
}
 
Example #29
Source File: IdentifierProperty.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public IdentifierGenerator getIdentifierGenerator() {
	return identifierGenerator;
}
 
Example #30
Source File: HqlSqlWalker.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static boolean supportsIdGenWithBulkInsertion(IdentifierGenerator generator) {
	return SequenceGenerator.class.isAssignableFrom( generator.getClass() )
	        || PostInsertIdentifierGenerator.class.isAssignableFrom( generator.getClass() );
}