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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:44:58+00:00 2026-06-10T01:44:58+00:00

I have the following classes: import play.db.ebean.Model; import javax.persistence.*; @Entity public class A extends

  • 0

I have the following classes:

import play.db.ebean.Model;
import javax.persistence.*;

@Entity
public class A extends Model {
    @Id
    private int id;

    /* Other irrelevant properties */

    @OneToOne(cascade = CascadeType.ALL, optional = true)
    private B b;
}

import play.db.ebean.Model;
import javax.persistence.*;

@Entity
public class B extends Model {
    @Id
    private int id;

    /* Other irrelevant properties */

    @OneToOne(mappedBy = "b")
    private A a;

    @OneToOne(cascade = CascadeType.ALL, optional = false)
    private C c;
}

import play.db.ebean.Model;
import javax.persistence.*;

@Entity
public class C extends Model {
    @Id
    private int id;

    /* Other irrelevant properties */

    @OneToOne(mappedBy = "c")
    private B B;
}

In the database, it looks like this:

Table    a
Columns  id, ... (other irrelevant columns), b_id

Table    b
Columns  id, ...(other irrelevant columns), c_id

Table    c
Columns  id, ...(other irrelevant columns)

Now in one the controller methods (for those familiar with Play) I have an object of A, and would like to delete its property “b” from the database. This should also cascade to c, so c gets deleted too.
This is my current approach:

B b = a.getB();
b.delete();

This throws an exception though:

[PersistenceException: ERROR executing DML bindLog[] error[Cannot
delete or update a parent row: a foreign key constraint fails
(databasename.a, CONSTRAINT fk_a_payme_4 FOREIGN KEY (b_id)
REFERENCES b (id))]]

It basically boils down to the fact that I’m trying to delete b, while a still holds a foreign key reference to b in column b_id, so I should first set that reference to null.

In Java, this translates to:

B b = a.getB();
a.setB(null);
a.update();
b.delete();

This does set the reference to b in object a to null and correctly deletes the b object, but c does not get deleted from the database. (Why? I though the cascade property would take care of this)
The only way I have found to fix this is to explicitly delete c as well, so like this:

B b = a.getB();
C c = b.getC();

b.setC(null);
b.update();
c.delete();

a.setB(null);
a.update();
b.delete();

I’m not very pleased about this though, because that’s already 8 lines of code to delete two rows in the database, and it would be more if there were more relationships in this picture.

So as for my question:

How can I delete b from a so the reference from a to b is deleted automatically first and how can I make sure c is deleted too when I delete b?

Thank you in advance!

Edit: best idea so far: move the ownership of the a-b relationship to b.

  • 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-10T01:44:59+00:00Added an answer on June 10, 2026 at 1:44 am

    Try using

    class A{
    
       ......
    
       @JoinColumn(nullable=true)
       private B b;
    
       .......
    }
    

    See this question for more info What is the difference between @ManyToOne(optional=false) vs. @Column(nullable=false).

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

Sidebar

Related Questions

I have the following classes in my Model: public abstract class Entity : IEntity
I have the following code: import java.util.*; public class SellTransaction extends Transaction { private
I have the following entity class (in Groovy): import javax.persistence.Entity import javax.persistence.Id import javax.persistence.GeneratedValue
I have the following classes: package models; public class Test extends activejdbc.Model { }
I have the following two classes: package { import flash.display.Sprite; import flash.events.Event; public class
I have following Java code: import java.io.*; class Global{ public static int a =
I have the following two classes: import java.io.*; import java.util.*; public class User {
I have the following classes: public class Foo { [Import] public IFirstInterface firstImport; [Import]
Example: For(int i=0; i<4; i++) playSound(Sound.wav); I have the following classes: Main import java.io.IOException;
Suppose, I have following classes: public class DisposableObj : IDisposable { public ChildObj CreateObj();

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.