[hibernate-issues] [JIRA] (BVAL-757) Lazy fetch type doesn't work for identical objects

Xset (JIRA) jira at hibernate.atlassian.net
Fri Jul 31 14:08:04 EDT 2020


Xset ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%3A2d33e13b-599f-4307-bedc-4709c2198945 ) *updated* an issue

Bean Validation ( https://hibernate.atlassian.net/browse/BVAL?atlOrigin=eyJpIjoiMzAzNDJkNzdmM2M2NGUyZmJhYjc3MmFjOGVjOGRhZWIiLCJwIjoiaiJ9 ) / Bug ( https://hibernate.atlassian.net/browse/BVAL-757?atlOrigin=eyJpIjoiMzAzNDJkNzdmM2M2NGUyZmJhYjc3MmFjOGVjOGRhZWIiLCJwIjoiaiJ9 ) BVAL-757 ( https://hibernate.atlassian.net/browse/BVAL-757?atlOrigin=eyJpIjoiMzAzNDJkNzdmM2M2NGUyZmJhYjc3MmFjOGVjOGRhZWIiLCJwIjoiaiJ9 ) Lazy fetch type doesn't work for identical objects ( https://hibernate.atlassian.net/browse/BVAL-757?atlOrigin=eyJpIjoiMzAzNDJkNzdmM2M2NGUyZmJhYjc3MmFjOGVjOGRhZWIiLCJwIjoiaiJ9 )

Change By: Xset ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%3A2d33e13b-599f-4307-bedc-4709c2198945 )

There is User Entity:

{code:java}@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;

...

@Column(nullable = false)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
public Set<UserPermission> permissions;
}{code}

There is permission entity Permission Entity :

{code:java}@Entity
@Table(name = "user_permissions", indexes = {@Index(unique = true, columnList = "id")})
public class UserPermission {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", referencedColumnName = "id")
public User user;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "target_user_id", referencedColumnName = "id")
public User targetUser;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "other_entity", referencedColumnName = "id")
public OtherEntity otherEntity;
}{code}

I use this code to fetch user entity User Entity :

{code:java}... get session
... open transaction

// fetching user
final User user = session.find(User.class, id);

// fetching permissions
final Set<UserPermission> userPermissions = user.permissions;

// foreach permissions
// HIBERNATE FETCHES HERE targetUser AND EVERYTHING CONNECTED TO THIS ENTITY, LOOKS LIKE FetchType.EAGER

for (UserPermission permission: userPermissions) {
// there is no code
}

... close transaction{code}

Look at the comment in code above:

{code:java}// HIBERNATE FETCHES HERE targetUser AND EVERYTHING CONNECTED TO THIS ENTITY, LOOKS LIKE FetchType.EAGER{code}

It means, that without any calls to targetUser, hibernate fetches it. That's really strange. This behavior really slows down performance of my application.

My assumption: Hibernate just can't proxy two similar objects in one entity (i mean, user and targetUser have the same class, hibernate cannot proxy both objects and thats why it fetches targetUser immediately)

How to solve this problem? Any suggestions?

( https://hibernate.atlassian.net/browse/BVAL-757#add-comment?atlOrigin=eyJpIjoiMzAzNDJkNzdmM2M2NGUyZmJhYjc3MmFjOGVjOGRhZWIiLCJwIjoiaiJ9 ) Add Comment ( https://hibernate.atlassian.net/browse/BVAL-757#add-comment?atlOrigin=eyJpIjoiMzAzNDJkNzdmM2M2NGUyZmJhYjc3MmFjOGVjOGRhZWIiLCJwIjoiaiJ9 )

Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.core&referrer=utm_source%3DNotificationLink%26utm_medium%3DEmail ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailNotificationLink&mt=8 ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100140- sha1:33445dc )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/hibernate-issues/attachments/20200731/1ea96610/attachment.html 


More information about the hibernate-issues mailing list