How do I figure out what class method is annotated with @Id?

Discussion in 'Java' started by laredotornado, Sep 22, 2011.

  1. Hi,

    I'm using Java 6 with the latest version of Hibernate. I have several
    POJOs, all of which have exactly one method annotated with
    "javax.persistence.Id". For example, one class has ...

    @Id
    @Column(name="ID")
    public Integer getID() {
    return ID;
    }

    Given a java.lang.Class, how do I figure out what method is annotated
    with the "@Id" annotation? Thanks, - Dave
    laredotornado, Sep 22, 2011
    #1
    1. Advertising

  2. laredotornado

    markspace Guest

    On 9/22/2011 11:42 AM, laredotornado wrote:
    > Hi,
    >
    > I'm using Java 6 with the latest version of Hibernate. I have several
    > POJOs, all of which have exactly one method annotated with
    > "javax.persistence.Id". For example, one class has ...
    >
    > @Id
    > @Column(name="ID")
    > public Integer getID() {
    > return ID;
    > }
    >
    > Given a java.lang.Class, how do I figure out what method is annotated
    > with the "@Id" annotation? Thanks, - Dave



    I think my first question is "Why do you want to do that?" It sounds
    like a very very bad idea. You shouldn't be messing around with those
    entities at all, let the persistence layer handle that. Your access to
    "id" should come from some higher level of business logic.

    If you must (not recommending):
    <http://download.oracle.com/javase/tutorial/reflect/index.html>

    <http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getAnnotations%28%29>
    markspace, Sep 22, 2011
    #2
    1. Advertising

  3. laredotornado

    Daniel Pitts Guest

    On 9/22/11 11:42 AM, laredotornado wrote:
    > Hi,
    >
    > I'm using Java 6 with the latest version of Hibernate. I have several
    > POJOs, all of which have exactly one method annotated with
    > "javax.persistence.Id". For example, one class has ...
    >
    > @Id
    > @Column(name="ID")
    > public Integer getID() {
    > return ID;
    > }
    >
    > Given a java.lang.Class, how do I figure out what method is annotated
    > with the "@Id" annotation? Thanks, - Dave


    Use Class.getDeclaredMethods and Method.isAnnotationPresent

    <http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getDeclaredMethods%28%29>

    <http://download.oracle.com/javase/6/docs/api/java/lang/reflect/AccessibleObject.html#isAnnotationPresent%28java.lang.Class%29>

    ----
    Daniel
    Daniel Pitts, Sep 22, 2011
    #3
  4. laredotornado

    markspace Guest

    On 9/22/2011 2:54 PM, Daniel Pitts wrote:

    > On 9/22/11 11:42 AM, laredotornado wrote:
    >> Given a java.lang.Class, how do I figure out what method is
    >> annotated with the "@Id" annotation? Thanks, - Dave


    >
    > Use Class.getDeclaredMethods and Method.isAnnotationPresent



    I'd prefer Class.getMethods. getDeclaredMethods won't look at inherited
    methods, which is not normally what you want.

    And you can skip isAnnotationPresent and just go for getAnnotations. It
    returns a 0 length array if no annotations are present. Your loop
    iterator should catch that by default, no need for additional checks or
    logic in the driving code.
    markspace, Sep 22, 2011
    #4
  5. laredotornado

    Daniel Pitts Guest

    On 9/22/11 3:30 PM, markspace wrote:
    > On 9/22/2011 2:54 PM, Daniel Pitts wrote:
    >
    >> On 9/22/11 11:42 AM, laredotornado wrote:
    >>> Given a java.lang.Class, how do I figure out what method is
    >>> annotated with the "@Id" annotation? Thanks, - Dave

    >
    >>
    >> Use Class.getDeclaredMethods and Method.isAnnotationPresent

    >
    >
    > I'd prefer Class.getMethods. getDeclaredMethods won't look at inherited
    > methods, which is not normally what you want.
    >
    > And you can skip isAnnotationPresent and just go for getAnnotations. It
    > returns a 0 length array if no annotations are present. Your loop
    > iterator should catch that by default, no need for additional checks or
    > logic in the driving code.
    >
    >
    >


    I've used getDeclaredMethods to catch non-public methods. getMethods()
    only returns public methods.

    You would have to "walk up" the super-class tree (I suppose getMethods()
    on an interface is good enough since there are no non-public methods
    except perhaps synthetics?
    Daniel Pitts, Sep 23, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven T. Hatton
    Replies:
    6
    Views:
    4,242
    Steven T. Hatton
    Apr 12, 2004
  2. Sakesun Roykiattisak
    Replies:
    1
    Views:
    241
    Christopher T King
    Aug 5, 2004
  3. Sarah Klaum

    Bind to annotated typed dataset

    Sarah Klaum, Apr 30, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    157
    Sarah Klaum
    Apr 30, 2004
  4. Don Y
    Replies:
    87
    Views:
    994
    Don Y
    Mar 9, 2012
  5. Laszlo Nagy
    Replies:
    1
    Views:
    88
    Ned Batchelder
    Nov 18, 2013
Loading...

Share This Page