Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
961 views
in Technique[技术] by (71.8m points)

java - JPA/EclipseLink - Retrieve Columns Names

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...