Q:

Editing an EJB entity

I've got a method in ejb part that has to increment a simple integer value of an entity and persist the changes into db.
I can't get it work, as it prints always the same value!
Here's the code :
public void incrementDownloads(Dispensa dispensa) {
    int dl = dispensa.getDownloads();
    System.out.println(dispensa.getTitolo() + " had " + dl + " downloads");
    dispensa.setDownloads(dl++);
    dispensaFacade.edit(dispensa);        
    dispensa = dispensaFacade.refresh(dispensa);
    System.out.println("now it has " + dispensa.getNumDownloads());
}

where dispensaFacade used methods are the one from the autogenerated AbstractFacade:
public void edit(T entity) {
    getEntityManager().merge(entity);
}

public T refresh(T entity){
    T res = getEntityManager().merge(entity);
    getEntityManager().refresh(res);
    return res;
}

My code prints out :

Title 1 had 0 downloads
now it has 0

which is clearly wrong!
Where's the mistake?

A:

When used alone, i.e.
++i;
// or
i++;

the pre/post increment operators are functionally equivalent.  In your case however, you are using them as a method parameter and the difference becomes apparent.
Here is some code that I hope will illustrate the issue.
public static void main(String[] args) {
    int i = 0;
    printPassedValue(i++);
    System.out.println("i = " + i);
    printPassedValue(++i);
    System.out.println("i = " + i);
}

private static void printPassedValue(int value) {
    System.out.println("Passed value: " + value);
}

when run this code produces
Passed value: 0
i = 1
Passed value: 2
i = 2

As you can see, when using post-increment this way (i.e. i++), the value of i is passed to the method before i is incremented.  However, when using pre-increment (++i) i is incremented and then the value is passed to the method.  The latter is apparently what you want.
This is one of the areas where one needs to be careful taking shortcuts;
if you had done 
dl++;  // or ++dl;
dispensa.setDownloads(dl);

then you would not have encountered this issue.

