spring-projects / spring-projects/spring-data-relational

@MappedCollection => allow to configure table name?

Open
#1,603 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: pending-design-work type: enhancement
Dominant language
Java
Stars
827
Forks
394
PR merge metrics
No merged PRs in 30d

Description

First of all, thanks again for this great project/product!

I have a suggestion to be able to reuse classes when using @MappedCollection. Currently, when using @MappedCollection for referencing sub-entities, a table name cannot be specified.

This results in classes that can not be reused as the table name needs to specified on the sub-entity. Due to the volume/amount of rows, I would like to have each @MappedCollection sub-entity in a separate table, per entity.

Problem description

Imagine the following Link class that I would like to reuse in different Spring Data JDBC Entities.

public class Link {

    private final String module;
    private final UUID id;

    public Link(String module, UUID id) {
        this.module = module;
        this.id = id;
    }

    public String getModule() {
        return module;
    }

    public UUID getId() {
        return id;
    }

    public String asString() {
        return module + ":" + id;
    }

    @Override
    public String toString() {
        return asString();
    }
}

The only way to embed this in multiple entities is by creating subclasses:

@Table(name = "task_links")
public class TaskLink extends Link {

    public TaskLink(String link) {
        super(link);
    }

    public TaskLink(String module, UUID id) {
        super(module, id);
    }
}

This way, it can be used using a @MappedCollection in the Task entity.

@Table(name = "tasks")
public class Task {

    @Id
    private final UUID id;

    @Version
    private final int version;

    @MappedCollection(idColumn = "task_id")
    private final List<TaskLink> taskLinks;

    ...
}

For another entity, the same approach is needed.

Proposed solution

Instead of the subclass, I would like to suggest the following approach, which would remove the need for the subclassing:

@Table(name = "tasks")
public class Task {

    @Id
    private final UUID id;

    @Version
    private final int version;

    @MappedCollection(idColumn = "task_id", tableName="task_links")
    private final List<Link> taskLinks;

    ...
}

Extra context

I also tried with a custom NamingStrategy but I do not have enough context when the getTableName is called as it does not provide any information whether this is called from a MappedCollection and if so, from which entity.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the @MappedCollection annotation and the NamingStrategy entry point described in the issue, then trace how collection table names are resolved. Determine how a per-collection tableName option could support reusable Link classes and separate tables for each owning entity; done means the proposed annotation usage works without subclassing.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.