4

I am trying to set column name dynamically in a class(given below) but in @Column it need constant value as name.

public class Common
 {
  final String pre_col_name_created;

  public Common( String pre_col_name )
    {
    this.pre_col_name_created = pre_col_name;
    }

  @Column( name = pre_col_name_created + "" )
  private String created;
}

above code give me error: Attribute value must be constant

Please suggest me to give pre_col_name_created value dynamically from other class in @Column.

I already refer below links:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23

my goal is: I am creating 10 tables and all tables contain created column but prefix with different value that set as per Data model class.

for ex.: tables abc and qwe table abc has id,a_column(varchar) and table qwe has id, q_column(varchar)

Using @Transient I got error:

transient error

AKA
  • 618
  • 1
  • 5
  • 12
  • 1
    I think it is not possible to do so, but, you may to use some VO Class (like sub class) and then map it to entity you need, just a tip – Sh. Pavel Jun 11 '18 at 10:25
  • What is your goal with dynamic tables? – Stefan Jun 11 '18 at 10:28
  • Looks like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What problem exactly are you trying to solve? – Seelenvirtuose Jun 11 '18 at 10:35
  • @Stefan please refer edit question. – AKA Jun 11 '18 at 10:37
  • 1
    Variables in annotations must be constant. There can't be instances of a class with different annotated values. Use, as already mentioned subclasses and override the attributes – Lino Jun 11 '18 at 10:39
  • @Lino Please elaborate your answer. – AKA Jun 11 '18 at 10:42
  • Use an embeddable or sub classes. There is no way to have only one JPA entity that maps to several tables. – Seelenvirtuose Jun 11 '18 at 10:42
  • @AKAggarwal The answer is: What you try to do is just impossible, due to the constraints of the language itself. You **have** to create a new class for every table/entity you want to map. You maybe want to have a look at `@MappedSupperclass` and `@AttributeOverrides` – Lino Jun 11 '18 at 10:47

2 Answers2

1

Simply you cannot, hibernate mapping will be evaluated when initializing the datasrouce beans which is in the startup of your application.

Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43
1

Below code is solution for you:

Test.java

@Entity
@Filter(
    name = "tenancyFilter",
    condition = "et_created = :created"
)
@AttributeOverride(
    name = "created",
    column = @Column(
        name = "et_created"
    )
)
public class Test extends Common
{
    @Id
    @Column( name = "comp_id" )
    private UUID id;

    public UUID getId()
    {
        return id;
    }

    public void setId( UUID id )
    {
        this.id = id;
    }
}

Common.java

@MappedSuperclass
@EntityListeners( { AuditingEntityListener.class})
@FilterDef(
    name = "tenancyFilter",
    parameters = {@ParamDef(
        name = "created",
        type = "timestamp"
    )}
)
public class Common
{

    private Timestamp created;

    public Timestamp getCreated()
    {
        return created;
    }

    public void setCreated( Timestamp created )
    {
        this.created = created;
    }
}

In above code there is Test class which you can use as classes where you want to change name of column and In class Common you can define type of common column you want.

Below is screenshot of Database: screenshot

I am waiting for you comment.Thanks

ankit
  • 2,591
  • 2
  • 29
  • 54
  • Thanks Ankit but i am getting error: `org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: An entity cannot be annotated with both @Entity and @MappedSuperclass: com.postgresspringbootgradle.postgresspringbootgradle.entity.Test` – AKA Jun 13 '18 at 07:13
  • 1
    @AKAggarwal Please remove `@MappedSuperclass` in from Test Class.Because `@MappedSuperclass` and `@Entity` not work together. – ankit Jun 13 '18 at 07:15
  • Thanks for your Help. – AKA Jun 13 '18 at 07:16
  • 1
    @AKAggarwal your Welcome. – ankit Jun 13 '18 at 07:16