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 7608317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:53:02+00:00 2026-05-31T00:53:02+00:00

I created some Model object to represent a company with several clients, and an

  • 0

I created some Model object to represent a company with several clients, and an invoice object that consists of a company and client combination and several invoice lines. I have created the following Model objects:

@Entity
public class Company extends Model {
    @OneToMany(mappedBy="company")
    public Set<Client> clients;
}

@Entity
public class Client extends Model {
    @ManyToOne
    public Company company;
}

@Entity
public class Invoice extends Model {
    @ManyToOne
    public Company company;
    @ManyToOne
    public Client client;
    @OneToMany(mappedBy="invoice", cascade=CascadeType.ALL)
    public Set<InvoiceLine> invoiceLines;
}

@Entity
public class InvoiceLine extends Model {
    @ManyToOne
    public Invoice invoice;
}

The test:

@Test
public void testModels() {
    Client client = createClient();
    Company company = createCompany();
    company.clients = new HashSet<Client>();
    company.clients.add(client);
    company.save();

    Invoice invoice = createInvoice(client, company);
    InvoiceLine invoiceLine1 = createInvoiceLine(invoice);
    InvoiceLine invoiceLine2 = createInvoiceLine(invoice);
    Set<InvoiceLine> invoiceLines = new HashSet<InvoiceLine>();
    invoiceLines.add(invoiceLine1);
    invoiceLines.add(invoiceLine2);
    invoice.invoiceLines = invoiceLines;
    invoice.save();

    Company retrievedCompany = Company.find("byName", company.name).first();
    assertNotNull(retrievedCompany);
    assertEquals(1, retrievedCompany.clients.size());
    assertEquals(2, InvoiceLine.count());

    assertEquals(1, Invoice.deleteAll());
    assertNull(Invoice.all());
    assertNull(InvoiceLine.all());

}

When running a test that creates an invoice with two invoice lines, and try to delete this invoice, I get the following error:

org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation:
“FK3004B0A1F4110EF6: PUBLIC.INVOICELINE FOREIGN KEY(INVOICE_ID) REFERENCES >PUBLIC.INVOICE(ID)”;
SQL statement: delete from Invoice [23003-149]

What am I doing wrong?

  • 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-05-31T00:53:03+00:00Added an answer on May 31, 2026 at 12:53 am

    Have you tried this with invoice.delete()? The problem is that deleteAll() doesn’t cascade the delete operation.

    deleteAll() uses a javax.persistence.Query internally, while delete() uses the EntityManager‘s remove() method. The cascading in JPA is handled by JPA and not by the database, and JPA does not cascade bulk deletes like the one executed by deleteAll().
    Check this link for more info on bulk delete/update.

    Also: adding the InvoiceLine entities to Invoice is redundant if you are already setting Invoice as the parent in createInvoiceLine(). Just do invoice.refresh() before executing the asserts.

    Maybe the following unit test clears things up.
    The Parent1 is like your Invoice. While Child1 is like your InvoiceLine.

    import java.util.*;
    import javax.persistence.*;
    import org.junit.*;
    import play.test.*;
    import models.*;
    
    public class Parent1Test extends UnitTest {
    
        public Parent1 p;
        public Child1 c1;
        public Child1 c2;
        public Child1 c3;
    
        @Before
        public void setUp() {
            Fixtures.deleteAllModels();
    
            p = new Parent1();
            c1 = new Child1();
            c2 = new Child1();
            c3 = new Child1();
        }
    
        public void byAddingParentToChilds() {
            c1.parent = p;
            c2.parent = p;
            c3.parent = p;
    
            c1.save();
            c2.save();
            c3.save();
            p.refresh();
        }
    
        @Test
        public void testByAddingParentToChilds() {
            byAddingParentToChilds();
            assertEquals(p.id, c1.parent.id);
            assertEquals(3, Child1.count());
        }
    
        public void byAddingChildsToParent() {
            p.childs = new ArrayList<Child1>();
            p.childs.add(c1);
            p.childs.add(c2);
            p.childs.add(c3);
            p.save();
        }
    
        @Test
        public void testByAddingChildsToParent() {
            // By adding childs
            byAddingChildsToParent();
    
            c1.refresh();
            assertEquals(3, Child1.count());
            // This will be null, because you added the childs to the 
            // parent while the childs are the owning side of the 
            // relation.
            assertNull(c1.parent);
        }
    
        @Test
        public void testDeletingAfterAddingParentToChilds() {
            byAddingParentToChilds();
            p.delete();
            assertEquals(0, Parent1.count());
            assertEquals(0, Child1.count());
        }
    
        @Test
        public void testDeletingAfterAddingChildsToParent() {
            byAddingChildsToParent();
            p.delete();
            assertEquals(0, Parent1.count());
            assertEquals(0, Child1.count());
        }
    
        @Test(expected=PersistenceException.class)
        public void testDeleteAllAfterAddingParentToChilds() {
            byAddingParentToChilds();
            // The cascading doesn't work for deleteAll() so this line
            // will throw an exception because the child elements still
            // reference the parent.
            assertEquals(1, Parent1.deleteAll());
        }
    
        @Test
        public void testDeleteAllAfterAddingChildsToParent() {
            byAddingChildsToParent();
            assertEquals(1, Parent1.deleteAll());
            assertEquals(0, Parent1.count());
            // Again the cascading doesn't work for deleteAll()
            assertEquals(3, Child1.count());
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created some widgets with jQuery UI and I notice that buttons have a
I've created some MbUnit Test Fixtures that have SetUp methods marked with the SetUp
I have one data model 'object' with fields->object_id, object_name. That is: http://localhost:3000/objects/ I have
I have a Post model object that has reference to a parent object. The
Lets say I have some Django model that has a list of numbers as
So I created some rspec_scaffold for an Exercise model and added map.resource :exercises to
I have created some extra functionality on my Linq-to-SQL classes to make things easier
I basically created some tables to play around with: I have Two main tables,
I have created some rounded navigation tabs using CSS and am having trouble when
I have created some simple app in Java, and 'deployed' it using Java Web

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.