Annotation
Annotation
5 Annotations and
EJB 3.0
15 Sep 2006
Gordon R. Cameron
Ken H. Stevens
JBoss World 2006
@Id @GeneratedValue(strategy=GenerationType.AUTO)
public int getId() {
return id;
}
@OneToMany(
cascade = CascadeType.ALL,
fetch = FetchType.EAGER,
mappedBy="order")
public Collection<LineItem> getLineItems() {
return lineItems;
}
}
Java 1.4 Annotations: JavaDoc
• @deprecated JavaDoc is now deprecated.
Use Java 1.5 @Deprecated annotation
instead.
• It’s looking like XDoclet will be replaced by
Java 5 annotations…
• It’s safer to generate production artifacts
from compiler/IDE aware annotations than
from comments.
Annotation Consumers
• “Tools” Only require SOURCE retention.
E.g. compilers, digest generators.
• “Introspectors” require RUNTIME
retention. Loads annotated classes and
interfaces into the VM for introspection.
Annotation Declaration
• Types
– Normal Annotation
– Single Member Annotation
– Marker Annotation
• No exceptions
• No inheritance
• Methods return primitive types, String,
Class, enum types, annotation types, or
arrays of the preceding types
Normal Annotation
public @interface RequestForEnhancement {
int id();
int priority
String synopsis();
engineer() default "[unassigned]";
String date() default "[unimplemented]";
}
@RequestForEnhancement(
id = 2868724,
synopsis = "Provide time-travel functionality",
engineer = "Mr. Peabody"
) public static void travelThroughTime(Date destination) { ... }
Single Member Annotation
public @interface Copyright {
String value();
}
@Copyright("2006 Intelliware")
public class OscillationOverthruster { ... }
Marker Annotation
public @interface Preliminary { }
@Target({TYPE,FIELD,METHOD,PARAMETER,
CONSTRUCTOR,LOCAL_VARIABLE})
@Retention(value=SOURCE)
public @interface SuppressWarnings {…}
@Target values
• TYPE
• FIELD
• METHOD
• PARAMETER
• CONSTRUCTOR
• LOCAL_VARIABLE
• ANNOTATION_TYPE
• PACKAGE
JSR-250 Common Annotations
• @Generated
• @Resource, @PostConstruct,
@PreDestroy
• @RunAs, @RolesAllowed, @PermitAll,
@DenyAll
Junit 4 Annotations
import org.junit.Test;
import junit.framework.TestCase;
@Test(expected=ArithmeticException.class)
public void divideByZero() {
int n = 2 / 0;
}
On the outside…
EJB 2.1
On the inside…
EJB 2.1
• Too much code. 5 files per entity.
• Rigid components:
– Can’t test outside the container
– Can’t serialize
– Can’t detach / reattach
– No inheritance
– No polymorphism
• No interceptors for Session Beans and
Message-driven Beans – resulting in lots of
repeated code, or reflection tricks
Poor Entity Bean Puppy can’t leave his Container…
The King…
Hibernate Entity POJOs are free to roam…
They can be serialized, detached, and reattached.
And polymorphed…
However, it is proprietary.
Proprietary solutions…
• Hibernate 2
• XDoclet
• Spring
• Tapestry
• JBoss 3
• There’s no standards compliant layer to
allow you to swap these tools out.
JSR-220 EJB3
JSR-244 Java EE 5
Forged from the revolution started
by The King…
Hibernate 3 is EJB3 compliant
JBoss 4+ is JEE5 compliant
(via Hibernate 3)
EJB3 Entity Beans
• Standardized ORM. Allow swapping of persistance
provider.
• Persistance is declared in annotations or XML. You
decide. Most stuff defaulted.
• Lets you do Java stuff. Support for inheritance and
polymorphism
• Just POJOs. Can test outside of container. Can detach,
reattach, and serialize.
• Improvement of EJB QL: Adding inner and outer join,
bulk update and delete, subqueries, and group by.
• Extended Persistence Contexts that live beyond the
current transaction.
• Embedded objects.
EJB3 Session and Message Beans
• Way less code: Elimination of component
interfaces (EJBObject, EJBLocalObject,
Remote), less checked exceptions. No more
required callbacks. No Home interface. Not in
your face anymore.
• EJB references are declared in annotations
• Interceptors for Session Beans and Message-
driven Beans
• Environmental / JNDI dependencies replaced
with annotations, injection, classpath lookup
• Most behaviours defaulted
SessionBean Example
@Stateless
@Remote(Calculator.class)
public class CalculatorBean implements Calculator {
public int add(int x, int y) {
return x + y;
}
public int subtract(int x, int y) {
return x - y;
}
}
@Resource(mappedName="DefaultDS")
private javax.sql.DataSource ds;
…JSF is to .NET
JSF Goals
• reusable UI toolkit for custom components
• simple data exchange
• UI state management
• event-driven
Built-in support for
• UI component state across requests
• Markup differences between different browsers
• Multi-page form processing
• Strongly typed events for server-side handlers
• Input validation & errors
• Automatic type conversion
• Helpful error / exception handling
• Navigation
Seam
• Built on EJB3 and JSF
• “Seamless” integration of presentation tier
to business tier
• Move web applications away from
stateless towards stateful
• Business-process oriented
• Designed from the ground up to be
testable.
Questions?
Java 1.5 Annotations