All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.id.Assigned Maven / Gradle / Ivy

There is a newer version: 6.6.2.Final
Show newest version
/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or .
 */
package org.hibernate.id;

import java.util.Properties;

import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.factory.spi.StandardGenerator;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.Type;

/**
 * An {@link IdentifierGenerator} that returns the current identifier assigned
 * to an instance.
 *
 * @author Gavin King
 *
 * @implNote This also implements the {@code assigned} generation type in {@code hbm.xml} mappings.
 */
public class Assigned implements IdentifierGenerator, StandardGenerator {
	private String entityName;

	public Object generate(SharedSessionContractImplementor session, Object obj) throws HibernateException {
		//TODO: cache the persister, this shows up in yourkit
		final Object id = session.getEntityPersister( entityName, obj ).getIdentifier( obj, session );
		if ( id == null ) {
			throw new IdentifierGenerationException(
					"ids for this class must be manually assigned before calling save(): " + entityName
			);
		}
		return id;
	}

	@Override
	public void configure(Type type, Properties parameters, ServiceRegistry serviceRegistry) throws MappingException {
		entityName = parameters.getProperty( ENTITY_NAME );
		if ( entityName == null ) {
			throw new MappingException("no entity name");
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy