title: How to reference a method or property in Java?
body: <p>I am rather new to Java. I'd like to know if it's possible to reference a method or property in Java. This is not specific to Hibernate ORM but just to give you an idea, the following example is written as Hibernate entity classes.</p>
  
  <pre class="lang-java prettyprint-override"><code>@Entity(name = "Person")
  public static class Person {
  
      @Id
      private Long id;
  
      private String name;
  
      @OneToMany(mappedBy = "author")
      private List&lt;Book&gt; books = new ArrayList&lt;&gt;();
  }
  
  @Entity(name = "Book")
  public static class Book {
  
      @Id
      private Long id;
  
      private String title;
  
      @NaturalId
      private String isbn;
  
      @ManyToOne
      private Person author;
  }
  </code></pre>
  
  <p>The <code>Person::books</code> property has an <code>OneToMany</code> annotation with <code>mappedBy</code> set to <code>"author"</code>, which eventually references the <code>Book::author</code> property at compile time. I believe this is possible thanks to Java's powerful reverse-engineering features a.k.a <code>Reflection</code>. This is great, but to make things even easier and debuggable, instead of putting a <strong>String</strong> value as the <code>mappedBy</code> value, I would like to use put a reference to the <code>Book::author</code>. This will ensure we're not referencing an invalid property on the fly (IDE-friendly). If we change the string to <code>"authors"</code> by mistake we would not know it's invalid unless we compile the code.</p>
  
  <p>So the preferred code for the <code>OneToOne</code> annotation would look like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>@OneToMany(mappedBy = Book::author)
  private List&lt;Book&gt; books = new ArrayList&lt;&gt;();
  </code></pre>
  
  <p>I believe method referencing exists in <strong>Objective-C</strong>. (and <strong>JavaScript</strong> too in a hacky way).</p>
  
  <p>As I said, the question is if Java has a method/property referencing feature or any hacks to do this? It's not specific to Hibernate ORM. I am talking about a language feature in general.</p>