persistence@glassfish.java.net

Re: Another cascade question

From: Greg Ederer <greg_at_ergonosis.com>
Date: Wed, 15 Nov 2006 06:48:59 -0800
Thanks, Chris.  I was able to fix this.

Cheers,

Greg

Christopher Delahunt wrote:
Hello Greg,
 
Not sure what your customerOrder.setCustomerNature(customerNature);
method does, but I don't see the code where you add the customerOrder object to customerNature's list of customerOrders.  So when you delete, the collection is empty which is why it does not cascade remove.  You must maintain both sides of any bidirectional relationship according to the spec.
 
 

Best Regards,
Chris
 
 
----- Original Message -----
From: Greg Ederer
To: persistence@glassfish.dev.java.net
Sent: Monday, November 13, 2006 11:16 AM
Subject: Re: Another cascade question

Hi Markus,

Here is the JUnit test that causes the error condition:

  @Test
  public void checkout()
  {
    boolean testRemove = true;
   
    System.out.println("starting checkout");
    String userName = "rodrigo";
   
    EntityManager em = getEntityManager();
   
    // New AppUser
    AppUser user = new AppUser();
    user.setUsername(userName);
    user.setFirstName("Mickey");
    user.setLastName("Mouse");
    user.setPassword("nopass");
   
    // AppUser becomes a customer
    CustomerNature customerNature = new CustomerNature();
    user.setCustomerNature(customerNature);
   
    // Give the user a shopping cart
    ShoppingCart shoppingCart = new ShoppingCart();
    customerNature.setShoppingCart(shoppingCart);
   
    // Add some items to the cart
    // New offering
    Offering offering = new Offering();
    offering.setName("Rubber Ducky");
    offering.setDescription("A nice rubber duck.");
    offering.setPrice(new Double(10.00));
    // Create a new ShoppingCartItem to put in the cart
    ShoppingCartItem shoppingCartItem = new ShoppingCartItem();
    shoppingCartItem.setOffering(offering);
    Offering offering2 = new Offering();
    offering2.setName("Dog");
    offering2.setDescription("A lovely dog.");
    offering2.setPrice(new Double(50.00));
    // Create a new ShoppingCartItem to put in the cart
    ShoppingCartItem shoppingCartItem2 = new ShoppingCartItem();
    shoppingCartItem2.setOffering(offering2);
    // Add the items to the cart
    shoppingCart.addItem(shoppingCartItem);
    shoppingCart.addItem(shoppingCartItem2);
   
    assertEquals(shoppingCart.getTotal(), 60.00);
   
    // Create a billing details
    CreditCardBillingDetails billingDetails = new CreditCardBillingDetails();
    billingDetails.setName("Company credit card");
    billingDetails.setDescription("Used for business purchases.");
    billingDetails.setAccountNumber("4111111111111111");
    billingDetails.setCreditCardType("VISA");
    billingDetails.setExpiryDate(new Date());
    billingDetails.setNameOnCard(customerNature.getAppUser().getFullName());
   
    // Create a customer order
    CustomerOrder customerOrder = new CustomerOrder();
    customerOrder.setCustomerNature(customerNature);
    customerOrder.setShoppingCart(shoppingCart);
    customerOrder.setBillingDetails(billingDetails);
    // CustomerOrder.setBillingDetails should add this billingDetails to customerNature
    assertTrue(customerNature.getBillingDetails().get(0).equals(billingDetails));
   
    System.out.println("Persisting...");
    try
    {
      em.getTransaction().begin();
      em.persist(offering);
      em.persist(offering2);
      em.persist(user);
      em.getTransaction().commit();
    }
    catch(Exception e)
    {
      //fail("Persist failed: " + e.getMessage());
      try
      {
        em.getTransaction().rollback();
      }
      catch(Exception ex)
      {
        //fail("Rollback of Persist failed: " + ex.getMessage());
      }
      e.printStackTrace();
    }
    System.out.println("Persisted.");
   
    if(testRemove)
    {
      System.out.println("Removing...");
      try
      {
        em.getTransaction().begin();
        em.remove(user);
        em.remove(offering);
        em.remove(offering2);
        em.getTransaction().commit();
      }
      catch(Exception e)
      {
        //fail("Remove failed: " + e.getMessage());
        try
        {
          em.getTransaction().rollback();
        }
        catch(Exception ex)
        {
          //fail("Rollback of Remove failed: " + ex.getMessage());
        }
        e.printStackTrace();
      }
      System.out.println("Removed.");
    }
  }
 

Thanks!

Greg

Markus Fuchs wrote:
Hi Greg,

hard to tell. The mapping is correct. Could you post your test program here?

Thanks,

-- markus.

Greg Ederer wrote On 11/12/06 09:08,:
Hi,

Following Markus' excellent advice on my OneToOne cascade question, I subsequently created the following implementation:

CustomerNature has:

  @OneToMany(cascade={CascadeType.ALL}, mappedBy="customerNature")
  private List<CustomerOrder> customerOrders = new ArrayList<CustomerOrder>();

 
CustomerOrder has:

  @ManyToOne
  @JoinColumn(name="customer_nature", nullable=false)
  private CustomerNature customerNature;


When I run my tests, I get:

javax.persistence.RollbackException: Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2006.8 (Build 060830)): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERROR: update or delete on "customer_nature" violates foreign key constraint "fk_customer_order_customernature_id" on "customer_order"
  Detail: Key (id)=(3) is still referenced from table "customer_order".Error Code: 0
Call:DELETE FROM customer_nature WHERE (ID = ?)
    bind => [3]


I am gradually working my way through the JSR 220 specification (wish I had more time to devote to this).  In the mean time, could some kind soul please enlighten me on this issue?

Thanks!

Greg
--
| E R G O N O S I S
| Greg Ederer
| Lead Developer
| greg@ergonosis.com
| 360.379.1157
| 
| OpenDocument - OK
|


--
| E R G O N O S I S
| Greg Ederer
| Lead Developer
| greg@ergonosis.com
| 360.379.1157
| 
| OpenDocument - OK
|


--
| E R G O N O S I S
| Greg Ederer
| Lead Developer
| greg@ergonosis.com
| 360.379.1157
| 
| OpenDocument - OK
|