-->
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: Problems with CustomType
PostPosted: Thu Mar 18, 2004 11:50 am 
Newbie

Joined: Tue Jan 06, 2004 5:52 am
Posts: 17
Location: Belgium
Hi,

I have 2 problems with creating my own CustomType.

1) I get a warning that my type does not implement the Serializable interface, although this is my code:
Code:
public class MonetoryAmountType implements CompositeUserType, Serializable


Do I need to do something more?

2) I have an Order object that is mapped in hibernate which has a property price that is of type MonetoryAmount. I can store and retrieve objects fine, but this query does not work:

Code:
MonetoryAmount value = new MonetoryAmount(5);
List list = session.find( "from Order as o where o.price = ?", value, Hibernate.custom(MonetoryAmountType.class) );


Any ideas?

Wim
PS: Using hibernate version 2.1.2

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

<hibernate-mapping package="persistence.hibernate.testbench.customtypetest">
   <class name="Order" table="tbl_orders" >
      <id name="_id" column="uid" type="long" unsaved-value="-1" access="field" >
         <generator class="native"/>
      </id>
      <property name="price" type="persistence.hibernate.testbench.customtypetest.MonetoryAmountType">
         <column name="value" />
         <column name="currency" />
      </property>
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 18, 2004 11:52 am 
Newbie

Joined: Tue Jan 06, 2004 5:52 am
Posts: 17
Location: Belgium
I forgot the actual stacktrace:

Code:
There was 1 error:
1) testStoringAndRetrievingObject(persistence.hibernate.testbench.customtypetest.CustomTypeTest)net.sf.hibernate.QueryException: path expression ends in a composite value: order0_.price [from persistence.hibernate.testbench.customtypetest.Order as o where o.price = ?]
   at net.sf.hibernate.hql.PathExpressionParser.getWhereColumn(PathExpressionParser.java:370)
   at net.sf.hibernate.hql.WhereParser.doPathExpression(WhereParser.java:352)
   at net.sf.hibernate.hql.WhereParser.doToken(WhereParser.java:366)
   at net.sf.hibernate.hql.WhereParser.token(WhereParser.java:251)
   at net.sf.hibernate.hql.ClauseParser.token(ClauseParser.java:87)
   at net.sf.hibernate.hql.PreprocessingParser.token(PreprocessingParser.java:123)
   at net.sf.hibernate.hql.ParserHelper.parse(ParserHelper.java:29)
   at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:149)
   at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:138)
   at net.sf.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:293)
   at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1530)
   at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1501)
   at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1491)
   at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1487)
   at persistence.hibernate.testbench.customtypetest.CustomTypeTest.testStoringAndRetrievingObject(CustomTypeTest.java:48)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at com.intellij.rt.execution.junit.TextTestRunner.main(TextTestRunner.java:12)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 19, 2004 7:01 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Show your implementation

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 19, 2004 7:16 am 
Newbie

Joined: Tue Jan 06, 2004 5:52 am
Posts: 17
Location: Belgium
This is the code of my custom type:
Code:
package persistence.hibernate.testbench.customtypetest;

import net.sf.hibernate.CompositeUserType;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Hibernate;
import net.sf.hibernate.engine.SessionImplementor;
import net.sf.hibernate.type.Type;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.io.Serializable;

public class MonetoryAmountType implements CompositeUserType, Serializable
{
   public String[] getPropertyNames()
   {
      return new String[]{"value", "currency"};
   }

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

   public Object getPropertyValue( Object component, int property ) throws HibernateException
   {
      Object result;

      MonetoryAmount monetoryAmount = (MonetoryAmount)component;
      switch (property)
      {
         case 0:
            result = new Integer( monetoryAmount.getValue() );
            break;
         case 1:
            result = new Integer( monetoryAmount.getCurrency() );
            break;
         default:
            throw new HibernateException( "Unknown property: " + property );

      }
      return result;
   }

   public void setPropertyValue( Object component, int property, Object value ) throws HibernateException
   {
      MonetoryAmount monetoryAmount = (MonetoryAmount)component;
      switch (property)
      {
         case 0:
            monetoryAmount.setValue( ((Integer)value).intValue() );
            break;
         case 1:
            monetoryAmount.setCurrency( ((Integer)value).intValue() );
            break;
         default:
            throw new HibernateException( "Unknown property: " + property );
      }
   }

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

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

      MonetoryAmount monAmount1 = (MonetoryAmount)x;
      MonetoryAmount monAmount2 = (MonetoryAmount)y;

      if( monAmount1.getValue() != monAmount2.getValue() )
         return false;

      if( monAmount1.getCurrency() != monAmount2.getCurrency() )
         return false;

      return true;
   }

   public Object nullSafeGet( ResultSet rs, String[] names, SessionImplementor session, Object owner ) throws HibernateException, SQLException
   {
      Integer value = (Integer) Hibernate.INTEGER.nullSafeGet(rs, names[0]);
      Integer currency = (Integer) Hibernate.INTEGER.nullSafeGet(rs, names[1]);

      Object result;

      if( value == null || currency == null )
         result = null;
      else
         result = new MonetoryAmount(value.intValue(), currency.intValue());

      return result;
   }

   public void nullSafeSet( PreparedStatement st, Object value, int index, SessionImplementor session ) throws HibernateException, SQLException
   {
      MonetoryAmount amount;
      if( value == null )
         amount = new MonetoryAmount();
      else
         amount = (MonetoryAmount)value;

      Hibernate.INTEGER.nullSafeSet(st, new Integer(amount.getValue()), index);
      Hibernate.INTEGER.nullSafeSet(st, new Integer(amount.getCurrency()), index+1);
   }

   public Object deepCopy( Object value ) throws HibernateException
   {
      Object result;

      if( value == null )
      {
         result = null;
      }
      else
      {
         MonetoryAmount source = (MonetoryAmount)value;
         result = new MonetoryAmount(source.getValue(), source.getCurrency());
      }

      return result;
   }

   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);
   }
}


This is my MonetoryAmount class:

Code:
package persistence.hibernate.testbench.customtypetest;

public class MonetoryAmount
{
   public static final int EURO = 1;

   private int _value;
   private int _currency = EURO;

   public MonetoryAmount()
   {
   }

   public MonetoryAmount( int value )
   {
      _value = value;
   }

   public MonetoryAmount( int value, int currency )
   {
      _value = value;
      _currency = currency;
   }

   public int getValue()
   {
      return _value;
   }

   public void setValue( int value )
   {
      _value = value;
   }

   public int getCurrency()
   {
      return _currency;
   }

   public void setCurrency( int currency )
   {
      _currency = currency;
   }

   public String toString()
   {
      return _value + " euro";
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 19, 2004 11:03 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Try something like
Code:
"from Order as o where o.price.currency = ? and o.price.ammount=?"


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.