2

I have an existing data scheme I'm reluctant to change. There are two entities/tables: parent and child, with parent having the foreign key column child_id. It's a 1-to-1 relationship.

The problem is: the magic behind the scenes expects the child table to have the foreign key column (the exception mentions a ...JOIN ON child.parent = parent.id). Is it possible to inverse this to match the existing scheme? (I know it is with hibernate, but I'd like to stay with JDBC).

Relevant code:

@Repository
public interface ParentRepository extends CrudRepository<Parent, Long>{
}
@Data
public class Parent {
    @Id
    private Long id;

    private Child child;
}
@Data
public class Child {
    @Id
    private Long id;
}

Somewhat related question: Spring Data JDBC invert OneToMany navigation

Tymur Gubayev
  • 468
  • 4
  • 14
  • it's very confusing (to me at least), but I'm trying to stick to `Spring Data JDBC` and your Links are to `Spring Data JPA`-documentation. If I add `implementation 'org.springframework.boot:spring-boot-starter-data-jpa'` as dependency (in my `build.gradle`), the repository stops working (with and without the `@Repository`). Removing `spring-boot-starter-data-jdbc`-dependency fixes it, but causes other trouble. – Tymur Gubayev Nov 28 '19 at 15:01
  • Regarding your comment see this question and answer: https://stackoverflow.com/q/58912292/66686 – Jens Schauder Nov 29 '19 at 07:04
  • @JensSchauder the comment was an answer to another comment, that's deleted. It had links to how one would do what I wanted, but using `jpa`. Just adding it as reference broke everything, so I abandoned that idea. – Tymur Gubayev Nov 29 '19 at 13:19
  • I understand that. But while you tried that approach you experienced a problem that is described and solved in the linked question. – Jens Schauder Nov 29 '19 at 20:37
  • Oh, apparently I needed a *really close* look at both the q&a and my console output, thanks for pointing it out. – Tymur Gubayev Nov 29 '19 at 23:21

1 Answers1

1

There is currently no support for this on the Spring Data JDBC side.

On option that comes to mind is to create a view that already performs the join and has instead of triggers to perform the correct actions on the Child table. You can then map the Child as embedded.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • A view is actually a viable option for 1:1 relationship (as was my original question). But now I have a follow-up problem: I have 1:n and want to navigate from the n-side of it to 1-side (in java), i.e. instead of `Parent` having `Set` I need the `Child` to have a `Parent` field. Is this also possible somehow? – Tymur Gubayev Nov 29 '19 at 13:26
  • That is a good question and the short answer is "Yes". For a proper answer please ask a proper question so others can find it too. 8-) – Jens Schauder Nov 29 '19 at 13:42
  • since you said it's a good one, here it is: https://stackoverflow.com/questions/59107015/spring-data-jdbc-invert-onetomany-navigation – Tymur Gubayev Nov 29 '19 at 14:56