1

I want to refer a field name in one class of java to another, such that I would get at least compiler error when someone changes the field name in base class.

Ex:

@Entity
class Employee {

    public String userId;
    public String timestampCreate;

}

class EmployeeReport {

    public Page<Employee> getEmployees() {

        return page = employeeJpaRepository.findAll(pageNumber, pageSize,
                Sort.by(Sort.Direction.DESC, "timestampCreate"));

    }

}

Here timestampCreate is the field name of Employee class.

I do not want to hardcode the column name; as it is bad practice and want it to be future proof, in case the field name changes. i.e. for example if someone changes the column name 'timestampCreate' to 'timestamp', then at lease I would like to see some compiler error in the EmployeeReport class.

Please suggest an idea. Thanks!

Note: My question is nowhere answered by the question they mapped and marked duplicate! I don't know why it was marked as duplicate and how to remove that!

A MJ
  • 115
  • 1
  • 9
  • 2
    Does this answer your question? [How to read the value of a private field from a different class in Java?](https://stackoverflow.com/questions/1196192/how-to-read-the-value-of-a-private-field-from-a-different-class-in-java) – SteffenJacobs Jan 08 '20 at 13:06
  • If I rename the field to `username` should it then be sorted by? If so: why / why not? What constitutes a valid field name change? You could add a static method `getTimestampFieldName` on `Employee` which returns `"timestampCreate"`. – luk2302 Jan 08 '20 at 13:06
  • What is `Sort`? Is it some class? What are the parameter types of `by` function? It doesn't look like a real code to me. I highly doubt that this method is meant to take name of the field to sort by as a `String`. What is more, think many times before using reflection - in most cases there is some problem with your design and reflection isn't the way to go. – Amongalen Jan 08 '20 at 13:25
  • @SteffenJacobs No, I am thinking if we can avoid hardcode usage of field name itself.. – A MJ Jan 08 '20 at 13:43
  • @luk2302 yes, using getTimestampFieldName in employee class is better, atleast issue will be with the same class. But I am also thinking if there is a way of using field name and compiler throws error, if the field name changes i.e. if someone changes 'timestampCreate' to 'timestamp', I wish compiler throw error, instead of runtime failure. – A MJ Jan 08 '20 at 13:47
  • @Amongalen It is an argument for Spring JPARepository query method. I just tagged reflection, but it neednot be through reflection only.. – A MJ Jan 08 '20 at 13:49
  • I think this question needs more fucus and better context @AMJ can you update your question instead put details inside comments, thanks. – Zydnar Jan 08 '20 at 14:27
  • Thanks @Zydnar for suggestion. I have edited and the question and details. – A MJ Jan 08 '20 at 15:50
  • 2
    What is this, stackoverflow marked my question as duplicate, where the other question is nowhere related to my question! – A MJ Jan 08 '20 at 16:01
  • Now that the fields in question are public, what problem are you having in accessing them? – Scott Hunter Jan 09 '20 at 16:58

0 Answers0