A jak vypada mapovani ?? Pouzil jsi tvuj user-type jako "type"??
Vlastimil Vávrů napsal(a):
>
>
> Zdravím konferenci,
>
> chci se zeptat zda někdo z vás již pracovat s UserType v Hiberenatu.
> Používám jej pro status uživatele. Viz:
>
> package cz.venturia.imagebank;
>
> import java.io.Serializable;
> import java.util.HashMap;
> import java.util.Map;
>
> public class UserStatus implements Serializable {
> private static final long serialVersionUID = 11 * Long.MIN_VALUE;
>
> private String name;
>
> public static final UserStatus ENABLED = new UserStatus("ENABLED");
> public static final UserStatus DISABLED = new UserStatus("DISABLED");
> public static final UserStatus NOT_APPROVED = new
> UserStatus("NOT_APPROVED");
> private static final Map INSTANCES = new HashMap();
>
> static {
> INSTANCES.put(ENABLED.toString(), ENABLED);
> INSTANCES.put(DISABLED.toString(), DISABLED);
> INSTANCES.put(NOT_APPROVED.toString(), NOT_APPROVED);
> }
>
> private UserStatus(String name) {
> this.name = name;
> }
>
> public String toString() {
> return name;
> }
>
> private Object readResolve() {
> return getInstance(name);
> }
>
> public static UserStatus getInstance(String name) {
> return (UserStatus) INSTANCES.get(name);
> }
> }
>
> UserStatusType trida vypada takto:
>
> package cz.venturia.imagebank.hibernate;
>
> 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 cz.venturia.imagebank.UserStatus;
>
> public class UserStatusUserType implements UserType {
>
> private static final int[] SQL_TYPES = { Types.VARCHAR };
>
> public Object deepCopy(Object value) throws HibernateException {
> return value;
> }
>
> public boolean equals(Object x, Object y) throws HibernateException {
> return x == y;
> }
>
> public boolean isMutable() {
> return true;
> }
>
> public Object nullSafeGet(ResultSet rs, String[] names, Object
> owner) throws HibernateException, SQLException {
> String name = rs.getString(names[0]);
> return rs.wasNull() ? null : UserStatus.getInstance(name);
> }
>
> public void nullSafeSet(PreparedStatement st, Object value, int
> index) throws HibernateException, SQLException {
> if (value == null) {
> st.setNull(index, Types.VARCHAR);
> } else {
> st.setString(index, value.toString());
> }
> }
>
> public Class returnedClass() {
> return UserStatus.class;
> }
>
> public int[] sqlTypes() {
> return SQL_TYPES;
> }
> }
>
> Zkusil jsem dal breakpointy do vsech metod ve tride UserTypeStatus - pri
> startu aplikace se zavola pouze sqlTypes a returnedClass.
>
> Pri cteni ani ukladani se jiz nevola zadna metoda.
>
> Zvlastni je, ze pri vytvareni uzivatele (registraci) se status nastavi
> (na NOT_APPROVED)
>
>
> Dík za rady.
>
>
--
Jiří Mareš (mailto:[EMAIL PROTECTED])
ČSAD SVT Praha, s.r.o. (http://www.svt.cz)
Czech Republic