N+1 select problem with Hibernate 6.3.1

I tried to experiment with the examples:
Student.class

@Entity
@NamedEntityGraph(
       name = "Student.guide",
       attributeNodes = {
             @NamedAttributeNode("guide")
       }
)
public class Student {
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="ID") 
    private Long id;
    
    @Column(name="enrollment_id", nullable=false)
    private String enrollmentId;   
    
    private String name;
    
    @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name="guide_id")
    private Guide guide;
...

Guide class:

@Entity
public class Guide {
 
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    @Column(name="ID") 
    private Long id;
    
    @Column(name="staff_id", nullable=false)
    private String staffId;    
    
    private String name;
    private Integer salary;
 
    @OneToMany(mappedBy="guide", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private Set<Student> students = new HashSet<Student>();    
...

Client class:

public class MergeDetachedClient2 {
    public static void main(String[] args) {
 
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello-world");
        EntityManager em = emf.createEntityManager();
 
        em.getTransaction().begin();
 
        EntityGraph<?> entityGraph = em.getEntityGraph("Student.guide");
        Map<String, Object> properties = new HashMap<>();
        properties.put("jakarta.persistence.fetchgraph", entityGraph);
 
        Student student = em.find(Student.class, 10L, properties);
        System.out.println(student);
        Guide guide = student.getGuide();
        System.out.println(guide);
        em.getTransaction().commit();
        em.close();
 
        guide.setSalary(4500);
        student.setName("Olga MiddleName1 Brown");
 
        EntityManager em2 = emf.createEntityManager();
        em2.getTransaction().begin();
 
        Student mergedStudent = em2.merge(student);
 
        em2.getTransaction().commit();
        em2.close();
 
    }
}

So, here I used EntityGraphs to solve N+1 select problem in EntityManager1 but when I merged student in EntityManager2 I got select statement (the red border in screenshot) which I can’t understand “What for is it?” Please, help me to understand it :). I thought when we use merge() method, one select statement will be issued to the database (with join operator to fetch related entities)

Hibernate ORM has to synchronize the Guide#students association with the database because you specified CascadeType.MERGE for that association with @OneToMany(mappedBy="guide", cascade = {CascadeType.PERSIST, CascadeType.MERGE}).

So when you merge a detached Student, Hibernate ORM will synchronize the state of that student and all associations transitively that are reachable via CascadeType.MERGE. This means the student’s guide has to be loaded and all of the students of that respective guide in order to merge the state.

Hibernate ORM has some heuristics when it comes to deciding the fetch style for associations and for some reason decided that it can’t join the Guide#students. I don’t know why that is, but it could be a bug. You can debug this in the lambda that is created in org.hibernate.loader.ast.internal.LoaderSelectBuilder#createFetchableConsumer.

Please try to create a reproducer with our test case template (https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) and if you are able to reproduce the issue, create a bug ticket in our issue tracker(https://hibernate.atlassian.net) and attach that reproducer.