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

L

laredotornado

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
 
M

markspace

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()>
 
D

Daniel Pitts

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

<http://download.oracle.com/javase/6...ect.html#isAnnotationPresent(java.lang.Class)>
 
M

markspace

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

Daniel Pitts

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top