1 package org.hibernate.loader.entity; 3 4 import java.io.Serializable ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 import java.util.List ; 8 import java.util.Map ; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.hibernate.HibernateException; 13 import org.hibernate.LockMode; 14 import org.hibernate.MappingException; 15 import org.hibernate.engine.SessionFactoryImplementor; 16 import org.hibernate.engine.SessionImplementor; 17 import org.hibernate.loader.JoinWalker; 18 import org.hibernate.loader.OuterJoinLoader; 19 import org.hibernate.persister.entity.OuterJoinLoadable; 20 import org.hibernate.type.Type; 21 22 30 public class EntityLoader extends OuterJoinLoader 31 implements UniqueEntityLoader { 32 33 private static final Log log = LogFactory.getLog(EntityLoader.class); 34 35 private final OuterJoinLoadable persister; 36 private final Type uniqueKeyType; 37 private final boolean batchLoader; 38 private final String entityName; 39 40 public EntityLoader( 41 OuterJoinLoadable persister, 42 LockMode lockMode, 43 SessionFactoryImplementor factory, 44 Map enabledFilters) 45 throws MappingException { 46 this(persister, 1, lockMode, factory, enabledFilters); 47 } 48 49 public EntityLoader( 50 OuterJoinLoadable persister, 51 int batchSize, 52 LockMode lockMode, 53 SessionFactoryImplementor factory, 54 Map enabledFilters) 55 throws MappingException { 56 this( 57 persister, 58 persister.getIdentifierColumnNames(), 59 persister.getIdentifierType(), 60 batchSize, 61 lockMode, 62 factory, 63 enabledFilters 64 ); 65 } 66 67 public EntityLoader( 68 OuterJoinLoadable persister, 69 String [] uniqueKey, 70 Type uniqueKeyType, 71 int batchSize, 72 LockMode lockMode, 73 SessionFactoryImplementor factory, 74 Map enabledFilters) 75 throws MappingException { 76 super(factory, enabledFilters); 77 78 this.uniqueKeyType = uniqueKeyType; 79 this.entityName = persister.getEntityName(); 80 this.persister = persister; 81 82 JoinWalker walker = new EntityJoinWalker( 83 persister, 84 uniqueKey, 85 batchSize, 86 lockMode, 87 factory, 88 enabledFilters 89 ); 90 initFromWalker( walker ); 91 92 postInstantiate(); 93 94 batchLoader = batchSize > 1; 95 96 log.debug( "Static select for entity " + entityName + ": " + getSQLString() ); 97 98 } 99 100 public Object load(Serializable id, Object optionalObject, SessionImplementor session) 101 throws HibernateException { 102 return load(session, id, optionalObject, id); 103 } 104 105 public Object loadByUniqueKey(SessionImplementor session, Object key) 106 throws HibernateException { 107 return load(session, key, null, null); 108 } 109 110 private Object load( 111 SessionImplementor session, 112 Object id, 113 Object optionalObject, 114 Serializable optionalId) 115 throws HibernateException { 116 117 List list = loadEntity( 118 session, 119 id, 120 uniqueKeyType, 121 optionalObject, 122 entityName, 123 optionalId, 124 persister 125 ); 126 127 if ( list.size()==1 ) { 128 return list.get(0); 129 } 130 else if ( list.size()==0 ) { 131 return null; 132 } 133 else { 134 if ( getCollectionOwners()!=null ) { 135 return list.get(0); 136 } 137 else { 138 throw new HibernateException( 139 "More than one row with the given identifier was found: " + 140 id + 141 ", for class: " + 142 persister.getEntityName() 143 ); 144 } 145 } 146 147 } 148 149 protected Object getResultColumnOrRow( 150 Object [] row, 151 ResultSet rs, 152 SessionImplementor session) 153 throws SQLException , HibernateException { 154 return row[row.length-1]; 155 } 156 157 protected boolean isSingleRowLoader() { 158 return !batchLoader; 159 } 160 161 162 } | Popular Tags |