📄 onetomanypersister.java
字号:
//$Id: OneToManyPersister.java,v 1.1.2.3 2003/11/16 04:28:29 oneovthafew Exp $
package net.sf.hibernate.collection;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.JDBCException;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.cache.CacheException;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.engine.SessionFactoryImplementor;
import net.sf.hibernate.engine.SessionImplementor;
import net.sf.hibernate.impl.MessageHelper;
import net.sf.hibernate.loader.BatchingCollectionInitializer;
import net.sf.hibernate.loader.CollectionInitializer;
import net.sf.hibernate.loader.Loader;
import net.sf.hibernate.loader.OneToManyLoader;
import net.sf.hibernate.mapping.Collection;
import net.sf.hibernate.sql.Update;
import net.sf.hibernate.util.ArrayHelper;
import net.sf.hibernate.util.StringHelper;
/**
* Collection persister for one-to-many associations.
* @author Gavin King
*/
public class OneToManyPersister extends AbstractCollectionPersister {
public OneToManyPersister(
Collection collection,
Configuration cfg,
SessionFactoryImplementor factory)
throws MappingException, CacheException {
super(collection, cfg, factory);
}
protected String generateDeleteString() {
Update update = new Update()
.setTableName(qualifiedTableName)
.addColumns(keyColumnNames, "null")
.setPrimaryKeyColumnNames(keyColumnNames);
if (hasIndex) update.addColumns(indexColumnNames, "null");
if (hasWhere) update.setWhere(sqlWhereString);
return update.toStatementString();
}
protected String generateInsertRowString() {
Update update = new Update()
.setTableName(qualifiedTableName)
.addColumns(keyColumnNames);
if (hasIndex) update.addColumns(indexColumnNames);
//identifier collections not supported for 1-to-many
return update.setPrimaryKeyColumnNames(elementColumnNames)
.toStatementString();
}
protected String generateUpdateRowString() {
return null;
}
protected String generateDeleteRowString() {
final String[] pkColumns;
if (hasIdentifier) {
pkColumns = rowSelectColumnNames;
}
else {
pkColumns = ArrayHelper.join(keyColumnNames, rowSelectColumnNames);
}
Update update = new Update()
.setTableName(qualifiedTableName)
.addColumns(keyColumnNames, "null");
if (hasIndex) update.addColumns(indexColumnNames, "null");
return update.setPrimaryKeyColumnNames(pkColumns)
.toStatementString();
}
public boolean consumesAlias() {
return true;
}
public boolean isOneToMany() {
return true;
}
public boolean isManyToMany() {
return false;
}
protected int doUpdateRows(Serializable id, PersistentCollection collection, SessionImplementor session)
throws HibernateException {
try {
int count=0;
try {
PreparedStatement rmvst = null;
int i=0;
Iterator entries = collection.entries();
while ( entries.hasNext() ) {
Object entry = entries.next();
if ( collection.needsUpdating(entry, i, elementType) ) { // will still be issued when it used to be null
if (rmvst==null) rmvst = session.getBatcher().prepareBatchStatement( getSQLDeleteRowString() );
writeKey(rmvst, id, false, session);
writeIndex(rmvst, collection.getIndex(entry, i), false, session);
session.getBatcher().addToBatch(-1);
count++;
}
i++;
}
}
catch (SQLException sqle) {
session.getBatcher().abortBatch(sqle);
throw sqle;
}
// finish all the "removes" first to take care of possible unique constraints
// and so that we can take advantage of batching
try {
PreparedStatement insst = null;
int i=0;
Iterator entries = collection.entries();
while ( entries.hasNext() ) {
Object entry = entries.next();
if ( collection.needsUpdating(entry, i, elementType) ) {
if (insst==null) insst = session.getBatcher().prepareBatchStatement( getSQLInsertRowString() );
writeKey(insst, id, false, session);
collection.writeTo(insst, this, entry, i, false);
session.getBatcher().addToBatch(1);
count++;
}
i++;
}
}
catch (SQLException sqle) {
session.getBatcher().abortBatch(sqle);
throw sqle;
}
return count;
}
catch (SQLException sqle) {
throw new JDBCException("could not update collection rows: " + MessageHelper.infoString(this, id), sqle );
}
}
public String joinSelectFragment(String alias, String suffix) {
StringBuffer buf = new StringBuffer();
buf.append( selectClauseFragment(alias) );
if ( isOneToMany() ) {
buf.append(StringHelper.COMMA_SPACE)
.append( elementPersister.identifierSelectFragment(alias, suffix) )
.append( elementPersister.propertySelectFragment(alias, suffix) );
}
return buf.toString();
}
protected CollectionInitializer createCollectionInitializer(SessionFactoryImplementor factory) throws MappingException {
Loader nonbatchLoader = new OneToManyLoader(this, factory);
if (batchSize>1) {
Loader batchLoader = new OneToManyLoader(this, batchSize, factory);
int smallBatchSize = (int) Math.round( Math.sqrt(batchSize) );
Loader smallBatchLoader = new OneToManyLoader(this, smallBatchSize, factory);
// the strategy for choosing batch or single load:
return new BatchingCollectionInitializer(this, batchSize, batchLoader, smallBatchSize, smallBatchLoader, nonbatchLoader);
}
else {
// don't do batch loading
return (CollectionInitializer) nonbatchLoader;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -