Class.clone error ?!?!?!

asked 2010-12-08 11:42:57 +0800

lramellavotta gravatar image lramellavotta flag of Italy
200 1 8

Hello everyone,
someone can explain to me why the clone () method does not return an object identical to the original?
What is wrong?

Thanks for your patience

old_current = current.clone();
   } catch (InterruptedException e) 

public class Utenti implements Cloneable
	private String utente;
	private String passwd;
	private String verifica;
	private String tipo;
	private String cognome;
	private String nome;
	private Boolean abilitato;
	private java.sql.Timestamp data_cambio_passwd;
	private java.sql.Timestamp data_abilitazione;
	private java.sql.Timestamp data_disabilitazione;
	private java.sql.Timestamp data_ultimo_login;
	private java.sql.Timestamp data_inserimento;
	private String login_inserimento;
	private java.sql.Timestamp data_variazione;
	private String login_variazione;
	public Utenti clone() throws CloneNotSupportedException 
		Utenti ut = (Utenti) super.clone();
		return ut;


delete flag offensive retag edit

2 Replies

Sort by ยป oldest newest

answered 2010-12-08 17:08:01 +0800

mjablonski gravatar image mjablonski
1284 3 5

Did you implement a meaningful .equals() in your class called "Utenti"? If not, .equals() is derived from Object.class and compares simple object identity (same as objectA == objectB) which is false for an object and it's clone by design.

Cheers, Maik

link publish delete flag offensive edit

answered 2010-12-09 02:24:59 +0800

lramellavotta gravatar image lramellavotta flag of Italy
200 1 8

updated 2010-12-09 02:26:20 +0800

Hi Maik, I have write this code in my class

	@Override public boolean equals(Object obj) 
	    if (this == obj) return true;
	    if (!(obj instanceof Utenti)) return false;
	    Utenti ut = (Utenti)obj;

	    return UtilityVarie.confrontaOggetti.uguale(this.abilitato, ut.abilitato) 
	        && UtilityVarie.confrontaOggetti.uguale(this.cognome, ut.cognome)
	        && UtilityVarie.confrontaOggetti.uguale(this.data_abilitazione, ut.data_abilitazione)
	        && UtilityVarie.confrontaOggetti.uguale(this.data_cambio_passwd, ut.data_cambio_passwd)
	        && UtilityVarie.confrontaOggetti.uguale(this.data_disabilitazione, ut.data_disabilitazione)
	        && UtilityVarie.confrontaOggetti.uguale(this.data_inserimento, ut.data_inserimento)
	        && UtilityVarie.confrontaOggetti.uguale(this.data_ultimo_login, ut.data_ultimo_login)
	        && UtilityVarie.confrontaOggetti.uguale(this.data_variazione, ut.data_variazione)
	        && UtilityVarie.confrontaOggetti.uguale(this.login_inserimento, ut.login_inserimento)
	        && UtilityVarie.confrontaOggetti.uguale(this.login_variazione, ut.login_variazione)
	        && UtilityVarie.confrontaOggetti.uguale(this.nome, ut.nome)
	        && UtilityVarie.confrontaOggetti.uguale(this.passwd, ut.passwd)
	        && UtilityVarie.confrontaOggetti.uguale(this.tipo, ut.tipo)
	        && UtilityVarie.confrontaOggetti.uguale(this.utente, ut.utente)
	        && UtilityVarie.confrontaOggetti.uguale(this.verifica, ut.verifica);

and this....

		public static class confrontaOggetti
			public static boolean uguale(String a, String b)
				return a==b;
			public static boolean uguale(Boolean a, Boolean b)
				return a=b;
			public static boolean uguale(java.sql.Timestamp a, java.sql.Timestamp b)
				return a == null ? b == null : a.equals(b);
			public static boolean uguale(Object a, Object b)
				return a == null ? b == null : a.equals(b);

Some fields (only those defined Timestamp) changed during this operation:

        private List<Utenti> thelist;
	private Utenti  current, old_current;

	public void refreshUi() 
		UtentiDAO pdao = (UtentiDAO)SpringUtil.getBean("UtentiDAO");
		Tabbox tbox = (Tabbox)getFellow("tbox");
		thelist = pdao.findAll();
		if (thelist.isEmpty() == false )
			setCurrent(thelist.iterator().next());   <<<------------ !!!!!! Here the problem.......
	public void setCurrent(Utenti cur) 
		this.current= cur;

The problem is that some fields (Timestamp) are included in the List with the milliseconds and are not in the database.

Field before RefreshUI: 08/12/2010 08:47:03.0
Same field after: 08/12/2010 08:47:03.557

It 's a problem of casting? It 's a problem JdbcTemplate? It 's a problem with my program?


link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools




Asked: 2010-12-08 11:42:57 +0800

Seen: 130 times

Last updated: Dec 09 '10

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More