Search code examples
javaspring-datamappingone-to-onespring-data-jdbc

Spring Data JDBC invert OneToOne navigation


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


Solution

  • 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.