-->
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.  [ 2 posts ] 
Author Message
 Post subject: how to map enum list
PostPosted: Wed May 16, 2007 10:37 pm 
Senior
Senior

Joined: Tue Sep 13, 2005 2:01 am
Posts: 137
public enum Color {
RED,
BLUE,
PINK
}


@?
public List<Color> getColors() {
...
}


How to map list of enum type? What does the resulting tables look like?
Thanks for help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 17, 2007 8:03 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

this can done by using UserType

public enum Color {
RED,
BLUE,
PINK
}



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

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

public class ColorUserType implements UserType {

private static final int[] TYPES = { Types.VARCHAR };

public int[] sqlTypes() {
return TYPES;
}

public Class returnedClass() {

return Color.class;
}

public boolean equals(Object arg0, Object arg1) throws HibernateException {
if (arg0 instanceof Color) {
Color new_name = (Color) arg0;
if (arg1 instanceof Color) {
Color new_name1 = (Color) arg1;
return new_name1.equals(new_name);
}
}
return false;

}

public int hashCode(Object arg0) throws HibernateException {

return arg0.hashCode();
}

public Object nullSafeGet(ResultSet rs, String[] names, Object arg2)throws HibernateException, SQLException {
Color dbValue = Color.valueOf((String) Hibernate.STRING.nullSafeGet(rs, names[0]));
return dbValue;
}

public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
if ( value != null ) {
String v =((Color)value).toString();
Hibernate.STRING.nullSafeSet(st, v, index);
}
else {
Hibernate.STRING.nullSafeSet(st, value, index);
}


}

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

public boolean isMutable() {
return false;
}

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

}

public Object assemble(Serializable arg0, Object arg1)
throws HibernateException {
return deepCopy(arg0);

}

public Object replace(Object arg0, Object arg1, Object arg2)
throws HibernateException {
return deepCopy(arg0);

}

}


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