-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to address UserType property in HQL query
PostPosted: Wed May 11, 2005 3:11 am 
Regular
Regular

Joined: Fri Oct 01, 2004 2:19 am
Posts: 111
Location: Melbourne, Australia
Hibernate version: 3.0

How do I address a property of a UserType derived class in an HQL query?
The following is an excerpt of a mapping document:
Code:
    <class name="Trail" table="TRAIL" discriminator-value="TR">

         .... other properties ....

        <property name="layer3Options" type="<pkgName>.Layer3OptionsUserType">
            <column name="L3_VLAN_ID"/>
            <column name="L3_MTU"/>
            <column name="L3_RATE"/>
        </property>
    </class>



    <query name="findTrails"> <![CDATA[
        select  t
        from    GETrail as t
                inner join t.tps as tp
        where (( tp.port.ne.id = :a_neID and tp.port.ne.class = SomeNE ) or
             ( tp.port.ne.id = :z_neID))
        and t.layer3Options.vlanID = :vlanID
        and t.isp.id = :ispID
                                ]]>
    </query>

my question relates to the and t.layer3Options.vlanID = :vlanID fragment. I keep getting errors (QueryException: org.hibernate.QueryException: could not resolve property: layer3Options.vlanID of: <pkg>.Trail) from Hibernate during initialisation.

The Layer3OptionsUserType implements the CompositeUserType interface
and performs all the necessary operations, AFAIK. It is public, accessible

What am I doing wrong here? what is the correct syntax for this query?

_________________
Cheers,

Bonny

please don't forget to rate :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 11:18 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
post your Layer3OptionsUserType code


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 6:57 pm 
Regular
Regular

Joined: Fri Oct 01, 2004 2:19 am
Posts: 111
Location: Melbourne, Australia
steve wrote:
post your Layer3OptionsUserType code


Here it is:
Code:

/**
* A Hibernate Composite User type to handle Layer3Options simple beans
* <p/>
* $Id: Layer3OptionsUserType.java,v 1.3 2005/05/11 02:26:45 bonny Exp $
*/
public class Layer3OptionsUserType implements CompositeUserType
{
    public Class returnedClass() { return Layer3Options.class; }

    public boolean equals(Object x, Object y)
    {
        if (x == y) return true;
        if (x == null || y == null) return false;
        return x.equals(y);
    }

    public int hashCode(Object o) throws PersistenceException { return o.hashCode(); }

    public Serializable disassemble(Object o, SessionImplementor s) throws PersistenceException
    {
        return (Serializable) deepCopy(o);
    }

    public Object assemble(Serializable cached, SessionImplementor s, Object owner) throws PersistenceException
    {
        if (cached == null) return null;

        Serializable[] cachedOptions = (Serializable[]) cached;
        Layer3Options options = new Layer3Options();

        options.setVlanID(((Integer) cachedOptions[0]));
        options.setMtu(((Integer) cachedOptions[1]));
        options.setRate(((Integer) cachedOptions[2]));
        return options;
    }

    public Object replace(Object original, Object target, SessionImplementor session, Object owner)
        throws PersistenceException
    {
        return assemble(disassemble(original, session), session, owner);
    }

    public Object deepCopy(Object value)
    {
        Integer[] source = (Integer[]) value;
        Integer[] result = new Integer[source.length];

        for (int i = 0; i < result.length; i++)
            result[i] = source[i];
        return value;
    }

    public boolean isMutable()
    {
        return false;
    }

    public Object nullSafeGet(ResultSet resultSet, String[] names, SessionImplementor session, Object owner)
        throws PersistenceException, SQLException
    {

        if (resultSet.wasNull()) return null;

        Layer3Options l3o = new Layer3Options();

        l3o.setVlanID(resultSet.getInt(names[0]));
        l3o.setMtu(resultSet.getInt(names[1]));
        l3o.setRate(resultSet.getInt(names[2]));

        return l3o;
    }

    public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
        throws PersistenceException, SQLException
    {

        if (value == null)
        {
            statement.setNull(index + 0, Types.NUMERIC);
            statement.setNull(index + 1, Types.NUMERIC);
            statement.setNull(index + 2, Types.NUMERIC);
        }
        else
        {
            Layer3Options options = (Layer3Options) value;
            statement.setInt(index + 0, options.getVlanID());
            statement.setInt(index + 1, options.getMtu());
            statement.setInt(index + 2, options.getRate());
        }
    }

    public String[] getPropertyNames()
    {
        return new String[] { "l3vlanid", "l3mtu", "l3rate" };
    }

    public Type[] getPropertyTypes()
    {
        return new Type[]
        {
            Hibernate.INTEGER,
            Hibernate.INTEGER,
            Hibernate.INTEGER
        };
    }

    public Object getPropertyValue(Object component, int property) throws PersistenceException
    {
        Layer3Options options = (Layer3Options) component;
        switch( property )
        {
        case    0:  return options.getVlanID();
        case    1:  return options.getMtu();
        case    2:  return options.getRate();
        }

        throw new PersistenceException("Unexepected property index when reading Layer3Options properties");
    }

    public void setPropertyValue(Object component, int property, Object value) throws PersistenceException
    {
        Layer3Options options = (Layer3Options) component;
        switch (property)
        {
        case    0: options.setVlanID(((Integer) value).intValue()); break;
        case    1: options.setMtu(((Integer) value).intValue()); break;
        case    2: options.setRate(((Integer) value).intValue()); break;
        }

        throw new PersistenceException("Unexepected property index when setting Layer3Options properties");
    }
}

_________________
Cheers,

Bonny

please don't forget to rate :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 7:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
So 'vlanID' is not advertised by you custom type as a "dereferencable" property:
public String[] getPropertyNames()
{
return new String[] { "l3vlanid", "l3mtu", "l3rate" };
}

In your query, try:
... and t.layer3Options.l3vlanid = :vlanID ...

Or change the method above to:
public String[] getPropertyNames()
{
return new String[] { "vlanID", "l3mtu", "l3rate" };
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 7:50 pm 
Regular
Regular

Joined: Fri Oct 01, 2004 2:19 am
Posts: 111
Location: Melbourne, Australia
Steve,

Thanks! I've changed the property name in the actual type, but forgot
to change the user type as well.

_________________
Cheers,

Bonny

please don't forget to rate :)


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