⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selectgenerator.java

📁 hibernate-3.1.3-all-src.zip 面向对象的访问数据库工具
💻 JAVA
字号:
//$Id: SelectGenerator.java 6929 2005-05-27 03:54:08Z oneovthafew $
package org.hibernate.id;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.type.Type;

/**
 * An IdentityGenerator that selects the inserted row, to determine
 * an identifier value assigned by the database. The correct row is
 * located using a unique key.
 * <br><br>
 * One mapping parameter is required: key.
 *
 * @author Gavin King
 */
public class SelectGenerator extends AbstractPostInsertGenerator implements Configurable {
	
	private String uniqueKeyPropertyName;
	private Type idType;
	private String entityName;

	public void configure(Type type, Properties params, Dialect d) throws MappingException {
		uniqueKeyPropertyName = params.getProperty("key");
		entityName = params.getProperty(ENTITY_NAME);
		this.idType = type;
	}
	
	protected String getSQL(PostInsertIdentityPersister persister) {
		return persister.getSelectByUniqueKeyString(uniqueKeyPropertyName);
	}

	protected void bindParameters(SessionImplementor session, PreparedStatement ps, Object object, PostInsertIdentityPersister persister) 
	throws SQLException {
		Type uniqueKeyPropertyType = session.getFactory()
				.getClassMetadata(entityName)
				.getPropertyType(uniqueKeyPropertyName);
		Object uniqueKeyValue = persister.getPropertyValue( object, uniqueKeyPropertyName, session.getEntityMode() );
		uniqueKeyPropertyType.nullSafeSet( ps, uniqueKeyValue, 1, session );
	}

	protected Serializable getResult(SessionImplementor session, ResultSet rs, Object object, PostInsertIdentityPersister persister) 
	throws SQLException {
		if ( !rs.next() ) {
			throw new IdentifierGenerationException( "the inserted row could not be located by the unique key: " + uniqueKeyPropertyName );
		}
		return (Serializable) idType.nullSafeGet(rs, persister.getRootTableKeyColumnNames(), session, object);
	}
}






⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -