I have two entities:
@Entity
@Table(name = "Something")
public class SomethingEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SOMETHING_SEQ")
@SequenceGenerator(sequenceName = "SOMETHING_SEQ", allocationSize = 1, name = "SOMETHING_SEQ")
@Column(name = "SOMETHING_ID", precision = 18, scale = 0, nullable = false)
private Long somethingId;
@Column(name = "SIMPLE_FIELD", length = 3, nullable = true)
private String simpleField;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "somethingId")
private OtherEntity other;
}
@Entity
@Table(name = "OTHER")
public class OtherEntity implements Serializable {
@Id
@OneToOne
@JoinColumn(name = "SOMETHING_ID")
private SomethingEntity somethingId;
@Column(name = "OTHER_SIMPLE_FIELD", precision = 18, scale = 2, nullable = false)
private Double otherSimpleField;
}
I want Spring-Data to fetch otherEntity
eagerly. I know it doesn't work by default, so my repository method is:
@EntityGraph(attributePaths = {"other"})
List<SomethingEntity> findAll(Predicate predicate);
It doesn't work and my guess is, it's because of @OneToOne
. I also tried JOIN FETCH, simple select by simpleField
(predicate in my example is QueryDSL), nothing works.
The most surprising thing is that the initial SQL is actually correct - it contains correct joins and selects correct fields. Then Spring-Data is loading each @OneToOne
relation by id anyway, causing 100 queries instead of 1.