1 /*
2 * Hibernate, Relational Persistence for Idiomatic Java
3 *
4 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5 * indicated by the @author tags or express copyright attribution
6 * statements applied by the authors. All third-party contributions are
7 * distributed under license by Red Hat Middleware LLC.
8 *
9 * This copyrighted material is made available to anyone wishing to use, modify,
10 * copy, or redistribute it subject to the terms and conditions of the GNU
11 * Lesser General Public License, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this distribution; if not, write to:
20 * Free Software Foundation, Inc.
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301 USA
23 *
24 */
25 package org.hibernate.loader.entity;
26
27 import java.io.Serializable;
28 import java.sql.ResultSet;
29 import java.sql.SQLException;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.hibernate.HibernateException;
36 import org.hibernate.engine.SessionFactoryImplementor;
37 import org.hibernate.engine.SessionImplementor;
38 import org.hibernate.loader.OuterJoinLoader;
39 import org.hibernate.persister.entity.OuterJoinLoadable;
40 import org.hibernate.transform.ResultTransformer;
41 import org.hibernate.type.Type;
42
43 public abstract class AbstractEntityLoader extends OuterJoinLoader
44 implements UniqueEntityLoader {
45
46 protected static final Logger log = LoggerFactory.getLogger(EntityLoader.class);
47 protected final OuterJoinLoadable persister;
48 protected final Type uniqueKeyType;
49 protected final String entityName;
50
51 public AbstractEntityLoader(
52 OuterJoinLoadable persister,
53 Type uniqueKeyType,
54 SessionFactoryImplementor factory,
55 Map enabledFilters) {
56 super( factory, enabledFilters );
57 this.uniqueKeyType = uniqueKeyType;
58 this.entityName = persister.getEntityName();
59 this.persister = persister;
60
61 }
62
63 public Object load(Serializable id, Object optionalObject, SessionImplementor session)
64 throws HibernateException {
65 return load(session, id, optionalObject, id);
66 }
67
68 protected Object load(SessionImplementor session, Object id, Object optionalObject, Serializable optionalId)
69 throws HibernateException {
70
71 List list = loadEntity(
72 session,
73 id,
74 uniqueKeyType,
75 optionalObject,
76 entityName,
77 optionalId,
78 persister
79 );
80
81 if ( list.size()==1 ) {
82 return list.get(0);
83 }
84 else if ( list.size()==0 ) {
85 return null;
86 }
87 else {
88 if ( getCollectionOwners()!=null ) {
89 return list.get(0);
90 }
91 else {
92 throw new HibernateException(
93 "More than one row with the given identifier was found: " +
94 id +
95 ", for class: " +
96 persister.getEntityName()
97 );
98 }
99 }
100
101 }
102
103 protected Object getResultColumnOrRow(Object[] row, ResultTransformer transformer, ResultSet rs, SessionImplementor session)
104 throws SQLException, HibernateException {
105 return row[row.length-1];
106 }
107
108 protected boolean isSingleRowLoader() {
109 return true;
110 }
111
112 }