java JPA/EclipseLink - 检索列名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3657968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:53:23  来源:igfitidea点击:

JPA/EclipseLink - Retrieve Columns Names

javaormjpajpa-2.0

提问by Bob Rivers

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).

我正在尝试更新我在 Java 方面的知识,因为我上次使用它是在 1.4.X 版本中......我正在尝试使用 1.6.0,特别是 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...

但是我在鬼混,当我决定用表的列名填充 JList 并且没有成功时......

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,

TIA,

Bob

鲍勃

回答by Bozho

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 Columnannotation 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.

请注意,Column注释是可选的,因此您必须确保已定义它。如果不是,您将不得不咨询名称翻译机制,这对于此来说太过分了。

回答by James

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

在 EclipseLink 中,您可以获取类的 ClassDescriptor 并获取其字段 (DatabaseField)。

i.e.

IE

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

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

回答by Nikson Kanti Paul

Property and Column mapping from EclipseLink ClassDescriptor

来自 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.
  • getFields()返回在类中使用的所有底层列名。
  • getMappings()返回类字段名称及其数据库列字段名称。

回答by Pascal Thivent

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...

但是我在鬼混,当我决定用表的列名填充 JList 并且没有成功时......

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.

好吧,虽然您可以解析注释(如 Bozho 所指出的),但 JPA 的全部意义在于以某种方式从业务对象中抽象出表名和列名(甚至可以使用默认值,使信息甚至不存在)。换句话说,我不会依赖它们,而是使用类名和属性名。