• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need help understanding annotation for EJB

 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new in JDk5. have learned some annotation but still bit confused by its usage in EJB. For example,
1. what does compiler exactly do when it sees @Stateful ?
2. In order for @Stateful to be meaningful, do I have to do "import xxx.xxx.Stateful" first ?
3. IS it true that whenever you see a "@Stateful", it means there is an interface called "Stateful" and it is defined in the imported package already ?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj,
1) The compiler transfers the annotation to the Java byte code. Later the app server generates the EJB code. (Note there are other types of annotations that do not go into the byte code.)

2) Yes, you need to import the annotation for the code to compiler.

3) No. There is no longer an interface. @Stateful is an annotation. It is defined in the imported package; it's just not an interface.
 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at an example below:

@Entity
public class Customer implements java.io.Serializable { // line 1
private String firstName;

private String lastName;
@Id
private long ssn; // line 50

....
...
} // line 100

1. How do I tell where is "scope" for each annotation ? For example, how do you know that "@Entity" actually "covers" from line 1 to 100 ? And how do you know "@Id" covers only line 50 ? why doesn't "@Id" covers from line 50 to more lines ? How to exactly tell the scope for each annotation ?

2. Do "@Id" get translated into some code at compile time or what ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to exactly tell the scope for each annotation ?

It depends where you put the annotation. If you annotate a class (like @Entity), the whole class is affected. If you annotate a field, or a method, the field or the method will be respectively affected.
Each annotation cannot be put everywhere. Look at the definition for @Entity :

It means that the @Entity annotation can only be set to a type (like a class).
Now look at the @Id definition :

You can see that the @Id annotation can be set on methods, and on fields. Look at the J2EE API if you want to check other annotations.

2. Do "@Id" get translated into some code at compile time or what ?

You're asking about Java annotations in general. I think you're in the wrong forum You can read more on annotations here and here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic