Given:
Hibernate 6.2.25.Final.
Entity 1:
@Entity
@Table(name = "chatRoom")
public class ChatRoom
{
@Id
private String id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "chatRoom",
orphanRemoval = true)
private Set<ChatRoomUser> chatRoomUsers;
private String info;
public ChatRoom()
{
this.id = UUID.randomUUID().toString();
}
}
Entity 2:
@Entity
@Table(name = "chat_room_user")
public class ChatRoomUser
{
@EmbeddedId
private ChatRoomUserId id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "chat_room_id", referencedColumnName = "id")
@MapsId("chatRoomId")
private ChatRoom chatRoom;
@Column(name = "info")
private String info;
}
Problem:
Now, if we get the ChatRoom entity and call em.refresh(chatRoom, LockModeType.PESSIMISTIC_WRITE) then Hibernate will also lock ChatRoomUser although chatRoomUser collection marked as LAZY.
While in hibernate 5.6.14 LAZY collection do not get locked.
I have created a test case GitHub - chernykhalexander/hibernate6EntityError: em refresh ignores the FetchType.LAZY where we can observe the behavior for the 2 versions of the Hibernate.
Question:
Could you please confirm that this is an intended behavior for the refresh method in Hibernate 6.x?