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? |
|