JPA / EclipseLink - Get Column Names

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

I managed to create an entity class. It works as I can store and retrieve data.

But I was cheating, and when I decided to populate the JList with the column names of the table and did not get success ...

This is a simple class and looks like this:

@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 get column names?

I found this post . This seems to be the right solution, but I thought it might have something lighter / more elegant? I donโ€™t know why, but I was expecting a method already done ...

TIA

Bean

+2
java orm jpa
source share
4 answers

You can parse 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 ensure that it is defined. If not, you will have to consult with a name translation mechanism, which would be too big for that.

+9
source share

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 mappings, a table or something else that you need.

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

+9
source share

Display properties and columns 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 column names that are used in the class.
  • getMappings() return the name of the class field with the name of the field of the database column.
+2
source share

But I was cheating, and when I decided to populate the JList with the column names of the table and did not get success ...

Well, although you can parse annotations (as Bojo points out), the whole point of JPA is to somehow abstract the names of tables and columns from business objects (which can even use the default values, making the information not even present), In other words, I I wouldnโ€™t rely on them, but use class names and attribute names.

+1
source share

All Articles