Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Spring Boot can I use the same Entity for multiple tables with the same structure

I have multiple tables with different names but they have the same structure. These tables are created on the fly so I don't know in advance their names. Is it possible to have a single Entity to work with all of these Tables? Something similar when you can omit schema so that I can also omit table and specify Table Name on the fly.

@Data
@Entity
@Table(name = "APPLICATIONS", schema = "AIM")
public class Application2 { }
like image 537
ivoronline Avatar asked Dec 09 '25 16:12

ivoronline


1 Answers

You can create a base entity and inherit your other entities from it. This would mean that everything that's common would be implemented in the base entity class and everything that differs would be an override and/or additional feature in the subclasses.

See this article which speaks about the creation of a base entity for all entities that have an Id as well as it provides a specific example for audits. Here's the example they are giving for audits:

import jakarta.persistence.Column;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.MappedSuperclass;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;

@Getter
@Setter
@MappedSuperclass
public abstract class BaseEntityAudit extends BaseEntity implements Serializable {
    private String createdBy;
    private String updatedBy;

    @CreationTimestamp
    @Column(name = "created_at", updatable = false)
    private Date createdAt;

    @UpdateTimestamp
    @Column(name = "updated_at")
    private Date updatedAt;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof BaseEntityAudit)) return false;
        if (!super.equals(o)) return false;
        BaseEntityAudit that = (BaseEntityAudit) o;
        return createdBy.equals(that.createdBy) &&
                updatedBy.equals(that.updatedBy) &&
                createdAt.equals(that.createdAt) &&
                updatedAt.equals(that.updatedAt);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(),
     createdBy, updatedBy, createdAt, updatedAt);
    }

    @Override
    public String toString() {
        return "BaseEntityAudit{" +
                "createdBy='" + createdBy + '\\'' +
                ", updatedBy='" + updatedBy + '\\'' +
                ", createdAt=" + createdAt +
                ", updatedAt=" + updatedAt +
                "}" +
                super.toString();
    }
}

Notice that:

  • it is abstract, which is not necessary, but it makes sense, unless you have a root entity type. So if you have Person and then want other tables for certain kinds of persons, then you may choose not to have it abstract, but if there is no main, default kind of them, then it's best left as is, abstractly
  • it does not specify which table it refers to, because it's abstract, of course you can work with concrete base types instead if that fits your need better

And here's an entity class inherited from the base entity type:

import com.example.entity.common.BaseEntityAudit;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Getter
@Setter
@Table(name = "users")
public class User extends BaseEntityAudit {

    @Column(name = "name")
    private String name;

    @Column(name = "email")
    private String email;

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\\'' +
                ", email='" + email + '\\'' +
                '}' +
                super.toString();
    }
}

If your tables really only differ in their name, then the entity classes you will have will have their own table specification, the class declared as extending the base entity class and that's it. But you have the option to have custom functionalities of course, like in the example above.

like image 129
Lajos Arpad Avatar answered Dec 11 '25 06:12

Lajos Arpad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!