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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:16:34+00:00 2026-05-23T05:16:34+00:00

@Entity @Table(name=Student) public class Student { private String id; private String firstName; private String

  • 0
@Entity
@Table(name="Student")
public class Student {

    private String id;
    private String firstName;
    private String lastName;
    private Set<Grade> grades = new HashSet<Grade>();

public void addGrade(Grade grade){
        grades.add(grade);
    }
    //rest of Getters and setters omitted

    public void removeGrade(Grade grade){
    grades.remove(grade);
    }
@OneToMany(cascade = CascadeType.ALL,fetch=FetchType.LAZY,orphanRemoval=true)
    @JoinColumn(name="s_id")
    public Set<Grade> getGrades() {
        return grades;
    }

Grade:

@Entity
@Table(name="Grade")
public class Grade {
//all omitted
}

I have object student and I would like to remove one existing grade from the collection of graded on student with id ‘xyz’

I’m doing the following:

String id = "xyz";
Session session = sessionFactory.getCurrentSession();
    Student student = (Student)session.load(Student.class,id);
        student.removeGrade(grade);
        session.update(student);

Everything works flawlessly , but in SQL queries, I see weird update:

Hibernate: select student0_.id as id0_0_, student0_.FirstName as FirstName0_0_, student0_.LASTNAME as LASTNAME0_0_ from Student student0_ where student0_.id=?
Hibernate: select grades0_.s_id as s4_0_1_, grades0_.id as id1_, grades0_.id as id1_0_, grades0_.FirstQuestion as FirstQue2_1_0_, grades0_.TotalGrade as TotalGrade1_0_ from Grade grades0_ where grades0_.s_id=?
Hibernate: update Grade set s_id=null where s_id=? and id=?
Hibernate: delete from Grade where id=?

Why do I have update? update Grade set s_id=null where s_id=? and id=?

How can I skip it, in order to increase performance?

  • 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-23T05:16:35+00:00Added an answer on May 23, 2026 at 5:16 am

    It appears unnecessary, and it is in your case because your cascade type is ALL. However, there are some data models where it might make sense for the Child to be able to exist independently of being on the to-many relationship of another Entity. In other words, the update makes sense if you don’t want to delete Grade if you break the relationship from Student to Grade. In that case, hibernate should just nullify the foreign-key value, but not delete the Grade row.

    So this is what happens — when you explicitly break the relationship by removing the child from the collection on the parent, hibernate knows to update the child table to remove the foreign key. When your cascade setting are set up with the ‘ALL’ setting (it actually is probably the orphanRemoval=true), hibernate knows to delete the actual child row when it no longer has a parent. I suspect that hibernate is not optimized such that it recognizes when it can just go to delete.

    This was probably an optimization that they just chose not to implement.

    You can read about collection performance here. Particularly section 19.5.

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

Sidebar

Related Questions

My entity class: @Entity @Table(name = user) public class User implements Serializable { private
Consider the following code: @Entity @Table(name = a) public class A implements Serializable {
Hi every one I have these classe @Entity @Table(name = login, uniqueConstraints={@UniqueConstraint(columnNames={username_fk})}) public class
@Entity public class Store {} @Entity @Table(name = storeitem) public class StoreItem { @Id
I have code like: @Entity @Table(name = A) @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) public class A
Here is the BlogPost model : @Entity @Table(name = blog_posts) public class BlogPost extends
I have the following (simplified) Hibernate entities: @Entity @Table(name = package) public class Package
I have these two class(table) @Entity @Table(name = course) public class Course { @Id
@Entity @Table(name = your_entity) public class YourEntityClass implements IEntity<Long> { @Id @SequenceGenerator(name = gen_test_entity2_id,
I have the following inner class: @Entity @Table(name = SATTET0) public class SATTET0Key {

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.