Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7711379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:21:38+00:00 2026-06-01T01:21:38+00:00

While creating an online shop application using play-1.2.4 ,I ran into some problems with

  • 0

While creating an online shop application using play-1.2.4 ,I ran into some problems with jpa..I wanted to provide an admin area using the CRUD module in play.Here ,an admin user can create/edit or delete the entities in the application(like Customers,Orders,Items etc).

A Customer can create Orders.Each Order will have a Set of CartItems.When an Order is deleted,the corresponding CartItems must be deleted.When a Customer is deleted,all his Orders must be deleted as well.I thought I could get this by setting the cascade property in jpa annotation.

I modelled it like this

Customer.java

@Entity
public class Customer extends Model {
    @Email
    @Required
    public String email;
    ...
    @OneToMany(mappedBy="customer", cascade=CascadeType.ALL)
    public List<Order> orders;
    @OneToOne
    public PayMethod currentPayment;
    ...
}

Order.java

@Entity
public class Order extends Model {  
    @OneToMany( cascade=CascadeType.ALL,orphanRemoval=true,fetch=FetchType.EAGER)
    public Set<CartItem> cartItems;

    @ManyToOne
    public Customer customer;
    @ManyToOne
    public PayMethod paymentMethod;
    ...
}

CartItem.java

@Entity
public class CartItem extends Model implements Comparable<CartItem>{    
    @ManyToOne
    public Item item;
    public int quantity;
}

PayMethod.java

@Entity
public class PayMethod extends Model {
    @Required
    public String cardNumber;
    @ManyToOne
    public Customer customer;
    ...
}

The following database tables were created

customer table

id |  email      |   fullname    | currentpayment_id
---|-------------|---------------|-----------------
2  |jon@gmail.com| jon           |29 

order table

 id |customer_id | paymentmethod_id 
----+------------+-----------------
 25 |  2         |       29

cartitem table

id  | quantity | item_id 
----+----------+---------
 26 |        1 |      14

*order_cartitem table*

 order_id | cartitems_id 
----------+--------------
       25 |           26

In the Admin interface created using CRUD(I haven’t implented any methods ,just using the provided CRUD module as is),I tried to delete a Customer,but then,I get this error,

ERROR: update or delete on table "cartitem" violates foreign key constraint "fk7ff437ad3e28aa91" on table "order_cartitem"
Detail: Key (id)=(26) is still referenced from table "order_cartitem".
08:03:03,031 ERROR ~ Could not synchronize database state with session

Is there something wrong with the way I modelled the Entities? I thought the delete on the Customer will be cascaded to Order and that in turn will cascade to its CartItems .

What do I have to do to get this cascading effect?Or do I have to manually remove each contained instances of CartItems?

EDIT: As per Seb’s reply

class Order extends Model { 
    @OneToMany(mappedBy="order", cascade=CascadeType.ALL,orphanRemoval=true,fetch=FetchType.EAGER)
    public Set<CartItem> cartItems;
    ...
}

class CartItem extends Model implements Comparable<CartItem>{

    @ManyToOne
    public Item item;

    public int quantity;

    @ManyToOne
    public Order order;
...
}

static void addItemToCart(Long itemId,Long orderId,String quantity) {
    Item item = Item.findById(itemId);
   Order order = Order.findById(orderId);
   int qty = Integer.parseInt(quantity);
   CartItem cartItem = new CartItem(item,qty);
   cartItem.order=order;
   order.addItem(cartItem, qty);
   order.save();
...
}

This gets rid of order_cartitem table and adds a field order_id to cartitem table

cartitem table

     id | quantity | item_id | order_id 
    ----+----------+---------+----------
     26 |        1 |      14 |       25    
     27 |        1 |      20 |       25

The admin(CRUD) interface,lists the Customers and Orders.When a particular Customer(the one who created the Order) is selected and the delete button is clicked,it results in a JPA error

JPA error
A JPA error occurred (Cannot commit): collection owner not associated with session: models.Order.cartItems

here is the stacktrace

If someone can understand why this is happening,please tell me.

Interestingly,I can click on the delete button for a particular Order,and it successfully calls the following controller method,

Admin.java

public static void deleteOrder(Long id) {
    Order order = Order.findById(id);
    order.delete();     
    ...
}

which deletes the Order and all its CartItems successfully..
So,why does this not happen when a Customer is deleted?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:21:39+00:00Added an answer on June 1, 2026 at 1:21 am

    Make your relation bidirectionnal by adding this to your cartitem class

    @ManyToOne
    public Order order;
    

    and a mappedBy=”order” in your cartItems @OneToMany

    then hibernate will better handles your deletion. I am guessing that without this bidirectionnal link, hibernate tried to firstly set the column to null. You can also try to allow null values in your join table to see what happens if you don’t want to enable the bidirectional relationship

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While creating the setup for VB.net application I am getting the following warning: Warning
While creating my horizontal menu using the <ul><li>....</li></ul> I came across a few behaviors
I am creating a web application where users can upload/download/view online pdfs. I want
While creating a Windows Form Application, I selected target to .Net Framework 4.0. Now
While creating classes, I followed OO conventions and declared all class variables before using
While creating a netlink socket using netlink_kernel_create() a function pointer is passed as argument
while creating a blog i am using the following model class and form .but
I am working on creating an online image editing tool.Looking for some refernce how
I am creating a VC++2008 Windows Form Application which needs to use some of
First some background, we are creating a new eGov application. Eventually, a citizen can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.