Java – JPA / eclipse link – retrieve column names

I'm trying to update my java knowledge because I was last in 1.4 When used in version x... I'm trying to use 1.6 0, especially the Java persistence API (2.0)

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

But when I decided to fill JList with the column names of the table and failed, I was fooling

This is a simple class that 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 any way to retrieve column names?

I found this post Seems to be an effective solution, but I think it may have something easier / more elegant? I don't know why, but I look forward to the method that has been completed

TIA,

Short hair

Solution

You can resolve column comments:

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 the name translation mechanism, which is too much for this

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>