JPA / EclipseLink - 检索列名称 [英] JPA/EclipseLink - Retrieve Columns Names

查看:106
本文介绍了JPA / EclipseLink - 检索列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新我的Java知识,因为我上次使用的时候是1.4.X版本......我正在尝试使用1.6.0,特别是Java Persistence API(2.0)。

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.

但是当我决定使用表的列名称填充JList时,我正在愚弄没有成功...

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;

    ...
}

有没有办法检索列名?

我发现这个帖子。似乎是一个有效的解决方案,但我认为它可能有更容易/更优雅的东西?我不知道为什么,但我期待已经完成的方法...

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

推荐答案

您可以解析列注释:

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.

这篇关于JPA / EclipseLink - 检索列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆