-->
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.  [ 1 post ] 
Author Message
 Post subject: UserType return null value !
PostPosted: Tue Nov 29, 2005 11:37 pm 
Regular
Regular

Joined: Mon Aug 02, 2004 9:33 am
Posts: 69
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.0.5

Mapping documents:

<property
name="fee"
type="com.stufftolet.model.posting.persistence.MonetaryAmountCompositeUserType"
update="true"
insert="true"
>
<column
name="fee"
length="2"
not-null="true"
/>
<column
name="fee_currency"
not-null="true"
/>
</property>


Name and version of the database you are using:MySql 5.0.16


public class MonetaryAmount implements Serializable {

private final BigDecimal value;
private final Currency currency;

public MonetaryAmount(BigDecimal value, Currency currency) {
this.value = value;
this.currency = currency;
}
/**
* @return Returns the currency.
*
*/
public Currency getCurrency() {
return currency;
}
/**
* @return Returns the value.
*
*/
public BigDecimal getValue() {
return value;
}
// ********************** Common Methods ********************** //

public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof MonetaryAmount)) return false;

final MonetaryAmount monetaryAmount = (MonetaryAmount) o;

if (!currency.equals(monetaryAmount.currency)) return false;
if (!value.equals(monetaryAmount.value)) return false;

return true;
}

public int hashCode() {
int result;
result = value.hashCode();
result = 29 * result + currency.hashCode();
return result;
}

public String toString() {
return "Value: '" + getValue() + "', " +
"Currency: '" + getCurrency() + "'";
}

public int compareTo(Object o) {
if (o instanceof MonetaryAmount) {
// TODO: This would actually require some currency conversion magic
return this.getValue().compareTo(((MonetaryAmount) o).getValue());
}
return 0;
}

// ********************** Business Methods ********************** //

public static MonetaryAmount fromString(String amount, String currencyCode) {
return new MonetaryAmount(new BigDecimal(amount),
Currency.getInstance(currencyCode));
}

public static MonetaryAmount convert(MonetaryAmount amount,
Currency toConcurrency) {
// TODO: This requires some conversion magic and is therefore broken
return new MonetaryAmount(amount.getValue(), toConcurrency);
}
}



public class MonetaryAmountCompositeUserType
implements CompositeUserType {

public Class returnedClass() { return MonetaryAmount.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 Object deepCopy(Object value) {
return value; // MonetaryAmount is immutable
}

public boolean isMutable() { return false; }

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

if (resultSet.wasNull()) return null;
BigDecimal value = resultSet.getBigDecimal( names[0] );
Currency currency =
Currency.getInstance(resultSet.getString( names[1] ) );
return new MonetaryAmount(value, currency);
}

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

if (value==null) {
statement.setNull(index, Types.NUMERIC);
statement.setNull(index+1, Types.VARCHAR);
} else {
MonetaryAmount amount = (MonetaryAmount) value;
String currencyCode =
amount.getCurrency().getCurrencyCode();
statement.setBigDecimal( index, amount.getValue() );
statement.setString( index+1, currencyCode );
}
}

public String[] getPropertyNames() {
return new String[] { "value", "currency" };
}

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

public Object getPropertyValue(Object component,
int property)
throws HibernateException {
MonetaryAmount MonetaryAmount = (MonetaryAmount) component;
if (property == 0)
return MonetaryAmount.getValue();
else
return MonetaryAmount.getCurrency();
}

public void setPropertyValue(Object component,
int property,
Object value) throws HibernateException {
throw new UnsupportedOperationException("MonetaryAmount is immutable");
}

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

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

/* (non-Javadoc)
* @see org.hibernate.usertype.CompositeUserType#hashCode(java.lang.Object)
*/
public int hashCode(Object arg0) throws HibernateException {
// TODO Auto-generated method stub
return 0;
}

/* (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 arg0, Object arg1, SessionImplementor arg2, Object arg3) throws HibernateException {
// TODO Auto-generated method stub
return null;
}
}



I got the above code from caveatemptor-0.9.5 ! I m using Spring with Hibernate. I m able to insert the MonetaryAmount into database. But when try to query, the fee return null value ! The query is just a normal query like below:

getHibernateTemplate().find("from Duration");


pls help, Thanks ![/b]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.