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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:54:36+00:00 2026-05-11T20:54:36+00:00

Here is my data model (simplified), public class AddressBook { private List<Group> groups =

  • 0

Here is my data model (simplified),

public class AddressBook {
    private List<Group> groups = new ArrayList<Group>();
    private List<People> peoples = new ArrayList<People>();

    @OneToMany(mappedBy = "addressbook", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    public List<Group> getGroups() {
        return groups;
    }

    @OneToMany(mappedBy = "addressbook", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    public List<People> getPeoples() {
        return peoples;
    }
}


public class Group {
    private AddressBook addressBook;


    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    public void setAddressBook(AddressBook addressBook) {
        this.addressBook = addressBook;
    }
}

public class People {
    private AddressBook addressBook;
    private Group group;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    public AddressBook getAddressBook() {
        return addressBook;
    }
    public Group getGroup() {
        return group;
    }
}

I want to delete a full group from my addressbook, and all the people belonging to this group. So I do something like:

adressBook.getPeople().removeAll(peopleBelongingToGroupA);
adressBook.getGroups().remove(groupA);

But when my transaction is committed, Hibernate does first:

delete from groups where groupName='groupA';

Instead of deleting the people first. That causes my FOREIGN_KEY constraint between people and group to be violated.

Is there a way to tell hibernate to delete the people first, then the groups? Is there a flaw in my model?

  • 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-11T20:54:37+00:00Added an answer on May 11, 2026 at 8:54 pm

    Have you tried putting setting the cascade on each @ManyToOne. You’ve only specified, in many ways, cascade deletion on the AddressBook. This property is for each association I believe.

    The EJB3.0 specification is well worth having to hand when writing these beans. See http://jcp.org/en/jsr/detail?id=220

    Update:
    Reading your datamodel again, there may be a missing annotation on people here that would explain the behaviour. Do you have cascade set on the link with people->group? This would explain why the first statement would first start by trying to delete the group. Presumably you would want an annotation for groups on people that does not cascade?

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

Sidebar

Ask A Question

Stats

  • Questions 230k
  • Answers 230k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer That's easy: You compiled your application with a later version… May 13, 2026 at 1:59 am
  • Editorial Team
    Editorial Team added an answer I don't know whether I understoood your question correctly, but… May 13, 2026 at 1:59 am
  • Editorial Team
    Editorial Team added an answer Please read about the urllib2 password manager and the basic… May 13, 2026 at 1:59 am

Related Questions

Firstly, bear with me here. I have a custom model binder which is successfully
I'm passing data between my controller and my view using a ViewModel class. When
I have implemented what I thought was a pretty decent representation of MVC in
I have a situation in which I am attempting to keep my model and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.