-->
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.  [ 2 posts ] 
Author Message
 Post subject: Mapping array of CompositeUserType's
PostPosted: Tue Nov 29, 2005 4:37 pm 
Newbie

Joined: Mon Apr 18, 2005 8:15 am
Posts: 7
Hi,

I have a problem mapping an array of CompositesUserType's.

The setup:
A "Router" class has an array of "IpRange's". The "IpRange" class consist of two String values. To be able to map this to the Database via Hibernate, I have implemented the CompositeUserType interface called "IpRangeType".

My problem is: How do I do this mapping?

I have tried following without luck:
Code:
        <array
            name="ipRanges"
            inverse="false"
            cascade="all"
        >

              <key
                  column="routerId"
              >
              </key>

              <index
                  column="arrayIndex"
              />

              <element
                  column="value"
                  type="IpRangeType"
                  not-null="false"
                  unique="false"
              />

        </array>


It get:

org.hibernate.MappingException: collection element mapping has wrong number of columns: router.ipRanges type: IpRangeType


I have also tried with two "column" tags, but then it also complains...

Here is the IpRangeType impl:
Code:
public class IpRangeType implements CompositeUserType
{
    public String[] getPropertyNames()
    {
        return new String[] {"lowerLimit", "uppperLimit"};
    }

    public Type[] getPropertyTypes()
    {
        return new Type[] {Hibernate.STRING, Hibernate.STRING};
    }
   
    public Object getPropertyValue(Object component, int property) throws HibernateException
    {
        IpRange ipRange = (IpRange) component;
        if(property == 0)
            return ipRange.getLowerLimit();
        else
            return ipRange.getUpperLimit();
    }

    public void setPropertyValue(Object component, int property, Object value) throws HibernateException
    {
        IpRange ipRange = (IpRange) component;
        if(property == 0)
            ipRange.setLowerLimit((String) value);
        else
            ipRange.setUpperLimit((String) value);
    }

    public Class returnedClass()
    {
        return IpRange.class;
    }

    public boolean equals(Object x, Object y) throws HibernateException
    {
        if(x == y)
            return true;
        if(x == null || y == null)
            return false;
        return x.equals(y); // Use the IpRange implementation of "equals"
    }

    public int hashCode(Object x) throws HibernateException
    {
        return x.hashCode();
    }

    /**
     * Gets the values from the database
     * @param
     * @return
     */
    public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
                    throws HibernateException, SQLException
    {
        if(rs.wasNull())
            return null;
        String dbLowerLimit = rs.getString(names[0]);
        String dbUpperLimit = rs.getString(names[1]);
        return new IpRange(dbLowerLimit, dbUpperLimit);
    }

    public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
                    throws HibernateException, SQLException
    {
        if(value == null)
        {
            st.setNull(index, Types.VARCHAR);
            st.setNull(index+1, Types.VARCHAR);
        }
        else
        {
            IpRange ipRange = (IpRange) value;
            st.setString(index, ipRange.getLowerLimit());
            st.setString(index+1, ipRange.getUpperLimit());
        }
    }

    public Object deepCopy(Object value) throws HibernateException
    {
        if (value == null)
            return null;
        else
        {
            IpRange ipRange  = (IpRange) value;
            // return copy
            return new IpRange(ipRange.getLowerLimit(),   ipRange.getUpperLimit());
        }
    }

    public boolean isMutable()
    {
        return true;
    }

    public Serializable disassemble(Object value, SessionImplementor session) throws HibernateException
    {
        return (Serializable) deepCopy(value);
    }

    public Object assemble(Serializable cached, SessionImplementor session, Object owner)
                    throws HibernateException
    {
        return deepCopy(cached);
    }

    public Object replace(Object original, Object target, SessionImplementor session, Object owner)
                    throws HibernateException
    {
        return null;
    }
}


Making it a Collection is not an option....

Hibernate version: 3.0.1

Name and version of the database you are using: MySQL 4.1.14

Please help me!!!

BR
Anders


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 5:06 am 
Newbie

Joined: Mon Apr 18, 2005 8:15 am
Posts: 7
Hi,

Just for those interested, here is the correct mapping:
Code:
<array
   name="ipRanges"
        inverse="false"
        cascade="all"
>

   <key
      column="routerId"
   >
   </key>

   <index
      column="arrayIndex"
   />

   <element
             type="IpRangeCompositeUserType"
             not-null="false"
             unique="false"
   >
      [b]<column name="lowerLimit"/>
      <column name="upperLimit"/>[/b]
   </element>
</array>
[/code]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.