File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes need help understanding annotation for EJB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "need help understanding annotation for EJB" Watch "need help understanding annotation for EJB" New topic
Author

need help understanding annotation for EJB

Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help understanding annotation for EJB
 
Similar Threads
EJB 3.0 Statefull & Stateless Cant see the Difference
Confusion between PreDestroy and Remove annotation
EJB 3 TransactionTimeout annotation needed
Is it possible to initialize a no-interface Stateful bean through annotation?
Stateful Web Service