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 { }
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With