0

I'm trying to set an attribute in an annotation, using Spring @Value, but I get Type mismatch: cannot convert from Value to String. Here is what I tried:

@Table(name = "myTable", catalog = @Value("${database.myCatalog}") )

Is it possible? And if yes, how to do it?

1 Answer 1

2

I think you are a little bit confused with how Spring uses that annotation.

As far as I know, the only way that annotation can only be set at field or method/constructor parameters.

Also, for Spring to resolve it, the POJO must be a Spring managed bean. That means that it must be defined in the Spring (Web)ApplicationContext implementation to be resolved.

Your question seems like you are annotating a JPA Entity which is not a Spring bean but a Class to be used by the JPA implementation that you are using (e.g. Hibernate).

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

1 Comment

You're right, of course an entity is not a Spring Bean. In that annotation, other than a literal value, I saw I can use a static final field from another class. I could have that field set from a constructor when my container starts, but the final requirement can't be relaxed. So if I want to configure that value outside source code the only solution that I found is declare two persistence contexts.

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.