001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.mandarax.sql;
019:
020: import java.sql.ResultSet;
021: import java.sql.SQLException;
022:
023: /**
024: * Object relational mapping where the object is just the value of the
025: * one (and probably only) column. This is suitable for queries like
026: * <code>SELECT NAME FROM CUSTOMERS WHERE ID=1</code>.
027: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
028: * @version 3.4 <7 March 05>
029: * @since 1.6
030: */
031: public class OneColumnMapping implements SQLObjectRelationalMapping {
032:
033: private Class targetType = null;
034:
035: /**
036: * Constructor.
037: */
038: public OneColumnMapping() {
039: super ();
040: }
041:
042: /**
043: * Constructor.
044: * @param clazz a target type
045: */
046: public OneColumnMapping(Class clazz) {
047: super ();
048:
049: setTargetType(clazz);
050: }
051:
052: /**
053: * Build a java object from a record.
054: * @return an object
055: * @param record a result set
056: */
057: public Object buildObject(ResultSet record) throws SQLException {
058: return record.getObject(1);
059: }
060:
061: /**
062: * Get the target class.
063: * @return a class
064: */
065: public Class getTargetType() {
066: return targetType;
067: }
068:
069: /**
070: * Set the target class.
071: * @param clazz a class
072: */
073: public void setTargetType(Class clazz) {
074: targetType = clazz;
075: }
076:
077: /**
078: * Compare objects.
079: * @param obj another object
080: * @return a boolean
081: */
082: public boolean equals(Object obj) {
083: if ((obj != null) && (obj instanceof OneColumnMapping)) {
084: OneColumnMapping map = (OneColumnMapping) obj;
085:
086: return (targetType == null) ? map.targetType == null
087: : targetType.equals(map.targetType);
088: }
089:
090: return false;
091: }
092:
093: /**
094: * Get the hash code.
095: * @return the hash code
096: */
097: public int hashCode() {
098: return getClass().hashCode()
099: ^ ((targetType == null) ? 0 : targetType.hashCode());
100: }
101: }
|