-->
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: Java 1.5, generics, enum and UserType
PostPosted: Fri Feb 18, 2005 10:59 am 
Beginner
Beginner

Joined: Wed Jan 26, 2005 5:34 am
Posts: 41
Location: France, Paris
Hi,

just to paste an example code, if it can help someone... :)
EnumUserType maps an Enum to a String representation.

Code:
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

public class EnumUserType<E extends Enum<E>> implements UserType {
    private Class<E> clazz = null;
    /**
     * Sole constructor
     */
    protected EnumUserType(Class<E> c) {
        this.clazz = c;
    }

    private static final int[] SQL_TYPES = {Types.VARCHAR};
    public int[] sqlTypes() {
        return SQL_TYPES;
    }

    public Class returnedClass() {
        return clazz;
    }

    public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException, SQLException {
        String name = resultSet.getString(names[0]);
        E result = null;
        if (!resultSet.wasNull()) {
            result = Enum.valueOf(clazz, name);
        }
        return result;
    }

    public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
        if (null == value) {
            preparedStatement.setNull(index, Types.VARCHAR);
        } else {
            preparedStatement.setString(index, value.toString());
        }
    }

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

    public boolean isMutable() {
        return false;
    }

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

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

    public Object replace(Object original, Object target, Object owner) throws HibernateException {
        return original;
    }
    public int hashCode(Object x) throws HibernateException {
        return x.hashCode();
    }
    public boolean equals(Object x, Object y) throws HibernateException {
        if (x == y)
            return true;
        if (null == x || null == y)
            return false;
        return x.equals(y);
    }
}

Then declare your enum and inherits from EnumUserType :
Code:
public enum EnumSample {
        SAMPLE_A,
        SAMPLE_B;
}
public class SampleEnumUserType extends EnumUserType<SampleEnum> {
    public SampleEnumUserType() {
        super(SampleEnum.class);
    }
}


Now in your mapping file:
Code:
   <property name="sample" type="mypackage.EnumSampleUserType" not-null="true"/>


Enjoy :)

_________________
Vincent


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 18, 2005 11:21 am 
Newbie

Joined: Thu Jan 20, 2005 11:01 am
Posts: 15
This looks nice (I haven't tried it yet, though). It might be better to put this in the wiki.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 19, 2005 7:23 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Put that on the wiki please.
Quote:
super(SampleEnum.class);

should be
Quote:
super(EnumSample.class);

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 19, 2005 1:39 pm 
Beginner
Beginner

Joined: Wed Jan 26, 2005 5:34 am
Posts: 41
Location: France, Paris
Done.
Added in the 'Design patterns' part.
http://www.hibernate.org/265.html

_________________
Vincent


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 19, 2005 2:48 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Thanks

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Annotation ...
PostPosted: Mon Feb 21, 2005 5:52 am 
Regular
Regular

Joined: Tue Oct 07, 2003 1:13 pm
Posts: 70
Location: Paris, France
Hello,
It there an annotation to deal with the custom type of the property ?
<property name="sample" type="mypackage.EnumSampleUserType" not-null="true"/>
How would it be translated with annotations ?
@Column(????)
Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 21, 2005 5:59 am 
Beginner
Beginner

Joined: Wed Jan 26, 2005 5:34 am
Posts: 41
Location: France, Paris
For now, i'm just using the XML mapping files because i begun with hibernate 2. I didn't yet look at the annotations with hibernate.

_________________
Vincent


Top
 Profile  
 
 Post subject: Re: Annotation ...
PostPosted: Wed Feb 23, 2005 9:52 am 
Newbie

Joined: Wed Feb 23, 2005 9:45 am
Posts: 4
dharma wrote:
Hello,
It there an annotation to deal with the custom type of the property ?
<property name="sample" type="mypackage.EnumSampleUserType" not-null="true"/>
How would it be translated with annotations ?
@Column(????)
Thank you.


+1
I am also interested in the answer to the question, i am suspecting that
it can't be done with annotations because EJB3 isn't going to support the
concept of a custom type.

Perhaps there is a hibernate annotation that can be used instead ?

To be honest I totally hate using xdoclet so would be willing to sacrafice
a lot to use anotations rather than xml mapping files.

--b


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 1:06 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
In CVS head you can use @Type()
It does not support composite user type and parameterized types yet.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 5:22 am 
Newbie

Joined: Wed Feb 23, 2005 9:45 am
Posts: 4
emmanuel wrote:
In CVS head you can use @Type()
It does not support composite user type and parameterized types yet.


Hi
Sorry for bugging you but will this annotation be hibernate specific or will it be something that ejb3 supports as well.

I'm trying to make an architectural decision at the moment and enumerated types caused me a lot of grief in the last version of my application.

Thanks

Bryan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 1:08 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
revoltingdigits wrote:
Sorry for bugging you but will this annotation be hibernate specific or will it be something that ejb3 supports as well.

It will be hibernate specific.

_________________
Emmanuel


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.