📄 abstractcollectionpersister.java
字号:
}
}
//TODO: copy/paste from recreate()
int loc = writeKey( st, id, offset, session );
if ( hasIdentifier ) {
loc = writeIdentifier( st, collection.getIdentifier(entry, i), loc, session );
}
if ( hasIndex /*&& !indexIsFormula*/ ) {
loc = writeIndex( st, collection.getIndex(entry, i, this), loc, session );
}
//if ( !elementIsFormula ) {
loc = writeElement(st, collection.getElement(entry), loc, session );
//}
session.getBatcher().addToBatch( 1 );
collection.afterRowInsert( this, entry, i );
count++;
}
i++;
}
if ( log.isDebugEnabled() ) log.debug( "done inserting rows: " + count + " inserted" );
}
catch ( SQLException sqle ) {
session.getBatcher().abortBatch( sqle );
throw sqle;
}
}
catch ( SQLException sqle ) {
throw JDBCExceptionHelper.convert(
sqlExceptionConverter,
sqle,
"could not insert collection rows: " +
MessageHelper.collectionInfoString( this, id, getFactory() ),
getSQLInsertRowString()
);
}
}
}
public String getRole() {
return role;
}
public String getOwnerEntityName() {
return entityName;
}
public EntityPersister getOwnerEntityPersister() {
return ownerPersister;
}
public IdentifierGenerator getIdentifierGenerator() {
return identifierGenerator;
}
public Type getIdentifierType() {
return identifierType;
}
public boolean hasOrphanDelete() {
return hasOrphanDelete;
}
public Type toType(String propertyName) throws QueryException {
if ( "index".equals( propertyName ) ) return indexType;
return elementPropertyMapping.toType( propertyName );
}
public abstract boolean isManyToMany();
public String getManyToManyFilterFragment(String alias, Map enabledFilters) {
StringBuffer buffer = new StringBuffer();
manyToManyFilterHelper.render( buffer, alias, enabledFilters );
if ( manyToManyWhereString != null ) {
buffer.append( " and " )
.append( StringHelper.replace( manyToManyWhereTemplate, Template.TEMPLATE, alias ) );
}
return buffer.toString();
}
public String[] toColumns(String alias, String propertyName)
throws QueryException {
if ( "index".equals( propertyName ) ) {
if ( isManyToMany() ) {
throw new QueryException( "index() function not supported for many-to-many association" );
}
return StringHelper.qualify( alias, indexColumnNames );
}
return elementPropertyMapping.toColumns( alias, propertyName );
}
public String[] toColumns(String propertyName)
throws QueryException {
if ( "index".equals( propertyName ) ) {
if ( isManyToMany() ) {
throw new QueryException( "index() function not supported for many-to-many association" );
}
return indexColumnNames;
}
return elementPropertyMapping.toColumns( propertyName );
}
public Type getType() {
return elementPropertyMapping.getType(); //==elementType ??
}
public String getName() {
return getRole();
}
public EntityPersister getElementPersister() {
if ( elementPersister == null ) throw new AssertionFailure( "not an association" );
return ( Loadable ) elementPersister;
}
public boolean isCollection() {
return true;
}
public Serializable[] getCollectionSpaces() {
return spaces;
}
protected abstract String generateDeleteString();
protected abstract String generateDeleteRowString();
protected abstract String generateUpdateRowString();
protected abstract String generateInsertRowString();
public void updateRows(PersistentCollection collection, Serializable id, SessionImplementor session)
throws HibernateException {
if ( !isInverse && collection.isRowUpdatePossible() ) {
if ( log.isDebugEnabled() ) {
log.debug( "Updating rows of collection: " + role + "#" + id );
}
//update all the modified entries
int count = doUpdateRows( id, collection, session );
if ( log.isDebugEnabled() ) {
log.debug( "done updating rows: " + count + " updated" );
}
}
}
protected abstract int doUpdateRows(Serializable key, PersistentCollection collection, SessionImplementor session)
throws HibernateException;
public CollectionMetadata getCollectionMetadata() {
return this;
}
public SessionFactoryImplementor getFactory() {
return factory;
}
protected String filterFragment(String alias) throws MappingException {
return hasWhere() ? " and " + getSQLWhereString( alias ) : "";
}
public String filterFragment(String alias, Map enabledFilters) throws MappingException {
StringBuffer sessionFilterFragment = new StringBuffer();
filterHelper.render( sessionFilterFragment, alias, enabledFilters );
return sessionFilterFragment.append( filterFragment( alias ) ).toString();
}
public String oneToManyFilterFragment(String alias) throws MappingException {
return "";
}
protected boolean isInsertCallable() {
return insertCallable;
}
protected boolean isUpdateCallable() {
return updateCallable;
}
protected boolean isDeleteCallable() {
return deleteCallable;
}
protected boolean isDeleteAllCallable() {
return deleteAllCallable;
}
public String toString() {
return StringHelper.unqualify( getClass().getName() ) + '(' + role + ')';
}
public boolean isVersioned() {
return isVersioned && getOwnerEntityPersister().isVersioned();
}
public String getNodeName() {
return nodeName;
}
public String getElementNodeName() {
return elementNodeName;
}
public String getIndexNodeName() {
return indexNodeName;
}
protected SQLExceptionConverter getSQLExceptionConverter() {
return sqlExceptionConverter;
}
public CacheEntryStructure getCacheEntryStructure() {
return cacheEntryStructure;
}
public boolean isAffectedByEnabledFilters(SessionImplementor session) {
return filterHelper.isAffectedBy( session.getEnabledFilters() ) ||
( isManyToMany() && manyToManyFilterHelper.isAffectedBy( session.getEnabledFilters() ) );
}
public boolean isSubselectLoadable() {
return subselectLoadable;
}
public boolean isMutable() {
return isMutable;
}
public String[] getCollectionPropertyColumnAliases(String propertyName, String suffix) {
String rawAliases[] = (String[]) collectionPropertyColumnAliases.get(propertyName);
if(rawAliases==null) return null;
String result[] = new String[rawAliases.length];
for ( int i=0; i<rawAliases.length; i++ ) {
result[i] = new Alias(suffix).toUnquotedAliasString( rawAliases[i] );
}
return result;
}
//TODO: formulas ?
public void initCollectionPropertyMap() {
initCollectionPropertyMap( "key", keyType, keyColumnAliases, keyColumnNames );
initCollectionPropertyMap( "element", elementType, elementColumnAliases, elementColumnNames );
if (hasIndex) {
initCollectionPropertyMap( "index", indexType, indexColumnAliases, indexColumnNames );
}
if (hasIdentifier) {
initCollectionPropertyMap(
"id",
identifierType,
new String[] { identifierColumnAlias },
new String[] { identifierColumnName }
);
}
}
private void initCollectionPropertyMap(String aliasName, Type type, String[] columnAliases, String[] columnNames) {
collectionPropertyColumnAliases.put(aliasName, columnAliases);
collectionPropertyColumnNames.put(aliasName, columnNames);
if( type.isComponentType() ) {
AbstractComponentType ct = (AbstractComponentType) type;
String[] propertyNames = ct.getPropertyNames();
for (int i = 0; i < propertyNames.length; i++) {
String name = propertyNames[i];
collectionPropertyColumnAliases.put( aliasName + "." + name, columnAliases[i] );
collectionPropertyColumnAliases.put( aliasName + "." + name, columnNames[i] );
}
}
}
public int getSize(Serializable key, SessionImplementor session) {
try {
PreparedStatement st = session.getBatcher().prepareSelectStatement(sqlSelectSizeString);
try {
getKeyType().nullSafeSet(st, key, 1, session);
ResultSet rs = st.executeQuery();
try {
return rs.next() ? rs.getInt(1) - baseIndex : 0;
}
finally {
rs.close();
}
}
finally {
session.getBatcher().closeStatement( st );
}
}
catch (SQLException sqle) {
throw JDBCExceptionHelper.convert(
getFactory().getSQLExceptionConverter(),
sqle,
"could not retrieve collection size: " +
MessageHelper.collectionInfoString( this, key, getFactory() ),
sqlSelectSizeString
);
}
}
public boolean indexExists(Serializable key, Object index, SessionImplementor session) {
return exists(key, incrementIndexByBase(index), getIndexType(), sqlDetectRowByIndexString, session);
}
public boolean elementExists(Serializable key, Object element, SessionImplementor session) {
return exists(key, element, getElementType(), sqlDetectRowByElementString, session);
}
private boolean exists(Serializable key, Object indexOrElement, Type indexOrElementType, String sql, SessionImplementor session) {
try {
PreparedStatement st = session.getBatcher().prepareSelectStatement(sql);
try {
getKeyType().nullSafeSet(st, key, 1, session);
indexOrElementType.nullSafeSet( st, indexOrElement, keyColumnNames.length + 1, session );
ResultSet rs = st.executeQuery();
try {
return rs.next();
}
finally {
rs.close();
}
}
finally {
session.getBatcher().closeStatement( st );
}
}
catch (SQLException sqle) {
throw JDBCExceptionHelper.convert(
getFactory().getSQLExceptionConverter(),
sqle,
"could not check row existence: " +
MessageHelper.collectionInfoString( this, key, getFactory() ),
sqlSelectSizeString
);
}
}
public Object getElementByIndex(Serializable key, Object index, SessionImplementor session, Object owner) {
try {
PreparedStatement st = session.getBatcher().prepareSelectStatement(sqlSelectRowByIndexString);
try {
getKeyType().nullSafeSet(st, key, 1, session);
getIndexType().nullSafeSet( st, incrementIndexByBase(index), keyColumnNames.length + 1, session );
ResultSet rs = st.executeQuery();
try {
if ( rs.next() ) {
return getElementType().nullSafeGet(rs, elementColumnAliases, session, owner);
}
else {
return null;
}
}
finally {
rs.close();
}
}
finally {
session.getBatcher().closeStatement( st );
}
}
catch (SQLException sqle) {
throw JDBCExceptionHelper.convert(
getFactory().getSQLExceptionConverter(),
sqle,
"could not read row: " +
MessageHelper.collectionInfoString( this, key, getFactory() ),
sqlSelectSizeString
);
}
}
public boolean isExtraLazy() {
return isExtraLazy;
}
protected Dialect getDialect() {
return dialect;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -