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

8
  • 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 Commented Jun 11, 2018 at 10:25
  • What is your goal with dynamic tables? Commented Jun 11, 2018 at 10:28
  • Looks like an XY problem. What problem exactly are you trying to solve? Commented Jun 11, 2018 at 10:35
  • @Stefan please refer edit question. Commented Jun 11, 2018 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 Commented Jun 11, 2018 at 10:39

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

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
@AKAggarwal Please remove @MappedSuperclass in from Test Class.Because @MappedSuperclass and @Entity not work together.
Thanks for your Help.
@AKAggarwal your Welcome.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.