Results 1 to 3 of 3

Thread: Proxy issue ?

  1. #1
    Join Date
    Nov 2011
    Posts
    8

    Default Proxy issue ?

    Hi,

    I am not sure if this the correct thread for my post. Please redirect me if I'm wrong.

    I have the following piece of code, which does not give the result I expected :

    Code:
    ...
     
    GlobalStatus newGlobalStatus  = currentStatusService.computeGlobalStatus(xxx, yyy);
    		
    GlobalStatus previousGlobalStatus = currentStatusService.getGlobalStatus(zzz);
    
    LOGGER.warn("1 newGlobalStatus.getId(): " + newGlobalStatus.getId());
    LOGGER.warn("1 previousGlobalStatus.getId(): " + previousGlobalStatus.getId());
    
    if(previousGlobalStatus==null || (previousGlobalStatus.getId()!=newGlobalStatus.getId()))
    {
      if(previousGlobalStatus==null ){LOGGER.warn("previousGlobalStatus : null " );}
      if(previousGlobalStatus!=null && previousGlobalStatus.getId()!=newGlobalStatus.getId())
      {
          LOGGER.warn("previousGlobalStatus.getId()!=newGlobalStatus.getId()" );
      }
    
    LOGGER.warn("2 previousGlobalStatus.getId() : " + previousGlobalStatus.getId() );
    LOGGER.warn("2 newGlobalStatus.getId() : " + newGlobalStatus.getId() );
    ....
    }
    GlobalStatus is an hibernate bean that looks like this :

    Code:
    @Entity
    @Table(name="GLOBAL_STATUS")
    public class GlobalStatus {
    
      private Integer id;
      ...
    
      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      public Integer getId() {
    		return id;
    	}
    	public void setId(Integer id) {
    		this.id = id;
    	}
      
      ...
    }
    The methods currentStatusService.computeGlobalStatus() and currentStatusService.getGlobalStatus() request objects from the database using HQL. Like this :

    Code:
    public GlobalStatus getByKnownCardholderCurrentStatus(
    			KnownCardholderCurrentStatus knownCardholderCurrentStatus) {
    		
    	StringBuilder stringQuery = new StringBuilder("");
    	stringQuery.append(" SELECT globalStatus ");
    	stringQuery.append(" FROM KnownCardholderCurrentStatus knownCardholderCurrentStatus ");
    	stringQuery.append(" JOIN knownCardholderCurrentStatus.globalStatus globalStatus ");
    	stringQuery.append(" WHERE knownCardholderCurrentStatus = :knownCardholderCurrentStatus ");
    		
    	Query query = sessionFactory.getCurrentSession().createQuery(stringQuery.toString());
    	query.setParameter("knownCardholderCurrentStatus", knownCardholderCurrentStatus);
    				
    	return (GlobalStatus) query.uniqueResult();
    }
    When I execute the piece of code I get :

    Code:
    1 newGlobalStatus.getId(): 130
    1 previousGlobalStatus.getId(): 130 
    previousGlobalStatus.getId()!=newGlobalStatus.getId()
    2 previousGlobalStatus.getId() : 130 
    2 newGlobalStatus.getId() : 130
    This shouldn't be happening...

    Am I really tired and I am missing something obvious ? I still don't understand why is this happening. I am suspecting that it is caused by the proxy mechanism of Spring.

    Any idea ?

    Sylvain

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,630

    Default

    You are comparing Integer objects and not int objects... new iNteger(30) = new integer(30) will return false... You need to use equals instead of = (or switch to int instead of Integer).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Nov 2011
    Posts
    8

    Default

    Thanks Marten, you are right.

    I have always thought that auto-unboxing would occur when comparing 2 Integer objects. In fact it does not, it still compares the object references. The tricky thing is that the reference is the same if you create the Integer from the same boxed primitive :

    Integer a = 20;
    Integer b = 20;

    System.out.println(a == b); //gives true

    Integer c = new Integer(20);
    Integer d = new Integer(20);

    System.out.println(c == d); //gives false

    I wonder how come I've never come across this beginner issue before.

    That is funny when something is unexplained, we always go looking at the wrong place...

    My post was really out-of-scope for this thread, as I was suspecting.

    Thanks again for your help

    Sylvain

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •