Spring Data JPA generated SQL of one-to-one self reference query methods has changed

Viewed 120

Technologies:

  • Spring-Boot
  • Maven
  • JPA

I have an entity which contains a one-to-one bidirectional self reference:

@Entity
public class Example {

  @Id
  @GeneratedValue
  Long id;

  @Setter
  String title;

  ...

  @OneToOne
  Example earlierVersion;

  @Setter
  @OneToOne(mappedBy = "earlierVersion")
  Example laterVersion;

}

What I am interested in are the latest versions of Examples. Therefore I am requesting via Spring JPA query methods:

List<Example> findByLaterVersionIsNull();

This is working well with Spring Boot version 1.5.2.REALEASE (maven).

But after a change to Spring Boot version 2.0.0 or higher it seems that the generated SQL which gets executed does not contain the necessary LEFT OUTER JOIN anymore.

E.g.:

Version 1.5.2.RELEASE

select ...
from example example0_
left outer join example example1_ on example0_.id=example1_.earlier_version_id
where (example1_.id is null) ?

After Version 2.0.0.RELEASE

select ... 
from combination combinatio0_ 
where (combinatio0_.id is null)

Of course the later query doesn't make sense at all, since id is the primary key.

So I am pretty sure I did something wrong or just did not have realized that something important has changed. But after researching a lot this will be my first post to this great community to get support.

Thanks in advance!

1 Answers

This looks like https://jira.spring.io/browse/DATAJPA-1418

Which was fixed and backported to the following Spring Data JPA releases:

  • 2.2 M1 (Moore)
  • 2.1.2 (Lovelace SR2)
  • 2.0.12 (Kay SR12)

If you use a boot version that uses one of these releases or a later Service Release the issue should go away.

You can also explicitly specify such a JPA version to use.

Related