3

I'm trying to update my knowledge in Java, since I last used in when it was in 1.4.X version... I'm trying to use 1.6.0, in particular the Java Persistence API (2.0).

I managed to create an entity class. It's working, since I'm able to store and retrieve data.

But I was fooling around and when I decided to fill a JList with the column names of a table and didn't got success...

It's a simple class and looks like:

@Entity
@Table(name = "T_CURRENCY", schema = "APP")
public class Currency implements Serializable {
    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Short id;
    @Basic(optional = false)
    @Column(name = "NAME")
    private String name;

    ...
}

Is there a way to retrieve the columns names?

I found this post. Seems to be a valid solution, but I thought that it might have something more easier/elegant? I don't know why, but I was expecting an already done method...

TIA,

Bob

Community
  • 1
  • 1
Bob Rivers
  • 5,261
  • 6
  • 47
  • 59

4 Answers4

10

You can parse the column annotations:

for (Field field : entity.getClass().getDeclaredFields()) {
   Column column = field.getAnnotation(Column.class);
   if (column != null) {
      columnNames.add(column.name());
   }
}

Note that the Column annotation is optional, so you must make sure you have it defined. If not you will have to consult the name translation mechanism, which would be too much for this.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
10

In EclipseLink you can get the ClassDescriptor for a class and get its fields (DatabaseField).

i.e.

em.unwrap(Session.class).getDescriptor(Currency.class).getFields()

You can also get the mappings, table, or anything else you desire.

See, http://www.eclipse.org/eclipselink/api/2.1/org/eclipse/persistence/descriptors/ClassDescriptor.html

James
  • 17,965
  • 11
  • 91
  • 146
2

Property and Column mapping from EclipseLink ClassDescriptor

// Get the session object 

org.eclipse.persistence.sessions.Session session = 
     ((org.eclipse.persistence.internal.jpa.EntityManagerImpl) 
                 em.getDelegate()).getSession();

// Get your desire class descriptor and mapping list 

List<org.eclipse.persistence.mappings.DatabaseMapping> datamap = 
     (List) session.getDescriptor(YOUR_ENTITY_CLASS_NAME.class).getMappings();


for (org.eclipse.persistence.mappings.DatabaseMapping dm : datamap) {

  System.out.println(" Attribute name : " + dm.getAttributeName());    // Class field name 
  System.out.println(" Column name : " + dm.getField().getName());     // Database Column name                    

}
  • getFields() return all underlying column names, which is being used in the class.
  • getMappings() return the class field name with it's database column field name.
Nikson Kanti Paul
  • 3,394
  • 1
  • 35
  • 51
1

But I was fooling around and when I decided to fill a JList with the column names of a table and didn't got success...

Well, while you can parse the annotations (as pointed out by Bozho), the whole point of JPA is somehow to abstract table and column names from the business objects (that could even use defaults, making the information not even present). In other words, I wouldn't rely on them but rather use the class name and the attribute names.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • I was actually hoping that the Metadata API in JPA 2.0 will have something to offer, but as far as I understand it doesn't include Column information. – Bozho Sep 07 '10 at 12:07
  • Correct, the Metadata API does not include column info. – James Sep 07 '10 at 14:06