Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@MappedCollection => allow to configure table name? #1603

Open
rdehuyss opened this issue Sep 6, 2023 · 0 comments
Open

@MappedCollection => allow to configure table name? #1603

rdehuyss opened this issue Sep 6, 2023 · 0 comments
Labels
status: pending-design-work Needs design work before any code can be developed type: enhancement A general enhancement

Comments

@rdehuyss
Copy link

rdehuyss commented Sep 6, 2023

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 6, 2023
@mp911de mp911de added status: pending-design-work Needs design work before any code can be developed type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: pending-design-work Needs design work before any code can be developed type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants