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");
}
}