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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:25:25+00:00 2026-05-31T19:25:25+00:00

I’m facing a problem while deleting a record that is referenced by another table.

  • 0

I’m facing a problem while deleting a record that is referenced by another table.

This is a code example (simplest scenario):

@Entity
@Table(name = "user")
public class User
{
    @Id
    @GeneratedValue(generator="user_id_seq", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name="user_id_seq", sequenceName="user_id_seq")
    @Column(name = "id", nullable = false)
    private int id;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private Set<UserLog> userLogSet;

    // Other attributes
    // Getters/setters
}

@Entity
@Table(name = "user_log")
public class UserLog
{
    @Id
    @GeneratedValue(generator="user_log_id_seq", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name="user_log_id_seq", sequenceName="user_log_id_seq")
    @Column(name = "id", nullable = false)
    private int id;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

    // Other attributes
    // Getters/setters
}

Whenever I try to delete a record from the “user” table, I get the following error from postgreSQL:

ERROR: update or delete on table “user” violates foreign key
constraint “fk228a019fd2495d20” on table “user_log”
Detail: Key
(id)=(5) is still referenced from table “user_log”.

What am I doing wrong?

Thank you!

Update: I tried coding a small example, it can be found here. After launching the application with Tomcat the database schema is automatically created (check the connection details in /src/main/webapp/WEB-INF/jdbc.properties). When you visit http://localhost:8080 some data is inserted in the database. If I try to delete any teacher who is referenced in the student table (using phpPgAdmin), I get an error similar to the one above.

  • 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-31T19:25:26+00:00Added an answer on May 31, 2026 at 7:25 pm

    Your mapping seems to be wrong.

    User can have many UserLogs I guess.

    Also @OneToMany should be on a collection e.g. java.util.Set.
    The cascade you have put is in UserLog which will take place if you act on instance of UserLog and not User.

    As far as I understand change mapping as follows.

    @Entity
    @Table(name = "user")
    public class User
    {
        @Id
        @GeneratedValue(generator="user_id_seq", strategy = GenerationType.SEQUENCE)
        @SequenceGenerator(name="user_id_seq", sequenceName="user_id_seq")
        @Column(name = "id", nullable = false)
        private int id;
    
        @OneToMany(cascade = CascadeType.ALL,mappedBy="user")
        private Set<UserLog> userLogs;
    
        // Other attributes
        // Getters/setters
    }
    
    
    @Entity
    @Table(name = "user_log")
    public class UserLog
    {
        @Id
        @GeneratedValue(generator="user_log_id_seq", strategy = GenerationType.SEQUENCE)
        @SequenceGenerator(name="user_log_id_seq", sequenceName="user_log_id_seq")
        @Column(name = "id", nullable = false)
        private int id;
    
        @ManyToOne
        @JoinColumn(name = "user_id", nullable = false)
        private User user;
    
        // Other attributes
        // Getters/setters
    }
    

    I guess you want to delete User and all its UserLog simply deleting User and leaving UserLogs doesn’t make sense.

    Hope this helps.

    Update:

    Cascades as annotated in entity classes are managed by ORM, not if you
    externally try to delete them from external tools like phpPgAdmin.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.