spring-data-relational icon indicating copy to clipboard operation
spring-data-relational copied to clipboard

Add support for @JoinColumn

Open rfelgent opened this issue 1 year ago • 12 comments

Hi,

I love the simplicity of spring-data-jdbc, but sometimes it feels too restrictive compared to possibilities of JPA. I miss the @JoinColumn feature.

Please let me describe my need for it

class Order {
   @Id
   String id;                        // technically, randomly generated value without _any_ business meaning, e.g. UUID
   String reference ;           // some kind of business information encoded in the value and it is _unique_ (!)
   @MappedCollection
   Set<OrderItem> orderItems;
   // might be a better name compared to @JoinColumn
   //@MappedColumn
   @JoinColumn("reference")  // <== not using the "id" field
   private Meta meta;
}

class OrderItem {
   @Id
   Long id;                      // technical id
   String orderId;            // back reference to Order
}

class Meta {
   @Id
   String reference;
   String data;
}

I have use cases, where relations are either based on that unique field/property/column or on another unique field/property/column. I tried to use the NamingStrategy to achieve my goal, but it didn't work either.

best regards

rfelgent avatar Nov 28 '24 10:11 rfelgent

Hmmm... @schauder , is there anything I can do in order to push my Feature request?

rfelgent avatar Dec 01 '24 13:12 rfelgent

Most of the time I'd say provide a PR, but currently I'm working on a feature that changes a lot of stuff in the areas that would be touched by a PR. Such a PR would therefore run into a lot of conflicts. But if you wait until said feature is done, a PR would be welcome, but probably quite challenging.

schauder avatar Dec 02 '24 07:12 schauder

Of course I can wait @schauder.

The first question for me: is the feature request itself reasonable ? what do you think?

rfelgent avatar Dec 02 '24 13:12 rfelgent

Yes, I do think this is absolutely reasonable.

schauder avatar Dec 02 '24 13:12 schauder

I am new to Spring Data JDBC. If there is support for @JoinColumn based on a non-primary column, we can technically use it for Many to Many and Many to One. Right? Please correct me If I have misunderstood the requirement.

kap199297 avatar Dec 11 '24 07:12 kap199297

I am new to Spring Data JDBC. If there is support for @JoinColumn based on a non-primary column, we can technically use it for Many to Many and Many to One. Right? Please correct me If I have misunderstood the requirement.

There is no support for @JoinColumn but, even when there eventually is, there will be no support for Many-to-Many nor Many-to-One. See https://spring.io/blog/2018/09/24/spring-data-jdbc-references-and-aggregates for details.

schauder avatar Dec 11 '24 08:12 schauder

@schauder Thanks for providing the references. I got the idea of aggregates with root entities. But I have one query if you are happy to answer it. Just consider any scenario of employee and asset where there is no foreign key relationship between tables and both have a common column saying employerCode.

class EmployerUser {
   @Id
   String id;                        
   String employeeCode;    // UniqueEmployeeCode, This can be null for contractors/any other type of users
   @JoinColumn("employeeCode") // Join Column reference to Asset Table
   Set<Asset> assets;
}

class Asset {
   @Id
   String id;
   String assetCode;
   String employeeCode; // No foreign key relationship
   String name;
   @JoinColumn("employeeCode") // Join Column reference to Asset Table
   @ReadOnlyProperty
   EmployerUser assets;
}

I know, it is not possible to persist data based on these mappings. But technically, we can get the data from entity mapping the common column if @JoinColumn is implemented in the future. Please correct my understanding if I have misunderstood anything. Thanks in advance. 😊

kap199297 avatar Dec 14 '24 11:12 kap199297

No. A mapping like this will still either cause a stackoverflow or some custom error due to a recursive definition of an aggregate. JoinColumn will not change the structure of mappings that are possible. Instead it will only change, which id to be used for a back reference.

schauder avatar Dec 16 '24 08:12 schauder

Thanks @schauder, I got your point related to the bi-directional mapping.

I got your answer from stack overflow that as per Spring boot JDBC implementation, it will fetch all information at the same time and doesn't have Lazy Loading principle. 😊

kap199297 avatar Dec 16 '24 08:12 kap199297

Most of the time I'd say provide a PR, but currently I'm working on a feature that changes a lot of stuff in the areas that would be touched by a PR. Such a PR would therefore run into a lot of conflicts. But if you wait until said feature is done, a PR would be welcome, but probably quite challenging.

Hi @schauder , what is the current status. I would like dive into making a merge request.

rfelgent avatar Jan 26 '25 04:01 rfelgent

Once this PR is done https://github.com/spring-projects/spring-data-relational/pull/1957 I'm happy to look at PRs for JoinColumn. Of course, you could start working on a solution based on that PR hoping that it doesn't change to much before getting merged.

schauder avatar Jan 29 '25 12:01 schauder

@rfelgent PR #1957 is done, so I think it should be possible to create a PR for @JoinColumn without too many conflicts.

denniseffing avatar Aug 29 '25 13:08 denniseffing