Skip to content

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

Open
@rdehuyss

Description

@rdehuyss

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions