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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:23:16+00:00 2026-06-17T07:23:16+00:00

I have an entity A that has a collection of basic types (e.g. String

  • 0

I have an entity A that has a collection of basic types (e.g. String). I use such a mapping because the strings associated to each instance of A depend on A‘s lifecycle. If I want to remove an instance of A from the DB, I also want its associated Strings to be removed.

My mapping is as follows:

@Entity
public class A {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "name", nullable = false, unique = true)
    private String name;

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "AStrings", joinColumns = @JoinColumn(name = "id"))
    @Column(name = "strings", nullable = false)
    private Set<String> strings;
}

If I create an instance of A and add it some strings, then I can persist the instance using Session.save(myInstance). Both the instance of A and its associated Strings are persisted.

But, if I want to remove the same instance from the DB, using Session.createQuery("delete A a where a.name = ?").setString(0, name).executeUpdate(), I get a foreign key constraint error:

Cannot delete or update a parent row: a foreign key constraint fails

But, I would expect the associated Strings to be automatically removed before removing the A‘s instance, but it seems it’s not the case. I also didn’t found a way to specify cascade rules.

Is there something wrong with my configuration?

Thanks


EDIT: I’ve also tried using @Cascade(CascadeType.DELETE) on the field strings, and it still doesn’t help. By looking at the database, I don’t see any ON DELETE policy for the concerned foreign key.

Someone who had the same issue opened a JIRA: https://hibernate.onjira.com/browse/HHH-4301. A solution (or workaround) has to exist, I can’t be the only person that uses @ElementCollection.

I’ve solved the issue. I thought that deleting using Session.delete() or using an HQL query was equivalent, but it seems not. Using HQL query, the dependent objects are not automatically deleted, so I get a foreign key constraint error. Using Session.delete() solves the problem. Also, Hibernate doesn’t seem to use cascade functionality of the DB since I still don’t see any CASCADE policy in the generated DDL, it handles this internally.

  • 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-17T07:23:17+00:00Added an answer on June 17, 2026 at 7:23 am

    I’ve solved the issue.

    I thought that deleting an Entity using Session.delete() or using an HQL query was equivalent, but it seems not. Using HQL query, the dependent objects are not automatically deleted, so I get a foreign key constraint error as explained in the question.

    Using Session.delete() solves the problem. Also, Hibernate doesn’t seem to use cascade functionality of the DB since I still don’t see any CASCADE policy in the generated DDL, it handles this internally.


    For moderators:

    I’ve added my answer to the question in the past (as you asked) but since this is the answer that solves the issue and that no answer (from other users) has been posted, I think I should post it here as an answer.

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

Sidebar

Related Questions

I have an entity that has a collection in it. The collection is a
Suppose I have the following data structure: a Customer entity that has a collection
Say, i have an entity that has a history of operations as a collection.
I have an entity that has a state table associated with it. The state
I have an entity Roster that has a collection of players. public class Roster
I have a doctrine entity that has a collection of entities ( children ).
I have an entity that has a few relations. I need to clone this
Imagine you have an entity that has some relations with other entities and you
Let's say we have an entity that has attributes att1 and att2, where att1
I have an entity Company that has many Orders. I mapped these as following

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.