0

I want to get the name of the getter of my entity property. I only have the database column name and I can also get the property. So either is obtaining the getter from the column name or from property. I have the following:

@JoinColumn(name = "CurrencyId")
public Currency getCurrency() {
    return currency;
}

I want to have getCurrency() As you can see the column name is CurrencyId. I would like to do something like:

Method method = getMethodFromColumnName(Class class, String columnName)
System.out.println("Getter is:" + method.getName())
Getter is: getCurrency

Maybe there is a hibernate trick to do this. Thanks in advance.

  • 1
    It should be possible to get a lot of information from Hibernate's internal model/mapping. Doing so might not be easy though. However, I feel a [xy-problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) so could you elaborate on what you're trying to achieve? – Thomas Mar 26 '19 at 14:06
  • This should help: https://stackoverflow.com/questions/2638590/best-way-of-invoking-getter-by-reflection – Not a JD Mar 26 '19 at 14:06
  • I'm curious in which case you need this ... – Ali Ben Zarrouk Mar 26 '19 at 14:24
  • Why do you even use `JoinColumn` and not `Column`? – f1sh Mar 26 '19 at 14:55
  • I have a column name and I want to invoke the getter of that column name. The thing is that I dont know what entity and what column name the user is going to choose. @Thomas – Alejandro Alcaraz Mar 26 '19 at 14:57
  • @f1sh I cant decide that. – Alejandro Alcaraz Mar 26 '19 at 14:58
  • 1
    Where does the user get the entity and column name from? Why does the column name even matter? You'd normally use the entity only, i.e. its name and the properties (and in that case the getter would be named straight forward). – Thomas Mar 26 '19 at 15:01

2 Answers2

0

You can use reflection to get access to all methods and check whether the column name matches:

Method getMethodFromColumnName(Class cls, String columnName) {
  for(Method m:cls.getMethods()){
    if(!m.getName().startsWith("get")) {
      //you can delete this if you don't want only getters
      continue;
    }
    JoinColumn annotation = m.getAnnotation(JoinColumn.class);
    if(annotation != null && annotation.name() != null){
      if(columnName.equals(annotation.name())){
        return m;
      }
    }
  }
  return null;
}

Of course this requires the annotation to be present on the getter, not the field or the setter.

This function returns the Method with the matching annotation.

f1sh
  • 11,489
  • 3
  • 25
  • 51
  • The annotation doesnt matter. I though about something like this, but then I can have a getter like getPurchaseOrder() and the cloumn name "POcolumn", so in the if comparison they won't be equals. – Alejandro Alcaraz Mar 26 '19 at 14:36
  • Why does the annotation not matter? That's where your column name is defined. And that's also what's being compared here, `annotation.name()` returns `CurrencyId` when applied to your example – f1sh Mar 26 '19 at 14:48
  • Note that annotations can still be overriden by XML or code – Thomas Mar 26 '19 at 14:58
0

the getter method is annotated with @JoinColumn(name="CurrencyId") so by using reflections in Java you can look for the method annotated with @JoinColumn or of course you can create another annotation for this by yourself, for example:

Method[] methods=Class.forName("name of the entity").getDeclaredMethods();
for (Method method : m) {
                if (method.getAnnotation(JoinColumn.class) != null) {
                    System.out.println("Getter is:" + method.getName());
                }
                break;
            }

Of course you can create your own annotation for this case, but you can use JoinColumn.

Ismail
  • 2,322
  • 1
  • 12
  • 26
  • this only checks if any of the class's method is annotated with `JoinColumn` and returns the first one, nothing else – f1sh Mar 26 '19 at 14:54