users@glassfish.java.net

What CascadeType really mean

From: <glassfish_at_javadesktop.org>
Date: Fri, 01 Jan 2010 08:21:28 PST

I read the documentation on cascadeType, I think I understand what it says, but then when I try something I don't get the desired results. I am using Hibernate 3.3 and JPA.

Here is the scenario:
I have a Many to One relationship between Activity to Account. I set cascadeType.PERSISTon the owning side (Activity) so when I persist Activity, it will also persist Account. But I do not want the merge on Activity to update Account. But it still does that. Why. Any thought is highly appreciated.

/Account.java
@Entity
public class Account implements Serializable {

  ...
  float balance;

  void setBalance(float balance) {
    this.balance = balance;
  }
 

}


//Activity.java
@Entity
public class Activity implements Serializable {

    @ManyToOne (cascade=CascadeType.PERSIST)
    Account account;

    float amount;
   void setAmount(float amount) {
     this.amount = amount;
   }

}


// Main
public class OneToManyRetrieve {

    public static void main(String args[]) {
...
            em = emf.createEntityManager();
            Activity activity = em.find(Activity.class, new Long(3));

            em.getTransaction().begin();
            activity.setAmount(111.11f);
            activity.getAccount().setBalance(100.88);
            em.merge(activity); // Why does it also persist Account when the CascadeType is only for PERSIST and not for MERGE
            em.getTransaction().commit();

...
    }


}
[Message sent by forum member 'teknomics' (karthik.shyamsunder_at_gmail.com)]

http://forums.java.net/jive/thread.jspa?messageID=378261