• Like

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Published

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
2,872
On SlideShare
0
From Embeds
0
Number of Embeds
4

Actions

Shares
Downloads
37
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. 1
  • 2. Agenda • • • • • 2
  • 3. 3
  • 4. • • • • > • • • • 4
  • 5. 5
  • 6. 6
  • 7. • • > > • > • > ● > ● 7
  • 8. • > > > • > > • > 8
  • 9. • • > • > > 9
  • 10. • • • • • • • • • • • • 10
  • 11. • • • > > > > 11
  • 12. 12
  • 13. • • > > > > > > 13
  • 14. com/acme/FooBean.class 14
  • 15. • • > > • > • > 15
  • 16. EJB 3.1 Lite Full EJB 3.1 Session Beans(stateful,stateless,singleton) ● ● Message-Driven Bean × ● 2.x/1.x CMP/BMP Entity Bean × ● (pruning) Java Persistence 2.0 ● ● 16
  • 17. Session Bean EJB 3.1 Lite Full EJB 3.1 Local/No-Intafece ● ● 3.0 Remote × ● 2.x Remote Home/Component × ● JAX-WS Web Service Endpoint × ● JAX-RPC Web Service Endpoint × ● (pruning) 17
  • 18. Services EJB 3.1 Lite Full EJB 3.1 EJB Timer Service × ● Asynchronous Session Bean Invocation × ● Interceptors ● ● RMI-IIOP Interoperability × ● Container-Managed Transactions / Bean-Managed Transactions ● ● Declarative and Programmatic Security ● ● Embeddable API ● ● 18
  • 19. public interface Hello { public String sayHello(); @EJB } private Hello h; ... h.sayHello(); @Stateless public class HelloBean implements Hello { public String sayHello() { return “hello”; }} 19
  • 20. @EJB @Stateless private HelloBean h; public class HelloBean { public String sayHello() ... { return “hello”; } } h.sayHello(); • • 20
  • 21. • • > 21
  • 22. • > • > • > 22
  • 23. @Stateless public class HelloBean implements Hello { public String sayHello(String msg) { return “Hello “ + msg; } } java:global/hello/HelloBean java:app/hello/HelloBean java:module/HelloBean 23
  • 24. • > > > • > > 24
  • 25. package test; import javax.ejb.Stateless; @Stateless public class Hello { public String sayHello(){ return "Hello Embedded TEST"; } } 25
  • 26. @Test public void testSayHello() { Map p = new HashMap(); p.put("org.glassfish.ejb.embedded.glassfish.instance.root", "/Applications/GlassFish/glassfishv3-webprofile/glassfish/ domains/domain1"); EJBContainer container = EJBContainer.createEJBContainer(p); try{ Hello hello = (Hello)container.getContext().lookup ("java:global/classes/Hello"); System.out.println(hello.sayHello()); }catch(Exception e){ e.printStackTrace(); }} 26
  • 27. 27
  • 28. • > • > 28
  • 29. • > > • > • 29
  • 30. • > ● > > • > 30
  • 31. @Singleton @Stateless public class SharedBean { public class FooBean { private SharedData shared; @EJB @PostConstruct private SharedBean shared; private void init() { public void foo() { shared = ...; int xyz = shared.getXYZ(); } … public int getXYZ() { } return shared.xyz; } } 31
  • 32. • > > ● ● • > > ● 32
  • 33. @Singleton @Startup public class StartupBean { @PostConstruct private void onStartup() { … } @PreDestroy private void onShutdown() { … } } 33
  • 34. • • > > • > ● > ● 34
  • 35. @Stateless public class BankBean { @PersistenceContext EntityManager accountDB; @Resource javax.mail.Session mailSession; @Schedule(hour=”8”, dayOfMonth=”Last”) void sendMonthlyBankStatements() { ... } } 35
  • 36. • > > > • > > ● 36
  • 37. @Stateless public class DocBean { @PersistenceContext EntityManager resultsDB; @EJB DocBean myself; public void processDocument(Document document) { myself.doAnalysisA(document); myself.doAnalysisB(document); } @Asynchronous public void doAnalysisA(Document d) {...} @Asynchronous public void doAnalysisB(Document d) {...} 37
  • 38. • • > > > > > > 38
  • 39. • • > > • • > > • • > > • > 39
  • 40. 40
  • 41. • • • 41
  • 42. • > > • > • > ● > 42
  • 43. • • • > 43
  • 44. 44
  • 45. • > • > • > > 45
  • 46. % telnet localhost 6666 Trying 127.0.0.1… Connected to localhost. Escape character is ‘^]’. Felix Remote Shell Console: ============================ -> ps (GlassFish v3 ) START LEVEL 1 ID State Level Name [ 0] [Active ] [ 0] System Bundle (2.0.2) ….. ( ) [ 245] [Installed ] [ 1] MyBundle (1.0.0) -> start 245 -> stop 245 -> exit Good Bye! 46
  • 47. • > • > > • > 47
  • 48. 48
  • 49. • > > > • > > > • > 49
  • 50. • > > > > • • > • > 50
  • 51. • • • 51
  • 52. • > SSH Client ● > Node1 Node2 • sshd sshd • > > > • 52
  • 53. • > > ● ● ● ● • 53
  • 54. • > • > • > • > 54
  • 55. • • > • • • • • > 55
  • 56. • • • • 56
  • 57. • > • > • > • > 57
  • 58. 58
  • 59. • > • > ● > > • > > • 59
  • 60. 60
  • 61. 61
  • 62. 62
  • 63. • • > > > ● > > 63
  • 64. • > > 64
  • 65. • • • • • • 65
  • 66. package hello; <web-app> import javax.servlet.annotation.WebServlet; <servlet> <servlet-name>Hello</servlet-name> import javax.servlet.http.HttpServlet; <servlet-class>hello.Hello</servlet-class> import javax.servlet.http.HttpServletRequest; </servlet> import javax.servlet.http.HttpServletResponse; <servlet-mapping> <servlet-name>Hello</servlet-name> @WebServlet(name="Hello", urlPatterns={"/Hello"}) <url-pattern>/Hello/* </url-pattern> </servlet-mapping> public class Hello extends HttpServlet { ... </web-app> @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ....... } } 66
  • 67. • • > 67
  • 68. • > > > • > 68
  • 69. <web-fragment> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet- class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> </web-fragment> 69
  • 70. • > > • > • > 70
  • 71. 71
  • 72. <FORM action=”/MyFileUpload” @WebServlet( name = ”MyFileUpload”, urlPatterns = enctype=”multipart/form-data” method=”POST”> {“/MyFileUpload”}) : <INPUT type=”file” name=”content”> @MultipartConfig( fileSizeThreshold=5000000, <INPUT type=”submit” value=”Submit”> MaxFileSize=10000000, location=”/tmp”) </FORM> public class MyFileUpload extends HttpServlet { protected void processRequest(HttpServletRequest • request, HttpServletResponse response) throws ServletException, IOException { • // <INPUT type=”file” name=”content”> Part part = request.getPart(“content”); // > // • String fname = getFilename(part); > // @MultipartConfig(location=”/tmp”) // part.write(fname); 72
  • 73. Servlet 3.0 • > • > > • > 73
  • 74. 74
  • 75. • > • > > > • > 75
  • 76. 76
  • 77. • • > > > ● > > > 77
  • 78. • > > > > > > 78
  • 79. @Entity public class Person implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; @ElementCollection PERSON private Set<String> nickname; PK ID Name PERSON_NICKNAME Person_ID NICKNAME 79
  • 80. @Entity public class Person implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; @ElementCollection PERSON @CollectionTable(name="NICKNAMES") PK ID @Column(name="aliases") Name private Set<String> nickname; NICKNAMES Person_ID aliases 80
  • 81. @Embeddable PERSON public class Address{ private String street; PK ID Name private String town; private String city; private String prefecture; } PERSON_ADDRESS @Entity Person_ID public class Person { street @ElementCollection town private Set<Address> addresses; city } prefecture 81
  • 82. @Embeddable public class ContactInfo { @Embedded Address addresses; PERSON } PK ID @Entity name public class Person { street @Id town private Long id; city private String name; prefecture private ContactInfo contactInfo; 82
  • 83. @Embeddable public class ContactInfo { PHONE @Embedded Address addresses; PK PHONENUMBER PHONETYPE @OneToMany Set<Phone> phone; } PERSON @Entity public class Person { @Id PK ID private Long id; name ... private String name; private ContactInfo contactInfo; PERSON_PHONE PK PERSON_ID PK PHONE_PHONENUMBER 83
  • 84. • > > • > > 84
  • 85. • > @Entity public class Person{ @OneToMany(mappedBy="owner") @MapKey(name="phone_type") private Map<String, PhoneNumber> phoneNumbers; … @Entity public class PhoneNumber{ @Basic private String phone_type; @ManyToOne private Person owner; 85
  • 86. Map Mapping Key Value @MapKeyColumn Map<Basic,Basic> @ElementCollection @MapKeyEnumerated @Column @MapKeyTemporal @MapKeyColumn Embeddable Map<Basic,Embeddable> @ElementCollection @MapKeyEnumerated @AttrivuteOverride @MapKeyTemporal @AssociationOverride @MapKey @MapKeyColumn Entity Map<Basic,Entity> @OneToMany,@ManyToMany @MapKeyEnumerated @MapKeyTemporal Embeddable Map<Embeddable,Basic> @ElementCollection @Column @AttrbuteOverride Embeddable Embeddable Map<Embeddable,Embeddable> @ElementCollection @AttrivuteOverride @AttrbuteOverride @AssociationOverride Map<Embeddable,Entity> @OneToMany,@ManyToMany Embeddable Entity Map<Entity,Basic> @ElementCollection @MapKeyJoinColumn @Column Embeddable Map<Entity,Embeddable> @ElementCollection @MapKeyJoinColumn @AttrivuteOverride @AssociationOverride Map<Entity,Entity> @OneToMany,@ManyToMany @MapKeyJoinColumn Entity 86
  • 87. @Entity public class Customer{ @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String firstName; .... @ElementCollection Customer_PHONENUBER @CollectionTable(name=”**”) :@MapKeyColumn(name=”**”) PK,FK1 Customer_ID PK PHONENUMBER_KEY @Column(name=”**”) private Map<String,String> PHONENUMBER phoneNumber; 87
  • 88. @Entity public class Customer{ .... @ElementCollection @CollectionTable @MapKeyEnumerated(EnumType.STRING) @MapKeyColumn @Column private Map<PhoneType,String> phoneNumber; ..... } public enum PhoneType{Mobile, Home, Company} 88
  • 89. @Entity Public class Project { .... @OneToMany @OrderBy(“lastname DESC ,phonenumber ASC ) private List<Person> person; } @Entity public class Person { .... private String lastname; private String phonenumber; } 89
  • 90. • > > @Entity public class CreditCard { @Id long ccNumber; @OneToMany // unidirectional @OrderColumn List<CardTransaction> transactionHistory; ... } 90
  • 91. • > > > @Entity public class Order { @Id int orderId; ... @OneToMany(cascade=PERSIST, orphanRemoval=true ) Set<Item> lineItems; 91
  • 92. • > > > > 92
  • 93. • > • > • > • > 93
  • 94. SELECT t FROM CreditCard c JOIN c.transactions t WHERE c.cardHolder.name = 'John Doe' AND INDEX(t) < 10 94
  • 95. // Inventory is Map<Movie,Integer> SELECT v.location.street, KEY(i).title, VALUE(i) FROM VideoStore v JOIN v.inventory i WHERE KEY(i).director LIKE '%Hitchcock%' AND VALUE(i) > 0 95
  • 96. • > • > • > 96
  • 97. UPDATE Employee e SET e.salary = CASE e.rating WHEN 1 THEN e.salary * 1.2 WHEN 2 THEN e.salary * 1.1 ELSE e.salary * 1.02 END 97
  • 98. Employee SELECT e FROM Employee e WHERE TYPE(e) IN (Contractor) Regular Contractor 98
  • 99. • > > • > > • > • > 99
  • 100. • > > • > > • > • > 100
  • 101. EntityManager em = … ; CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Person> p = cb.createQuery(Person.class); Root<Person> person = p.from(Person.class); p.select(person).where( cb.equal(person.get(“name”),”Taro Yamada”)); SELECT p FROM Person p WHERE p.name = 'Taro Yamada' 101
  • 102. • > ● > • > ● > 102
  • 103. • EntityManager em = … ; CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Person> p = cb.createQuery(Person.class); Root<Person> person = p.from(Person.class); p.select(person).where( cb.equal(person.get(“name”),”Taro Yamada”)); 103
  • 104. • @Entity import javax.persistence.metamodel public class Person { .SingularAttribute; @Id @javax.persistence.metamodel.Stati @GeneratedValue(strategy = cMetaModel(Person.class) GenerationType.AUTO) public class Person_ { private Long id; public static volatile private String name; SingularAttribute<Person,Long> id; Private Integer age; public static volatile SingularAttribute<Person,String> name; public static volatile SingularAttribute<Person,Integer> age; 104
  • 105. • EntityManager em = ...; Metamodel meta = em.getMetaModel(); EntityType<Person> Person_ = meta.entity(Person.class); 105
  • 106. • EntityManager em = … ; CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Person> p = cb.createQuery(Person.class); Root<Person> person = p.from(Person.class); p.select(person).where( cb.equal(person.get(Person_.name),”Taro Yamada”)); 106
  • 107. • > ● • > ● ● 107
  • 108. • em.lock(person, LockModeType.PESSIMISTIC_WRITE); • > • > • > 108
  • 109. • @Entity public class Employee { @Id Integer empId; String name; @Max(15) Integer vacationDays; @Valid Address worksite; ... } @Embeddable public class Address { @Size(max=30) String street; @NotNull @Size(max=20) String city; @Size.max @Zipcode String zipcode; @Min/@Max ... @Future/@Past } @Size 109
  • 110. • > > • > > > 110
  • 111. • > > • > > > > 111
  • 112. 112
  • 113. @Entity @Cache(type=CacheType.WEAK, isolated=false, expiry=60000, alwaysRefresh=true, disableHits=true, coordinationType=INVALIDATE_CHANGE_OBJECTS) @Cacheable(true) public class Person implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; 113
  • 114. • > ● ● • > ● ● ● 114
  • 115. public List<Person> findPersons(){ TypedQuery<Person> query = em.createQuery(“SELECT p ...”); query.setProperty("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS); query.setProperty("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH); return query.getResultList(); } 115
  • 116. • > 116
  • 117. • > 117
  • 118. 118