-->
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.  [ 11 posts ] 
Author Message
 Post subject: Boolean custom type - Y or null (y-true,null-false)
PostPosted: Tue Aug 17, 2004 8:18 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
How I make this type - in database is Y for true and null for false ?

regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 10:54 pm 
Beginner
Beginner

Joined: Mon Aug 09, 2004 12:31 pm
Posts: 47
Location: New York, NY, USA
Use a UserType.

_________________
--DP


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 1:34 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
I do - hibernate return warning now
WARN net.sf.hibernate.type.CustomType - custom type does not implement Serializable: class yu.co.snpe.hibernate.YesOrNullUserType

It work fine (I guess) - can custom type be primitive ?


regards


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 8:52 am 
Beginner
Beginner

Joined: Mon Aug 09, 2004 12:31 pm
Posts: 47
Location: New York, NY, USA
If it's warning you about implementing serializable, then why not just implement Serializable?

_________________
--DP


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 12:41 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
I can't implement Serializable for primitive type


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 3:34 pm 
Beginner
Beginner

Joined: Mon Aug 09, 2004 12:31 pm
Posts: 47
Location: New York, NY, USA
It's not the 'boolean' type that must implement Serializable, it's the object that implements UserType that must implement Serializable (in your case yu.co.snpe.hibernate.YesOrNullUserType )

_________________
--DP


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 4:03 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
It don't help me - I try implements Serializable in my user type, but CustomType call returnedClass and check interface for this class - it isn't important what my user type implements

regards


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 7:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Try placing Serializable as the first implements clause. All others follow it. This should stop the warning message.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 10:37 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
David,
It isn't work - CustomType class check returnedClass (boolean.class), not my user type.
Class work fine - I test, load, save, delete
This is class :

package yu.co.snpe.hibernate;

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

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.UserType;
import net.sf.hibernate.util.EqualsHelper;

public class YesOrNullUserType implements Serializable,UserType {

private final String stringTrue = "Y";

public int[] sqlTypes() {
return new int[] { Types.VARCHAR };
}

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

public boolean equals(Object x, Object y) throws HibernateException {
return EqualsHelper.equals(x, y);
}

public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
String val = rs.getString(names[0]);
if (null != val && val.equals(stringTrue))
return (new Boolean(true));

return new Boolean(false);
}

public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
if (((Boolean) value).booleanValue())
st.setString(index, stringTrue);
else
st.setNull(index, Types.VARCHAR);
}

public Object deepCopy(Object value) throws HibernateException {
return value;
}

public boolean isMutable() {
return false;
}

}

regards


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 11:19 am 
Beginner
Beginner

Joined: Mon Aug 09, 2004 12:31 pm
Posts: 47
Location: New York, NY, USA
You wrote:
Code:
public Class returnedClass() {
return boolean.class;
}


but then
Code:
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
String val = rs.getString(names[0]);
if (null != val && val.equals(stringTrue))
return (new Boolean(true));

return [b]new Boolean(false);[/b]}


Did you really mean:
Code:
public Class returnedClass() {
return Boolean.class;
}

_________________
--DP


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 11:27 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
I can't return primitive type in nullSafeGet, because return type is Object

I don't know can I use primitive type in User Type, but this class wokr with warning

My question is : is it correct - I know that I can do work with Boolean

regards


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