While tracking down my own problem, I modified the Hibernate3 CompositeUserTypeTest adding the following finder code from line 45 right after persisting a Transaction entity:
Code:
Query q = s.createQuery("from Transaction tran where tran.value = :value");
q.setParameter("value", new MonetoryAmount( new BigDecimal(1.5), Currency.getInstance("USD") ) );
q.list();
I believe this code should correctly find the persisted object. Instead, it gives the following error:
Code:
JDBCExceptionReporter - could not execute query [select transactio0_.id as id0_, transactio0_.description as descript2_0_, transactio0_.amount as amount0_, transactio0_.currency as currency0_ from Trnsctn transactio0_ where (transactio0_.amount, transactio0_.currency)=?]
java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:2250)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:2949)
at org.hibernate.type.StringType.set(StringType.java:26)
at org.hibernate.type.CurrencyType.set(CurrencyType.java:78)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:85)
at org.hibernate.test.cut.MonetoryAmountUserType.nullSafeSet(MonetoryAmountUserType.java:153)
Does this mean a bug, or signals a problem in the MonetaryAmount UserType example, or in my logic maybe?
(I could not write a single UserType or CompositeUserType either which could generate a correct SQL query while using them as query parameters, that's why I started to play with the hibernate testcase but it did not seem to work out either).