KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > loader > entity > EntityLoader


1 //$Id: EntityLoader.java,v 1.8 2005/06/13 20:27:16 oneovthafew Exp $
2
package org.hibernate.loader.entity;
3
4 import java.io.Serializable JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
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 /**
23  * Loads an entity instance using outerjoin fetching to fetch associated entities.
24  * <br>
25  * The <tt>EntityPersister</tt> must implement <tt>Loadable</tt>. For other entities,
26  * create a customized subclass of <tt>Loader</tt>.
27  *
28  * @author Gavin King
29  */

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 JavaDoc entityName;
39
40     public EntityLoader(
41             OuterJoinLoadable persister,
42             LockMode lockMode,
43             SessionFactoryImplementor factory,
44             Map JavaDoc 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 JavaDoc 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 JavaDoc[] uniqueKey,
70             Type uniqueKeyType,
71             int batchSize,
72             LockMode lockMode,
73             SessionFactoryImplementor factory,
74             Map JavaDoc 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 JavaDoc load(Serializable JavaDoc id, Object JavaDoc optionalObject, SessionImplementor session)
101     throws HibernateException {
102         return load(session, id, optionalObject, id);
103     }
104
105     public Object JavaDoc loadByUniqueKey(SessionImplementor session, Object JavaDoc key)
106     throws HibernateException {
107         return load(session, key, null, null);
108     }
109
110     private Object JavaDoc load(
111             SessionImplementor session,
112             Object JavaDoc id,
113             Object JavaDoc optionalObject,
114             Serializable JavaDoc optionalId)
115     throws HibernateException {
116         
117         List JavaDoc 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 JavaDoc getResultColumnOrRow(
150         Object JavaDoc[] row,
151         ResultSet JavaDoc rs,
152         SessionImplementor session)
153     throws SQLException JavaDoc, HibernateException {
154         return row[row.length-1];
155     }
156
157     protected boolean isSingleRowLoader() {
158         return !batchLoader;
159     }
160
161     
162 }
Popular Tags