When I add an @OneToOne mapping on the non-owning side (User), I am getting extra selects loading Staff when performing a query. Once I remove the @OneToOne in the user class, N+1 problem is resolved.
User.staff is set to lazy, so it should not be trying to load that.
Anything I'm missing, or is this always the case with bidirectional associations?
/* named HQL query Staff.findByName */
/* load com.xxx.Staff */
/* load com.xxx.Staff */
/* load com.xxx.Staff */
PS. A user is not necessarily a staff member, but staff has to be a user.
Staff class
@Entity
@Table(name = "staff")
@NamedQueries({
@NamedQuery(name = Staff.QUERY_BY_NAME, query = "from Staff s left join fetch s.user u left join fetch u.staff where (lower(s.user.displayName) like :key) or (lower(s.user.lastName) like :key)")})
public class Staff extends AbstractDomainObject<Long> {
public static final String QUERY_BY_NAME = "Staff.findByName";
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
private User user;
User Class
@Entity
@Table(name = "env_user")
@SecondaryTable(name = "app_user")
public class User extends AbstractUserDetailsDomainObject<Long> {
public static final String QUERY_BY_USERNAME = "User.findByUsername";
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(optional = true, fetch = FetchType.LAZY, mappedBy = "user")
private Staff staff;
}