我具有以下实体关系。
实体及其关系的映射方式如下:
public class UserGroup {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", referencedColumnName = "user_id", nullable = false)
@Fetch(FetchMode.JOIN)
private User user;
@OneToOne(mappedBy = "userGroup", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@Setter(AccessLevel.NONE)
@Fetch(FetchMode.JOIN)
private UserRole userRole;
}
public class User {
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER)
@Cascade(org.hibernate.annotations.CascadeType.ALL)
@Fetch(FetchMode.JOIN)
private Set<UserGroup> userGroups;
}
public class UserRole {
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_group_id")
@Fetch(FetchMode.JOIN)
private UserGroup userGroup;
}
当我执行类似以下内容的简单查询时:
userGroupDao.get(id)
我看到一个休眠查询,它连接了usergroup,user,userRole。我只需要userGroup查询,因为user
和userRole
都标记有FetchType.LAZY
。这里有初始化问题吗?
这是休眠查询:
Hibernate: /* criteria query */ select ...<list of columns>... from user_groups this_ inner join users storeduser2_ on this_.user_id=storeduser2_.user_id left outer join user_roles storeduser3_ on this_.id=storeduser3_.user_group_Id where this_.id=?
Hibernate: select ...<list of columns>... user_groups usergroupn0_ left outer join user_roles storeduser1_ on usergroupn0_.id=storeduser1_.user_group_Id where usergroupn0_.user_id=?