-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Complex POJOs - Too complex for Hibernate?
PostPosted: Thu May 19, 2005 11:36 am 
Regular
Regular

Joined: Tue Nov 30, 2004 4:23 pm
Posts: 62
I started finally implementing Hibernate and things are going well. Except for one big portion of our architecture that uses POJOs that we are populating from JDBC and are pretty complex in the attribute types. Most of the attributes utilize java primitive types, but the other half use custom types.

1) We have a custom DateTime to correctly handle Timezone, so all Dates from the database are converted into this DateTime and pulled from this object back into the database as a Date or Timestamp.
2) We have custom TypeSafeCollection built by wrappering a Vector and also providing a TypeSafeEnumeration, so that we can verify a collection only holds a certain type of object. So as when we query the database, we get back a TO and add that TO to our TypeSafeCollection.
3) We have a custom AbstractTypeSafeEnumeration which is a way to take codes that are stored in the database as ( 1, California ) for instance and allow it to make sense to the application. So this Enumeration contains a value and description. You get an integer from the database and call a method that returns a version of this object with its value and description.

Knowing all this, would creating Custom User Types for all of these solve my problem? Should this type of logic move into another layer that converts the Hibernate POJOs into Business Specific POJOs? Or should I scrap using Hibernate and just continue with JDBC?

Thanks,

-jay


Top
 Profile  
 
 Post subject: Custom mappings using UserType
PostPosted: Fri May 20, 2005 11:43 am 
Regular
Regular

Joined: Tue Nov 30, 2004 4:23 pm
Posts: 62
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 TypeSafeEnumeration
Code:
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 20, 2005 12:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You can do the collections using a custom UserCollectionType in Hibernate3. You specify this using the "collection-type" attribute on the collection mapping much the same as you do with the "type" attribute for normal property elements.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.