From 4dc2467a12f11db7b2a92e92e7c148df121acb72 Mon Sep 17 00:00:00 2001 From: dsyer Date: Mon, 1 Oct 2007 20:11:49 +0000 Subject: [PATCH] RESOLVED - issue BATCH-149: ClobUserType is the wrong base type for BatchStatus http://opensource.atlassian.com/projects/spring/browse/BATCH-149 Use the ImmutableValueUserType that was introduced for ExitStatus. --- .../repository/dao/BatchStatusUserType.java | 60 ++++++++++--------- .../dao/ImmutableValueUserType.java | 6 ++ .../dao/ImmutableValueUserTypeTests.java | 12 ++++ 3 files changed, 51 insertions(+), 27 deletions(-) diff --git a/execution/src/main/java/org/springframework/batch/execution/repository/dao/BatchStatusUserType.java b/execution/src/main/java/org/springframework/batch/execution/repository/dao/BatchStatusUserType.java index de5d602e3..8e97f800f 100644 --- a/execution/src/main/java/org/springframework/batch/execution/repository/dao/BatchStatusUserType.java +++ b/execution/src/main/java/org/springframework/batch/execution/repository/dao/BatchStatusUserType.java @@ -19,47 +19,53 @@ package org.springframework.batch.execution.repository.dao; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Types; +import org.hibernate.HibernateException; import org.springframework.batch.core.domain.BatchStatus; -import org.springframework.jdbc.support.lob.LobCreator; -import org.springframework.jdbc.support.lob.LobHandler; -import org.springframework.orm.hibernate3.support.ClobStringType; /** * User type object to help Hibernate to persist {@link BatchStatus} objects - * (just plonking it a Clob). + * (just plonking it a String). * - * @author tomas.slanina + * @author Dave Syer * */ -public class BatchStatusUserType extends ClobStringType { +public class BatchStatusUserType extends ImmutableValueUserType { - /** - * Get a {@link BatchStatus} from a Clob. - * - * @return a {@link BatchStatus} object whose string representation is the - * same as the database value. - * - * @see org.springframework.orm.hibernate3.support.ClobStringType#nullSafeGetInternal(java.sql.ResultSet, - * java.lang.String[], java.lang.Object, - * org.springframework.jdbc.support.lob.LobHandler) + /* (non-Javadoc) + * @see org.springframework.batch.execution.repository.dao.ImmutableValueUserType#sqlTypes() */ - protected Object nullSafeGetInternal(ResultSet rs, String[] names, Object owner, LobHandler lobHandler) - throws SQLException { - String status = (String) super.nullSafeGetInternal(rs, names, owner, lobHandler); - return BatchStatus.getStatus(status); + public int[] sqlTypes() { + return new int[] { Types.VARCHAR }; } /** - * Convert an object to a string and then pop it in a Clob. - * - * @see org.springframework.orm.hibernate3.support.ClobStringType#nullSafeSetInternal(java.sql.PreparedStatement, - * int, java.lang.Object, org.springframework.jdbc.support.lob.LobCreator) + * Get the + * @see org.springframework.batch.execution.repository.dao.ImmutableValueUserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object) */ - protected void nullSafeSetInternal(PreparedStatement ps, int index, Object value, LobCreator lobCreator) - throws SQLException { - String status = (value == null) ? "" : value.toString(); - super.nullSafeSetInternal(ps, index, status, lobCreator); + public Object nullSafeGet(ResultSet rs, String[] names, Object owner) + throws HibernateException, SQLException { + return BatchStatus.getStatus(rs.getString(names[0])); + } + + + + /** + * Plonk a String representation of the status in the prepared statement. + * + * @see org.springframework.batch.execution.repository.dao.ImmutableValueUserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int) + */ + public void nullSafeSet(PreparedStatement st, Object value, int index) + throws HibernateException, SQLException { + st.setString(index, value.toString()); + } + + /* (non-Javadoc) + * @see org.springframework.batch.execution.repository.dao.ImmutableValueUserType#returnedClass() + */ + public Class returnedClass() { + return BatchStatus.class; } } diff --git a/execution/src/main/java/org/springframework/batch/execution/repository/dao/ImmutableValueUserType.java b/execution/src/main/java/org/springframework/batch/execution/repository/dao/ImmutableValueUserType.java index 03af04085..a2f42955a 100644 --- a/execution/src/main/java/org/springframework/batch/execution/repository/dao/ImmutableValueUserType.java +++ b/execution/src/main/java/org/springframework/batch/execution/repository/dao/ImmutableValueUserType.java @@ -31,6 +31,12 @@ public abstract class ImmutableValueUserType implements UserType { } public boolean equals(Object x, Object y) throws HibernateException { + if (x==null && y==null) { + return true; + } + if (x==null) { + return false; + } return x.equals(y); } diff --git a/execution/src/test/java/org/springframework/batch/execution/repository/dao/ImmutableValueUserTypeTests.java b/execution/src/test/java/org/springframework/batch/execution/repository/dao/ImmutableValueUserTypeTests.java index e84701ff9..245e09913 100644 --- a/execution/src/test/java/org/springframework/batch/execution/repository/dao/ImmutableValueUserTypeTests.java +++ b/execution/src/test/java/org/springframework/batch/execution/repository/dao/ImmutableValueUserTypeTests.java @@ -38,6 +38,18 @@ public class ImmutableValueUserTypeTests extends TestCase { assertEquals("foo", type.disassemble("foo")); } + public void testEqualsWithNullObjects() { + assertTrue(type.equals(null, null)); + } + + public void testEqualsWithFirstNullObject() { + assertFalse(type.equals(null, "foo")); + } + + public void testEqualsWithSecondNullObject() { + assertFalse(type.equals("foo", null)); + } + public void testEqualsWithEqualObjects() { assertTrue(type.equals("foo", "foo")); }