-->
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: Properties on User Types
PostPosted: Fri Jul 22, 2005 12:57 pm 
Newbie

Joined: Tue Jul 19, 2005 9:19 am
Posts: 7
I have a my own UserType that maps type to two varchar columsn in the database.
In my code I am trying to set a value of this type and save it to the database however this value is not saving. It does however save if create a new version of the type and then save it to the database.



Does not work :(

Code:
Opsr opsr2 = (Opsr) sessionManager.getSession().load(Opsr.class, opsr.getId());
[b]opsr2.getPsd().getAddress().getLine1().setValue("this shold be the value");[/b]
tx = sessionManager.getSession().beginTransaction();
sessionManager.getSession().saveOrUpdate(opsr2);
tx.commit();


Works

Code:
Opsr opsr2 = (Opsr) sessionManager.getSession().load(Opsr.class, opsr.getId());
[b]opsr2.getPsd().getAddress().setLine1(new ErroredString("this should be the value"));[/b]
tx = sessionManager.getSession().beginTransaction();
sessionManager.getSession().saveOrUpdate(opsr2);
tx.commit();



Is this the correct behaviour of a user type?

Just incase here is the user type

Code:
public class ErroredStringType implements UserType {

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

    /**
     * @see net.sf.hibernate.UserType#sqlTypes()
     */
    public int[] sqlTypes() {
        return SQL_TYPES;
    }

    /**
     * @see net.sf.hibernate.UserType#returnedClass()
     */
    public Class returnedClass() {
        return ErroredString.class;
    }

    /**
     * @see net.sf.hibernate.UserType#equals(java.lang.Object, java.lang.Object)
     */
    public boolean equals(Object x, Object y) throws HibernateException {
        if (x == null || y == null) {
            return false;
        }
        if (x == y) {
            return true;
        }
        return x.equals(y);
    }

    /**
     * @see net.sf.hibernate.UserType#nullSafeGet(java.sql.ResultSet,
     *      java.lang.String[], java.lang.Object)
     */
    public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException,
            SQLException {
        String value = resultSet.getString(names[0]);
        if (resultSet.wasNull()) {
            // last column was SQL NULL
        }
        String errors = resultSet.getString(names[1]);
        if (resultSet.wasNull()) {
            // last column was SQL NULL
        }
        String icrValue = resultSet.getString(names[2]);
        if (resultSet.wasNull()) {
            // last column was SQL NULL
        }
        String icrException = resultSet.getString(names[3]);
        if (resultSet.wasNull()) {
            // last column was SQL NULL
        }
        return new ErroredString(value, errors, icrValue, icrException);
    }

    /**
     * @see net.sf.hibernate.UserType#nullSafeSet(java.sql.PreparedStatement,
     *      java.lang.Object, int)
     */
    public void nullSafeSet(PreparedStatement statement, Object value, int index) throws HibernateException,
            SQLException {
        if (value == null) {
            statement.setNull(index, Types.VARCHAR);
            statement.setNull(index + 1, Types.VARCHAR);
            statement.setNull(index + 2, Types.VARCHAR);
            statement.setNull(index + 3, Types.VARCHAR);
        } else {
            String str = ((ErroredString) value).getValue();
            String error = ((ErroredString) value).getError();
            String icrValue = ((ErroredString) value).getIcrValue();
            String icrException = ((ErroredString) value).getIcrException();

            if (str == null) {
                statement.setNull(index, Types.VARCHAR);
            } else {
                statement.setString(index, str);
            }

            if (error == null) {
                statement.setNull(index + 1, Types.VARCHAR);
            } else {
                statement.setString(index + 1, error);
            }

            if (icrValue == null) {
                statement.setNull(index + 2, Types.VARCHAR);
            } else {
                statement.setString(index + 2, icrValue);
            }

            if (icrException == null) {
                statement.setNull(index + 3, Types.VARCHAR);
            } else {
                statement.setString(index + 3, icrException);
            }
        }
    }

    /**
     * @see net.sf.hibernate.UserType#deepCopy(java.lang.Object)
     */
    public Object deepCopy(Object arg0) throws HibernateException {
        return arg0;
    }

    /**
     * @see net.sf.hibernate.UserType#isMutable()
     */
    public boolean isMutable() {
        return true;
    }


Thanks of any help


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 24, 2005 7:17 pm 
Newbie

Joined: Tue Jul 19, 2005 9:19 am
Posts: 7
Just incase anyone is ever interested the problem was that I had declared it as mutable but the deep copy was just returning the argument.

Pretty stupid bug cause me much headaches...


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.