-->
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.  [ 5 posts ] 
Author Message
 Post subject: When are (Composite)UserTypes updated?
PostPosted: Thu Nov 03, 2005 7:22 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Hibernate version:
3.0.5

Hi

We've created a CompositeUserType. Now we try to update a property of this usertype and commit the transaction.

Code:
                auto = (TestAuto)autoDao.read(auto.getId());
                auto.getMarke().setTextSpracheDe("Toyota");  // Marke is a UserType


It seams, that hibernate doesn't consider the entity as dirty, since the usertpye instance itself has not been changed.

Does this mean, that we have to clone and replace the user type every time a property of the user type changes?

Best Regards
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 7:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Show you UserType code as that is the thing responsible for telling Hibernate when a "Marke" should be considered dirty.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 8:26 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Code:
/*
* Copyright 2005 Bedag Informatik AG
*
*/

package ch.bedag.gba.test.type;

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 ch.bedag.gba.type.AbstractUserType;

/**
* @alias LanguageStringType 
* @since  21.07.2005
*/
public class TestMultiLanguageStringUserType extends AbstractUserType {

    private static final int[] SQL_TYPES = {
            Types.VARCHAR,Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
    public static final int DE=1;
    public static final int FR=2;
    public static final int IT=3;
    public static final int EN=4;
    private static final int COUNT=4;

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

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

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

    public boolean equals(Object x, Object y) throws HibernateException {
      if (x == y) return true;
      if (x == null || y == null) return false;
      return x.equals(y);
    }

    public int hashCode(Object arg0) throws HibernateException {
        if (arg0 == null) {
            return 0;
        } else {
            return arg0.hashCode();
        }
    }

    public boolean isMutable() {
        return false;
    }

    public Object nullSafeGet(ResultSet result, String[] names, Object owner)
            throws HibernateException, SQLException {

       String textSpracheDe = result.getString(names[0]);
       String textSpracheFr = result.getString(names[1]);
       String textSpracheIt = result.getString(names[2]);
       String textSpracheEn = result.getString(names[3]);
       if (textSpracheDe == null && textSpracheFr == null &&
           textSpracheIt == null && textSpracheEn == null) {
          return null;
       } else {
          TestMultiLanguageString languageString=new TestMultiLanguageString(textSpracheDe,
                   textSpracheFr, textSpracheIt, textSpracheEn);
          notify(languageString,owner);
          return languageString;
        }       
    }

    public void nullSafeSet(PreparedStatement st, Object value, int index)
            throws HibernateException, SQLException {
           
       if (value == null) {
         st.setNull(index, SQL_TYPES[0]);
         st.setNull(index+1, SQL_TYPES[1]);
         st.setNull(index+2, SQL_TYPES[2]);
         st.setNull(index+3, SQL_TYPES[3]);                
      }else{
          nullSafeSetString(st,index, SQL_TYPES[0],
                  ((TestMultiLanguageString)value).getTextSpracheDe());
          nullSafeSetString(st,index+1, SQL_TYPES[1],
                  ((TestMultiLanguageString)value).getTextSpracheFr());
          nullSafeSetString(st,index+2, SQL_TYPES[2],
                  ((TestMultiLanguageString)value).getTextSpracheIt());
          nullSafeSetString(st,index+3, SQL_TYPES[3],
                  ((TestMultiLanguageString)value).getTextSpracheEn());
      }
    }

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

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

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

    private void nullSafeSetString(PreparedStatement st,
            int index,
            int sqlType,
            String value)
          throws SQLException {
        if (value == null) {
            st.setNull(index, sqlType);
        } else {
          st.setString(index, value);
        }
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 8:47 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Actually this fixed the problem:

Code:
    public Object deepCopy(Object arg0) throws HibernateException {
        return new TestMultiLanguageString((TestMultiLanguageString) arg0);
    }


Now the user type is updated even if only a property of the usertype is updated.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 5:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
The other thing is your isMutable() impl, as instances certainly do seem to be mutable (according to your first code example).


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