Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

question about CMT, id for managed entity is null

843830Mar 5 2010 — edited Mar 8 2010
I am trying to use CMT to persist basic customer shopping cart details to the database. For each transaction, single 'customer' and 'order' entities are created and persisted, and then multiple 'orderHasProduct' entities are created and persisted, depending on the number of products ordered. The problem I'm having is that when I try to bind the 'orderHasProduct' entity to the managed 'order' entity, the ID for the managed entity is returning null. What is interesting is that, looking at the server output below, you can see that '13' has been assigned as the order ID, however I'm not able to associate this ID with the 'orderHasProduct' entity.

I have been trying to figure this out for days. Can someone please offer some suggestions? I've listed code below, and server output beneath.
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public boolean placeOrder(String name, String email, String phone, String address, String cityRegion, String ccNumber, ShoppingCart cart) {

    try {
        Customer customer = addCustomer(name, email, phone, address, cityRegion, ccNumber);
        CustomerOrder order = addOrder(customer, cart);
//            addOrderedItems(order, cart);
        return true;
    } catch (Exception e) {
        context.setRollbackOnly();
        return false;
    }
}

public Customer addCustomer(String name, String email, String phone, String address, String cityRegion, String ccNumber) {

    Customer customer = new Customer();
    customer.setName(name);
    customer.setEmail(email);
    customer.setPhone(phone);
    customer.setAddress(address);
    customer.setCityRegion(cityRegion);
    customer.setCcNumber(ccNumber);

    em.persist(customer);

    return customer;
}

public CustomerOrder addOrder(Customer customer, ShoppingCart cart) {

    // set up customer order
    CustomerOrder order = new CustomerOrder();
    order.setCustomerId(customer);        // works fine - new ID is obtained from managed entity
    order.setAmount(BigDecimal.valueOf(cart.getTotal()));

    em.persist(order);

    Iterator it = cart.getItems().keySet().iterator();

    // iterate through shopping cart and add items to OrderHasProduct
    while(it.hasNext()) {

        // get product
        String s = (String)it.next();
        int productId = Integer.parseInt(s);
        Product product = em.find(Product.class, productId);

        // get product quantity
        ShoppingCartItem item = (ShoppingCartItem) cart.getItems().get(String.valueOf(productId));
        String quantity = String.valueOf(item.getQuantity());

        // create ordered item
        OrderHasProduct orderedItem = new OrderHasProduct();
        orderedItem.setCustomerOrder(order);        // the ID from the managed entity returns null
        orderedItem.setProduct(product);
        orderedItem.setQuantity(quantity);

        em.persist(orderedItem);
    }

    return order;
}
Server output:
FINER: begin unit of work commit
FINER: TX beginTransaction, status=STATUS_ACTIVE
FINEST: Execute query InsertObjectQuery(entity.Customer[id=null])
FINEST: reconnecting to external connection pool
FINE: INSERT INTO customer (phone, email, address, cc_number, name, city_region, date_created) VALUES (?, ?, ?, ?, ?, ?, ?)
        bind => [123456789, quincy.jones@redhat.com, 45 Wilson Street, 1111222233334444, Quincy, 1, null]
FINEST: Execute query ValueReadQuery(sql="SELECT LAST_INSERT_ID()")
FINE: SELECT LAST_INSERT_ID()
FINEST: assign sequence to the object (12 -> entity.Customer[id=null])
FINEST: Execute query InsertObjectQuery(entity.CustomerOrder[id=null])
FINEST: Execute query WriteObjectQuery(entity.Customer[id=12])
FINE: INSERT INTO `order` (amount, date_created, customer_id) VALUES (?, ?, ?)
        bind => [6.49, null, 12]
FINEST: Execute query ValueReadQuery(sql="SELECT LAST_INSERT_ID()")
FINE: SELECT LAST_INSERT_ID()
FINEST: assign sequence to the object (13 -> entity.CustomerOrder[id=null])
FINEST: Execute query InsertObjectQuery(entity.OrderHasProduct[orderHasProductPK=null])
FINE: INSERT INTO order_has_product (quantity, order_id, product_id) VALUES (?, ?, ?)
        bind => [1, null, null]
FINE: SELECT 1
WARNING: Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'order_id' cannot be null
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 5 2010
Added on Mar 5 2010
6 comments
210 views