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

  • Home
  • SEARCH
  • 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 8379479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:11:55+00:00 2026-06-09T16:11:55+00:00

I use JPA with Hibernate. I have a User class and a Flag class

  • 0

I use JPA with Hibernate.

I have a User class and a Flag class :

class User
{
    // ...
}

class Flag
{
    @OneToOne(optional=false)
    private User user;

}

I dont want/need a reference to the Flag object in the User class (and that’s the main point). But when I delete a User, I’d like the associated Flag (if there’s one) to be deleted too, without any foreign key constraint fails exception!

I know I could add a reference to the Flag inside the User, like :

class User
{
    @OneToOne(optional=true, cascade=CascadeType.ALL, mappedBy="user")
    @org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    private Flag flag;
}

And then the Flag object would be deleted when I delete the user.

But is it possible to cascade delete the Flag without having a reference to it in the User?

If not, what if the correct way to delete such an object?

  • 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-09T16:11:57+00:00Added an answer on June 9, 2026 at 4:11 pm

    You can’t cascade the delete automatically in this case, you’ll need to implement your own delete.

    When you delete a user, first run a query like:

    delete from Flag F where F.userId = :userId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use JPA and Hibernate for my project. I have two classes with same
I use JPA, and Hibernate as its implementation. What maven2 dependencies I need to
I use JPA + Hibernate. I have 2 entities (Parent, Child) with OneToMany connection
I use JPA 2 with Hibernate Entity Manager 3.6.4. Once I have marked my
i have the following problem. I use Spring and JPA (Hibernate) to persist Data
I use: EJB3 / JPA (Hibernate) MySQL 5 I have to set up a
I want to use the Hibernate Search full text search capabilities. I have a
Usually I'm a Hibernate user and for my new project we use JPA 2.0.
As I read in a lot of articles, when I use JPA/Hibernate query it
Is there any way I can use JPA or hibernate annotations to specify the

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.