EJB 3 0 Johannes Carln Jan Vsterns Callista
EJB 3. 0 Johannes Carlén - Jan Västernäs Callista Enterprise AB Copyright Callista Enterprise AB, 2005 Page: 1
Vad tycker vi ? o Min ödmjukhet är byggd på 35 års framgångar ù Bert Karlsson o Det är min uppfattning. Och jag delar den. ù Alf Svensson o Kronan har flera mynt ù Gunnar Persson, Brynäscoach Copyright Callista Enterprise AB, 2005 Page: 2
Outline o Current Situation ( pre EJB 3. 0 ) o Java 5. 0 Annotations o EJB 3. 0 ( based on early draft – not complete ) o Demo o Conclusion Copyright Callista Enterprise AB, 2005 Page: 3
Current situation Copyright Callista Enterprise AB, 2005 Page: 4
Current situation o EJB used to be the ”silver bullet” o Many people disagree ù ù ù Need Application Server Deployment Descriptors Callbacks Too much overhead Testing is difficult Persistence model (Entity Beans) sucks Copyright Callista Enterprise AB, 2005 Page: 5
Current situation o EJB also bring values ù Declarative Transactions & Security, Threading, Resource Pooling etc ù May not be needed by all applications ù Component Programming model o Strong Open Source alternate products has emerged ù Spring – Io. C and much more ù Hibernate - persistence ù x. Doclet – javadoc-driven source/configuration files generation Copyright Callista Enterprise AB, 2005 Page: 6
Java 5. 0 Copyright Callista Enterprise AB, 2005 Page: 7
Java 5. 0 o Annotations o Generics ù Map<Person, Integer> points = new Hash. Map<Person, Integer> o Autoboxing ù points. put(jan, 5); o Enums ù enum Season { WINTER, SPRING, SUMMER, FALL } o Enhanced for-loop List<String> =. . for ( String s : list ) { System. out. println(s); } Copyright Callista Enterprise AB, 2005 Page: 8
Java 5. 0 Annotations definitions o Use @ to define @Unittest public Order. Impl. Test { @Web. Service public Order. Service @Async public ship. Order() o Can be used on class, interface, method parameter, field. o Annotation must be defined in an interface public @interface Unittest { o Can have arguments ù @Copyright(“Callista Enterprise AB”) o java. lang. annotation package Copyright Callista Enterprise AB, 2005 Page: 9
Java 5. 0 Annotations usage o Parse source code o Runtime via new Class, Method and Field class methods Class myclass = Order. Impl. Tes. class; for (Annotation a : myclass. get. Annotations()) { System. out. println("annotation: " + a. annotation. Type()); } annotation: interface se. callista. annotate. Unittest Copyright Callista Enterprise AB, 2005 Page: 10
EJB 3. 0 Extreme makeover Copyright Callista Enterprise AB, 2005 Page: 11
Overall goals with EJB 3. 0 o Decrease the number of required artifacts ( some are optional ) ù Home, Local Interface ù Deployment Descriptors ù Vendor Deployment Descriptors o Use of annotations o POJO’s and POJI’s o Transparent persistence (POJO-based a la Hibernate ) Copyright Callista Enterprise AB, 2005 Page: 12
Overall goals with EJB 3. 0 (cont’d) o Enhancements of entity beans ù Inheritance and polymorphism ù Metadata annotations for ORM ù Enhancements of EJB QL – explicit inner and outer joins, subqueries, group-by, dynamic queries… o No callbacks o Dependency Injection o Decrease the requirements of checked exceptions o Out of container testing Copyright Callista Enterprise AB, 2005 Page: 13
Interface o DD…. Declare Directly Declare your interface to be remote or local directly in your class file: @Remote public interface Order. Service { public Order. DTO find. By. Order. No(Long order. No); } Local interface declaration by @Local Copyright Callista Enterprise AB, 2005 Page: 14
Implementation o Set the type of your EJB the same way: @Stateless public class Order. Service. Bean implements Order. Service { … } Big difference compared with todays decoupled impl/interface Copyright Callista Enterprise AB, 2005 Page: 15
Transaction Handling o Set the transaction attribute of the method: @Tx(Tx. Type. REQUIRESNEW) public void update. Order (Order. DTO order) { … } Copyright Callista Enterprise AB, 2005 Page: 16
Message Driven Beans @Message. Driven @Connection. Config( destination. Type = javax. jms. Queue. class, destination. Jndi. Name = “jms/orderconfirmation", durable = true, subscription. Id = "queue. Example") public class Confirmation. MDB implements Message. Listener { public void on. Message(Message recv. Msg) {. . . Copyright Callista Enterprise AB, 2005 Page: 17
Dependency Injection @Inject ù anything from JNDI ù Access objects like Session. Context, Timer. Service, User. Transaction, Entity. Manager, EJB’s, data sources, environment variables, JMS Queues etc @Inject private Data. Source ds; Copyright Callista Enterprise AB, 2005 Page: 18
Dependency Injection o @EJB ù inject EJB references private Order. Bean order. Bean; @EJB(jndi. Name = "se. callista. Orderbean") public void set. Order. Bean(Order. Bean order. Bean) { this. order. Bean = order. Bean; } o @Resource ù access data sources ù other Copyright Callista Enterprise AB, 2005 Page: 19
Entity Beans o Plain Old Java Objects o Inheritance and polymorphism o O/R mapping with annotations o Allocate with new. o Accessed through the Entity. Manager Copyright Callista Enterprise AB, 2005 Page: 20
Entity Bean Definition @Entity @Table(name = ”CALLISTA_PRODUCT") public class Callista. Product implements java. io. Serializable { private Long id; private String name; . . . } Copyright Callista Enterprise AB, 2005 Page: 21
Entity. Manager o javax. ejb. Entity. Manager o Possible to attach, detach, reattach to/from Entity. Manager public void create(Object entity); // attach public <T> T find(Class<T> entity. Class, Object primary. Key); public <T> T merge(T entity); // reattach public void remove(Object entity); public Query create. Query(String ejbql. String); public void flush(); public void evict(Object entity); // detach . . . Copyright Callista Enterprise AB, 2005 Page: 22
Making objects persistent @Inject private Entity. Manager entity. Manager; public void create(Callista. Product product) { entity. Manager. create(product); // entity. Manager. flush(); } Copyright Callista Enterprise AB, 2005 Page: 23
Deleting objects @Inject private Entity. Manager entity. Manager; public void delete(Callista. Product product) { entity. Manager. remove(product); // entity. Manager. flush(); } Copyright Callista Enterprise AB, 2005 Page: 24
Updating objects @Inject private Entity. Manager entity. Manager; public void update(Callista. Product product) { entity. Manager. merge(product); // entity. Manager. flush(); } Copyright Callista Enterprise AB, 2005 Page: 25
Entity Beans – mapping tables & attributes @Entity @Table(name = "PURCHASE_ORDER") public class Order implements java. io. Serializable { private int id; private String customer. Name private Date order. Date @Id(generate = Generator. Type. AUTO) public int get. Id(){return id; } @Column(name=”CUSTOMER_NAME”, length=80) public String get. Customer. Name(){ return customer. Name; } public Date get. Order. Date(){ return order. Date; } Copyright Callista Enterprise AB, 2005 Page: 26
Entity Beans – one-to-many (1) Order: @One. To. Many(cascade = Cascade. Type. ALL, fetch = Fetch. Type. EAGER) @Join. Column(name = "order_id") public Collection<Order. Item> get. Items() { return items; } Copyright Callista Enterprise AB, 2005 Page: 27
Entity Beans – one-to-many (2) Order. Item: @Entity public class Order. Item implements java. io. Serializable { private Order order; @Many. To. One @Join. Column(name = "order_id") public Order get. Order(){ return order; } } Copyright Callista Enterprise AB, 2005 Page: 28
Queries @Inject private Entity. Manager manager; public List<Customer> find. By. Last. Name(String last. Name){ Query query = manager. create. Query( "from Customer c where c. lastname = : lastname"); query. set. Parameter(”lastname", last. Name); return (List<Customer>) query. list. Results(); } Copyright Callista Enterprise AB, 2005 Page: 29
Calling the service List<Customer> customer. List = service. find. By. Last. Name("Svensson"); for (Customer c : customer. List) { System. out. println(c. get. Name()); } Copyright Callista Enterprise AB, 2005 Page: 30
Object Relational Mapping o Object model is NOT equal to Database schema o Fine-grained Objects, aka Complex attributes ù Company having a Billing Address and Home Address, all attributes in one Table – three Objects o Inheritance ù See coming slides o Derived properties ù Exists in the Object but not in the Database Copyright Callista Enterprise AB, 2005 Page: 31
Entity Beans - inheritance o Three ways to implement inheritance. First up… o Single table strategy Copyright Callista Enterprise AB, 2005 Page: 32
Entity Beans - inheritance o One table per subclass Copyright Callista Enterprise AB, 2005 Page: 33
Entity Beans - inheritance o Join table strategy Copyright Callista Enterprise AB, 2005 Page: 34
Join table strategy @Entity @Inheritance(strategy = Inheritance. Type. JOINED, discriminator. Type = Discriminator. Type. STRING) @Discriminator. Column(name = ”CUSTOMER_TYPE") public class Customer implements java. io. Serializable { @Entity @Inheritance(strategy = Inheritance. Type. JOINED, discriminator. Type = Discriminator. Type. STRING, discriminator. Value = ”B_CUST") public class Business. Customer extends Customer { Copyright Callista Enterprise AB, 2005 Page: 35
Demo - Callista Store EJB 3. 0 o Eclipse 3. 1 M 4, compile-checking favorite Notepad o JBoss 4. 0. 1 with EJB 3 Preview 2 o Ant build o Changes – less code ù ù ù No No No DAO: s SQL Deployment Descriptors Home Interfaces callbacks o Changes – more code ù Annotations Copyright Callista Enterprise AB, 2005 Page: 36
Demo content o Polymorfistic Query on Customer ù Find all customers ù call description() abstract method for each hit Copyright Callista Enterprise AB, 2005 Page: 37
Conclusion Copyright Callista Enterprise AB, 2005 Page: 38
Good news o Easier development – less artifacts ù POJO + POJI + Annotations o Entity beans – completely new ù Transparent, POJO-based o Dependency Injection ù Better for testing o Is a specification – not some proprietary open source ù Vendor competition ù Portability Copyright Callista Enterprise AB, 2005 Page: 39
Concerns o Timing – when will it be ready ? ù Spring+Hibernate is gaining momentum o Out of container testing. Not in the spec yet. ù Maybe ”next release” o Dependency Injection – not a full implementation ù Inject non-JNDI POJO: s ? Copyright Callista Enterprise AB, 2005 Page: 40
Questions ? Copyright Callista Enterprise AB, 2005 Page: 41
EJB 3. 0 o Coming to an Application Server near you soon o Considerably easier to use Copyright Callista Enterprise AB, 2005 Page: 42
Rod Johnson 14 april o Färgstark kille ! o Självkostnad c: a 1000: o Börja bearbeta din chef nu. o Preliminärt intresse till Maria o Välkommna! Copyright Callista Enterprise AB, 2005 Page: 43
- Slides: 43