-->
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: Bug when using a CompositeUserType in a composite-id ?
PostPosted: Thu Apr 14, 2005 11:34 am 
Beginner
Beginner

Joined: Thu Feb 03, 2005 12:48 pm
Posts: 22
Here is my mapping file :

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class
       name="MyClass"
       table="my_table"
       mutable="false"
    >
      <cache usage="read-only"/>

        <composite-id>
         <key-property name="skillId" type="short" column="skill_id" />
         <key-property   name="period"
                     type="MyCompositeUserType">
            <column name="skill_start_date"/>
            <column name="skill_end_date"/>
         </key-property>
      </composite-id>
         
      <property   name="name"
               type="string"
               column="skill_name"
      />
         
   </class>
</hibernate-mapping>


When I run it, I get a stack overflow, without stacktrace.

On the other hand, the following mapping file works :


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class
       name="MyClass"
       table="my_table"
       mutable="false"
    >
      <cache usage="read-only"/>

        <id
         name="skillId"
         type="short"           
         column="skill_id"
      />
      
      <property   name="name"
               type="string"
               column="skill_name"
      />

      <property   name="period"
                     type="MyCompositeUserType">
      >
            <column name="skill_start_date"/>
            <column name="skill_end_date"/>
      </property>         
         
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 14, 2005 12:13 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
and the code/stack trace is...

_________________
Emmanuel


Top
 Profile  
 
 Post subject: No stack trace
PostPosted: Thu Apr 14, 2005 1:18 pm 
Beginner
Beginner

Joined: Thu Feb 03, 2005 12:48 pm
Posts: 22
Under some conditions, a stack overflow does not ishow a stackstrace.
We are in this case.

My code is just simply :

Code:
        Collection c = session
            .getHbSession()
            .createCriteria(MyClass.class)
            .list();


I suspect this happens every time you nest a CompositeUserType in a composite-id.

In case of, here is my composite user type (which works when I do not have the mentioned nesting):

Code:


import net.sf.jtemporal.Period;


/**
* Knows how to serialize and deserialize a Period to JDBC for Hibernate
*/
public class PeriodType implements CompositeUserType, Serializable
{

    /**
     *
     */
    public PeriodType() {
        super();
        // TODO Auto-generated constructor stub
    }

    /* (non-Javadoc)
     * @see org.hibernate.usertype.CompositeUserType#getPropertyNames()
     */
    public String[] getPropertyNames() {
        // TODO Auto-generated method stub
        return new String[] {"start","end"};
    }

    /* (non-Javadoc)
     * @see org.hibernate.usertype.CompositeUserType#getPropertyTypes()
     */
    public Type[] getPropertyTypes() {
        // TODO Auto-generated method stub
        return new Type[] {Hibernate.STRING, Hibernate.STRING};
    }

    /* (non-Javadoc)
     * @see org.hibernate.usertype.CompositeUserType#getPropertyValue(java.lang.Object, int)
     */
    public Object getPropertyValue(Object component, int property)
            throws HibernateException {
        Period period = (Period) component;
        switch (property) {
            case 0:   return period.getStart();
            case 1:   return period.getEnd();
            default:  throw new HibernateException("property = "+ property);
        }
    }

    /* (non-Javadoc)
     * @see org.hibernate.usertype.CompositeUserType#setPropertyValue(java.lang.Object, int, java.lang.Object)
     */
    public void setPropertyValue(Object component, int property, Object value)
            throws HibernateException {
        throw new UnsupportedOperationException("Immutable!");
    }

    /* (non-Javadoc)
     * @see org.hibernate.usertype.CompositeUserType#returnedClass()
     */
    public Class returnedClass() {
        return Period.class;
    }

    /* (non-Javadoc)
     * @see org.hibernate.usertype.CompositeUserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], org.hibernate.engine.SessionImplementor, java.lang.Object)
     */
    public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session,
            Object owner) throws HibernateException, SQLException {

        String startString = rs.getString(names[0]);
        if (startString == null || rs.wasNull())   return null;
       
        BusinessDate startDate = BusinessDateFactory.fromString(startString);

        String endString = rs.getString(names[1]);
       
        BusinessDate endDate = BusinessDateFactory.fromEndDate(endString);
       
        assert endDate != null;

        return new Period(startDate, endDate);
    }
   

    /* (non-Javadoc)
     * @see org.hibernate.usertype.CompositeUserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int, org.hibernate.engine.SessionImplementor)
     */
    public void nullSafeSet(PreparedStatement st, Object value, int index,
            SessionImplementor session) throws HibernateException, SQLException {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException(
            "Not implemented yet, 'cause tables are read-only");
    }

    /* (non-Javadoc)
     * @see org.hibernate.usertype.CompositeUserType#deepCopy(java.lang.Object)
     */
    public Object deepCopy(Object value) throws HibernateException {
        // since a Period is immutable, return the same instance
        return value;
    }

    /* (non-Javadoc)
     * @see org.hibernate.usertype.CompositeUserType#isMutable()
     */
    public boolean isMutable() {
        return false;
    }

    /* (non-Javadoc)
     * @see org.hibernate.usertype.CompositeUserType#disassemble(java.lang.Object, org.hibernate.engine.SessionImplementor)
     */
    public Serializable disassemble(Object value, SessionImplementor session)
            throws HibernateException {
        // TODO good enough ???
        return (Serializable) value;
    }

    /* (non-Javadoc)
     * @see org.hibernate.usertype.CompositeUserType#assemble(java.io.Serializable, org.hibernate.engine.SessionImplementor, java.lang.Object)
     */
    public Object assemble(Serializable cached, SessionImplementor session, Object owner)
            throws HibernateException {
        // TODO good enough ???
        return cached;
    }

    /* (non-Javadoc)
     * @see org.hibernate.usertype.CompositeUserType#replace(java.lang.Object, java.lang.Object, org.hibernate.engine.SessionImplementor, java.lang.Object)
     */
    public Object replace(Object original, Object target, SessionImplementor session,
            Object owner) throws HibernateException {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Not implemented yet");
    }

    /* (non-Javadoc)
     * @see org.hibernate.usertype.CompositeUserType#equals(java.lang.Object, java.lang.Object)
     */
    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);
    }

    /* (non-Javadoc)
     * @see org.hibernate.usertype.CompositeUserType#hashCode(java.lang.Object)
     */
    public int hashCode(Object x) throws HibernateException {
        return hashCode(x);
    }

}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 15, 2005 4:41 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you most likely have a bug in you equals / hashcode implementation :
- of the entity
- of the type
- of the user type implem

_________________
Emmanuel


Top
 Profile  
 
 Post subject: fixed
PostPosted: Fri Apr 15, 2005 4:47 am 
Beginner
Beginner

Joined: Thu Feb 03, 2005 12:48 pm
Posts: 22
The bug was in the hashCode(object) implementation
My fault, sorry.


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.