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

Related Questions

last time I asked how to populate a data structure here . Now I
I'm downloaded the code from John Papa's book here: http://silverlight-data.com/ and am sucessfully running
ex: <a><strike>example data in here</strike></a> I want everything inside the a tag, to the
How can I represent the following in XSD. <price-update> <![CDATA[ arbitrary data goes here
Here is my Json data: [{ok : false, details : [{n : Email, v
Here is a sample code to retrieve data from a database using the yield
Here are two questions related to modifying the data source for strongly typed dataset
Here's the setup I have in a vs2008 solution: Data layer in a project
Here is some XAML <HierarchicalDataTemplate DataType={x:Type data:FolderEntity} ItemsSource={Binding Path=FolderEntities,UpdateSourceTrigger=PropertyChanged}> <Label Content={Binding FolderName}/> </HierarchicalDataTemplate> <TreeView/>
I need to define new UI Elements as well as data binding in code

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.