0

I want to configure Hibernate to set maximum length for a VARCHAR field. This maximum length is defined in a configuration file, and this configuration file is loaded by a class ValidationConfiguration.

This is my entity:

@Entity
public class MyEntity{

    @Autowired /*I know this is bad practice, 
    I just want to let readers know that this object is instantiated.*/
    private ValidationConfiguration config;

        @Column(length = config.getMaxLength()) /*This gives a "java: element value 
        must be a constant expression"*/
        String description;

        //Get and set
    }

Is this possible? If not, are there any workarounds?

Magnus
  • 589
  • 8
  • 26
  • Possible duplicate [of](https://stackoverflow.com/questions/13363969/how-to-set-length-of-an-column-in-hibernate-with-maximum-length) – Mladen Savić Jul 13 '18 at 10:50
  • It's related, but not a duplicate. I read that question before I posted my own. – Magnus Jul 13 '18 at 12:18

1 Answers1

0

From your code, it is clearly visible, that You are just defining the ValidationConfiguration by private ValidationConfiguration config;
But You are not Instantiating the Object. So, Instantiate the object like new ValidationConfiguration() and since you haven't shared the code of ValidationConfiguration , i am predicting that your method is getMaxLength() not static. If the problem persists, do share the ValidationConfiguration code.

If that is a bean, then you can autowire it simply and don't create new instantiation.

Tahir Hussain Mir
  • 2,506
  • 2
  • 19
  • 26
  • You can assume the object is instantiated. I added an @Autowire now. However, I can't call any method at all inside the annotation. *Should* I be able to call a method of a member variable inside the annotation arguments? – Magnus Jul 13 '18 at 12:17
  • don't autowire it. Can you check on this statement private ValidationConfiguration config = new ValidationConfiguration(); – Tahir Hussain Mir Jul 16 '18 at 05:41
  • I tried adding `ValidationConfiguration config = new ValidationConfiguration();` inside the constructor, but to no avail. Inside of the annotation arguments, the compiler seems to treat `config` as a primitive type. I still get the "java: element value must be a constant expression". – Magnus Jul 16 '18 at 08:03