title: How to get Column names of JPA entity
body: <p>All my JPA entity classes implement an interface called Entity which is defined like this:</p>
  
  <pre><code>public interface Entity extends Serializable {
  // some methods
  
  }
  </code></pre>
  
  <p>Some of the fields of my JPA entity have <code>@Column</code> annotation on top of them and some don't. <code>MyEntity</code> class is defined like below:</p>
  
  <pre><code>@Entity
  public class MyEntity implements Entity {
  
     @Id
     private Long id; // Assume that it is auto-generated using a sequence. 
  
     @Column(name="field1")
     private String field1;
  
  
     private SecureString field2; //SecureString is a custom class
  
     //getters and setters
  
  }
  </code></pre>
  
  <p>My delete method accepts an Entity.</p>
  
  <pre><code>@Override
  public void delete(Entity baseEntity) {
  
     em.remove(baseEntity); //em is entityManager
  }
  </code></pre>
  
  <p>Whenever the delete method is invoked I want three things inside my delete method:</p>
  
  <p>1) Fields of <code>MyEntity</code> that are of type <code>SecureString</code></p>
  
  <p>2) Column name of that particular field in DB (The field may or may not have <code>@Column</code> annotation)</p>
  
  <p>3) The value of <code>id</code> field</p>
  
  <p>Note that when the <code>delete()</code> method is invoked, we don't know for which entity it is invoked, it may be for <code>MyEntity1</code>, <code>MyEntity2</code> etc.</p>
  
  <p>I have tried doing something like below:</p>
  
  <pre><code>for (Field field : baseEntity.getClass().getFields()) {
      if (SecureString.class.isAssignableFrom(field.getType())) {
          // But the field doesn't have annotation @Column specified
          Column column = field.getAnnotation(Column.class);
  
          String columnName = column.name();
      }
  }
  </code></pre>
  
  <p>But this will only work if the field has <code>@Column</code> annotation. Also it doesn't get me other two things that I need. Any ideas?</p>