2

Is there a way to import a value from properties file to class level annotation?

application.properties

native.query="Select * from Registration"

Registration.java

@NamedNativeQuery(name="RegQuery", resultSetMapping="RegResult",
    query = "${native.query}"
)
@Entity
public class Registration {
    ...
}

${native.query} is treated as a string in this case.

EDIT1

I cannot put this as an answer since it does not directly answer the question. Might as well put this here for reference.

As mentioned below the comment section, it is impossible to Import value of properties file value to a class annotation.

Since this is a native query, I retain the query inside properties file for easy environment switching.

application.properties

native.query="Select * from Registration"

Remove the @NamedNativeQuery

Registration.java

@Entity
public class Registration {
    ...
}

Import the property using @Value. (Better implement this query on DAL)

RegistrationService.java

@Service
public class RegistrationService {
    @Value("${native.query}")
    private String regQuery;

    @Autowired
    private EntityManager em;

    public getAllRegistered(){
        Query q1 = em.createNativeQuery(regQuery, "RegResult");
    }
}
jchips12
  • 1,177
  • 1
  • 11
  • 27
  • Possible duplicate of [How to import value from properties file and use it in annotation?](http://stackoverflow.com/questions/33586968/how-to-import-value-from-properties-file-and-use-it-in-annotation) – Nikita Artyushov Jan 19 '17 at 08:26
  • @NikitaArtyushov "Value" annotation can only be used inside the class because you need an attribute to contain the value. You can see in my question that I'm trying to import the property value outside the class. – jchips12 Jan 19 '17 at 08:33
  • How about create some class with static values and use it on your entity? like @NamedNativeQuery(name="RegQuery", resultSetMapping="RegResult", query = staticclass.query ) – Boldbayar Jan 19 '17 at 08:55
  • or do you need dynamic solution? – Boldbayar Jan 19 '17 at 08:55
  • I think you can't keep it in property file because annotation attributes must be a compile time constant. – Boldbayar Jan 19 '17 at 08:56
  • @Boldbayar that doesn't necessarily mean that you can't do it. Spring allows you to use SpEL in many cases (including annotations). It looks like Spring also support SpEL in `@Query` however it appears to be limited to certain things so I don't think you can provide an entire query using SpEL. – g00glen00b Jan 19 '17 at 09:22
  • @g00glen00b this maybe could help you dunno man, http://stackoverflow.com/questions/10802798/spring-data-jpa-query-with-parameter-properties. gl – Boldbayar Jan 19 '17 at 09:26
  • 2
    @Boldbayar that's what I meant, you can use SpEL, but it appears that it's only possible for parameters and some other cases, not for entire queries like the OP wants, so my guess is that the answer to this question is that it cannot be solved. – g00glen00b Jan 19 '17 at 09:40
  • 2
    You cannot use this. Spring allows property names an SpEL in some if its annotations but that support has to be backed into the annotation processing logic. You want to do this in JPA annotations and that support simply isn't there (nor will it ever be). So the answer is yu cannot do this for JPA annotations. – M. Deinum Jan 19 '17 at 09:59
  • @Boldbayar I tried the static field but its not compiling. – jchips12 Jan 22 '17 at 23:43
  • @M.Deinum and Boldbayar I have to agree with you that this might not work at all. – jchips12 Jan 23 '17 at 06:00

0 Answers0