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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:32:51+00:00 2026-06-14T11:32:51+00:00

I have three tables in my database; an Author table, which has an id

  • 0

I have three tables in my database; an Author table, which has an id column, a Publisher table, which has an id column, and a Book table, which has an id column, an authorId foreign key column, to the Author id, and a publisherId foreign key column,to the Publisher id. The foreign key on update/on delete options are all set to ‘NO ACTION’, but from what I understand, Hibernate should be able to handle these itself.

The Author entity has a set of Books, which contain the publisher Id, but do not know about the Author. I’ve set this up as follows:

@Entity
@Table(name="author")
public class Author {
    private Integer id;
    private Set<Book> books;

    @Id @GeneratedValue
    public Integer getId() {
        return id;
    }

    @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL) {
    @JoinColumn(name="authorId")
    public Set<Book> getBooks() {
        return books;
    }

    public void setBooks(Set<Book> books) {
        this.books = books;
    }
}

@Entity
@Table(name="book") {
    private Integer id;
    private Integer publisherId;

    @Id @GeneratedValue
    public Integer getId() {
        return id;
    }

    @ManyToOne(FetchType.EAGER)
    @JoinColumn(name="publisherId")
    public Integer getPublisherId() {
        return publisherId;
    }

    public void setPublisherId(Integer publisherId) {
        this.publisherId = publisherId;
    }
}

My problem is that when I attempt to delete a Book from the Author’s set of books, Hibernate tries to update the authorId foreign key to null, and I get the error that the column authorId cannot be null in the Book table. What I want to happen is for that row to be deleted entirely.

I have also tried to add ‘orphanRemoval=true’ to the OneToMany annotation, and set on update/on delete to Cascade, but I get the same error.

Which annotations can I use to indicate that when a book is removed from the Author’s set of books, I want that row in the Book table to be removed? Is this possible at all with how I have my relationships set up and, if not, what is a better way to approach this?

  • 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-14T11:32:52+00:00Added an answer on June 14, 2026 at 11:32 am

    In the database is the AuthorId column set to NOT NULL? It could be that Hibernate is going to delete the row in two passes. The first pass nulls the foreign key reference, the second pass then deletes the row. I’m using NHibernate and have certainly seen similar behaviour. If this is the case, then you should be able to sort your problem out by setting the AuthorId to be NULLable.

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

Sidebar

Related Questions

i have three tables in my database book,author,publishing ... i want the user can
I have three mysql table from same database Db1. Three tables have following columns.
I have three tables in a database: Product table - +100000 entries Attribute table
I have three database tables: users emails invitations Emails are linked to users by
I have three tables in a Mysql database - countries, cities and hotels. Their
In my database I have three tables: Users: UserID (Auto Numbering), UserName, UserPassword and
I have a database with three tables in my database namely catering, room, room_booking
I have three or four tables in a MySQL database associated with an upcoming
I have a database with three tables: user_table country_table city_table I want to write
I have an issue in entity framework code first, three table in database as

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.