Looks like I can use UserType to create and satisfy my needs of a special Date object for my DateTime object needs.
Can I do the same though for a TypeSafeCollection. If I query for an object and it returns me a list of that object, can it return me a specialized version of a list (my TypeSafeCollection)? And vice versa. If I have an object that contains a TypeSafeCollection of a set of objects, can Hibernate translate that collection into a list to create the necessary SQL updates?
This is a sample of my TypeSafeCollection
- This holds an internal Vector that is technically the type safe list.
Code:
public class UserTOList extends TypeSafeCollection {
/**
* default Constructor
*/
public UserTOList() {
super();
}
/**
* Appends the specified element to the end of this list.
*
* @param o - element to be appended to this list.
* @return true (as per the general contract of Collection.add) or false if a null object is passed in.
*/
public boolean add(UserTO o) {
return super.add(o);
}
/**
* Removes the element at the specified position in this list. Shifts any subsequent elements to
* the left (subtracts one from their indices).
*
* @return UserTO the object removed from the list.
* @throws IndexOutOfBoundsException - if index out of range (index < 0 || index >= size()).
*/
public UserTO remove(int index) {
return (UserTO) super.removeObj(index);
}
/**
* Removes the first occurrence of the specified element in this Collection. If the Collection
* does not contain the element, it is unchanged. More formally, removes the element with the
* lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists).
* This is generally safer than using the remove method which takes an index as it will eliminates
* the possibility of encountering an IndexOutOfBoundsException, however the Object must implement
* an equals method to be sure that the correct object is removed.
*
* @param o - element to be removed from this Collection, if present.
* @return true if the Collection contained the specified element.
*/
public boolean remove(UserTO o) {
return super.remove(o);
}
/**
* Searches for the first occurence of the given argument, testing for equality using the equals method.
*
* @param o - an object.
* @return the index of the first occurrence of the argument in this collection.; returns -1 if the object is not found.
*/
public int indexOf(UserTO o) {
if (o == null) {
return -1;
}
return super.indexOf(o);
}
/**
* Returns an enumeration of the components of this vector. The returned Enumeration object will
* generate all items in this collection. The first item generated is the item at index 0, then
* the item at index 1, and so on.
*
* @return an enumeration of the components of this Collection.
*/
public UserTOEnumeration elements() {
return new UserTOEnumeration(super.elementsObj());
}
/**
* Returns the element at the specified position in this list.
*
* @param index - index of element to return.
* @return the element at the specified position in this list.
* @throws IndexOutOfBoundsException - if index out of range (index < 0 || index >= size()).
*/
public UserTO get(int index) {
return (UserTO) super.getObj(index);
}
/**
* Replaces the element at the specified position in this list with the specified element.
*
* @param index - index of element to replace.
* @param element - element to be stored at the specified position.
* @return the element previously at the specified position.
* @throws IndexOutOfBoundsException - if index out of range (index < 0 || index >= size()).
*/
public UserTO set(int index, UserTO o) {
return (UserTO) super.setObj(index, o);
}
public class UserTOEnumeration extends TypeSafeEnumeration {
public UserTOEnumeration (Enumeration enum) {
super(enum);
}
/**
* @return Returns the next element of this enumeration if this enumeration object has at least one more element to provide.
*/
public UserTO nextElement() {
return (UserTO) super.nextElementObj();
}
}
}
I think I can use the UserType to satisfy my TypeSafeEnumeration. So if a query from Hibernate returns an int, I could use that to look up a code in my static collection of codes, and return the correct code object.
This is a sample of my TypeSafeEnumerationCode:
public class SubregionType
extends AbstractTypeSafeEnumeration
implements Serializable {
public static final SubregionType EAST =
new SubregionType(1, "East Coast");
public static final SubregionType WEST=
new SubregionType(2, "West Coast");
public static final SubregionType MID =
new SubregionType(3, "Midwest");
private SubregionType() {
}
public static final SubregionType getType(int value)
{
return (SubregionType) AbstractTypeSafeEnumeration.getTypeByValue(
value, SubregionType.class);
}
private SubregionType( int intValue, String displayString ) {
super( intValue, displayString );
}
}
Thanks,
jay