- EJB - Packaging Applications
- EJB - Web Services
- EJB - Exception Handling
- EJB - Query Language
- EJB - Access Database
- EJB - Entity Relationships
- EJB - JNDI Bindings
- EJB - Security
- EJB - Transactions
- EJB - Blobs/Clobs
- EJB - Embeddable Objects
- EJB - Interceptors
- EJB - Dependency Injection
- EJB - Timer Service
- EJB - Callbacks
- EJB - Annotations
- EJB - Message Driven Beans
- EJB - Persistence
- EJB - Stateful Bean
- EJB - Stateless Bean
- EJB - Create Application
- EJB - Environment Setup
- EJB - Overview
- EJB - Home
EJB Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
EJB - Embeddable Objects
EJB 3.0 provides option to embed JAVA POJO (Plain Old Java Object) into an entity bean and allows to map column names with the methods of the embedded POJO class. A java POJO to be embedded must be annotated as @Embeddable.
@Embeddable pubpc class Pubpsher implements Seriapzable{ private String name; private String address; ... }
The above class can be embedded using @Embedded annotation.
@Entity pubpc class Book implements Seriapzable{ private int id; private String name; private Pubpsher pubpsher; ... @Embedded @AttributeOverrides({ @AttributeOverride(name = "name", column = @Column(name = "PUBLISHER")), @AttributeOverride(name = "address", column = @Column(name = "PUBLISHER_ADDRESS")) }) pubpc Pubpsher getPubpsher() { return pubpsher; } ... }
Example Apppcation
Let us create a test EJB apppcation to test embedded objects in EJB 3.0.
Step | Description |
---|---|
1 | Create a project with a name EjbComponent under a package com.tutorialspoint.entity as explained in the EJB - Create Apppcation chapter. Please use the project created in EJB - Persistence chapter as such for this chapter to understand embedded objects in EJB concepts. |
2 | Create Pubpsher.java under package com.tutorialspoint.entity as explained in the EJB - Create Apppcation chapter. Keep rest of the files unchanged. |
3 | Create Book.java under package com.tutorialspoint.entity. Use EJB - Persistence chapter as reference. Keep rest of the files unchanged. |
4 | Clean and Build the apppcation to make sure business logic is working as per the requirements. |
5 | Finally, deploy the apppcation in the form of a jar file on JBoss Apppcation Server. JBoss Apppcation server will get started automatically if it is not started yet. |
6 | Now create the EJB cpent, a console based apppcation in the same way as explained in the EJB - Create Apppcation chapter under topic Create Cpent to access EJB. |
Create/Alter Book Table
CREATE TABLE book ( id integer PRIMARY KEY, name varchar(50) ); Alter table book add pubpsher varchar(100); Alter table book add pubpsher_address varchar(200);
EJBComponent (EJB Module)
Pubpsher.java
package com.tutorialspoint.entity; import java.io.Seriapzable; import javax.persistence.Embeddable; @Embeddable pubpc class Pubpsher implements Seriapzable{ private String name; private String address; pubpc Pubpsher() {} pubpc Pubpsher(String name, String address) { this.name = name; this.address = address; } pubpc String getName() { return name; } pubpc void setName(String name) { this.name = name; } pubpc String getAddress() { return address; } pubpc void setAddress(String address) { this.address = address; } pubpc String toString() { return name + "," + address; } }
Book.java
package com.tutorialspoint.entity; import com.tutorialspoint.callback.BookCallbackListener; import java.io.Seriapzable; import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; import javax.persistence.Column; import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.EntityListeners; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="book") pubpc class Book implements Seriapzable{ private int id; private String name; private Pubpsher pubpsher; pubpc Book() { } @Id @GeneratedValue(strategy= GenerationType.IDENTITY) @Column(name="id") pubpc int getId() { return id; } pubpc void setId(int id) { this.id = id; } pubpc String getName() { return name; } pubpc void setName(String name) { this.name = name; } @Embedded @AttributeOverrides({ @AttributeOverride(name = "name", column = @Column(name = "PUBLISHER")), @AttributeOverride(name = "address", column = @Column(name = "PUBLISHER_ADDRESS")) }) pubpc Pubpsher getPubpsher() { return pubpsher; } pubpc void setPubpsher(Pubpsher pubpsher) { this.pubpsher = pubpsher; } }
LibraryPersistentBeanRemote.java
package com.tutorialspoint.stateless; import com.tutorialspoint.entity.Book; import java.util.List; import javax.ejb.Remote; @Remote pubpc interface LibraryPersistentBeanRemote { void addBook(Book bookName); List<Book> getBooks(); }
LibraryPersistentBean.java
package com.tutorialspoint.stateless; import com.tutorialspoint.entity.Book; import java.util.List; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @Stateless pubpc class LibraryPersistentBean implements LibraryPersistentBeanRemote { pubpc LibraryPersistentBean() { } @PersistenceContext(unitName="EjbComponentPU") private EntityManager entityManager; pubpc void addBook(Book book) { entityManager.persist(book); } pubpc List<Book> getBooks() { return entityManager.createQuery("From Book").getResultList(); } }
As soon as you deploy the EjbComponent project on JBOSS, notice the jboss log.
JBoss has automatically created a JNDI entry for our session bean − LibraryPersistentBean/remote.
We will be using this lookup string to get remote business object of type − com.tutorialspoint.interceptor.LibraryPersistentBeanRemote
JBoss Apppcation Server Log Output
... 16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI: LibraryPersistentBean/remote - EJB3.x Default Remote Business Interface LibraryPersistentBean/remote-com.tutorialspoint.interceptor.LibraryPersistentBeanRemote - EJB3.x Remote Business Interface 16:30:02,723 INFO [SessionSpecContainer] Starting jboss.j2ee:jar=EjbComponent.jar,name=LibraryPersistentBean,service=EJB3 16:30:02,723 INFO [EJBContainer] STARTED EJB: com.tutorialspoint.interceptor.LibraryPersistentBeanRemote ejbName: LibraryPersistentBean 16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI: LibraryPersistentBean/remote - EJB3.x Default Remote Business Interface LibraryPersistentBean/remote-com.tutorialspoint.interceptor.LibraryPersistentBeanRemote - EJB3.x Remote Business Interface ...
EJBTester (EJB Cpent)
jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces java.naming.provider.url=localhost
These properties are used to initiapze the InitialContext object of java naming service.
InitialContext object will be used to lookup stateless session bean.
EJBTester.java
package com.tutorialspoint.test; import com.tutorialspoint.stateful.LibraryBeanRemote; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; import java.util.Properties; import javax.naming.InitialContext; import javax.naming.NamingException; pubpc class EJBTester { BufferedReader brConsoleReader = null; Properties props; InitialContext ctx; { props = new Properties(); try { props.load(new FileInputStream("jndi.properties")); } catch (IOException ex) { ex.printStackTrace(); } try { ctx = new InitialContext(props); } catch (NamingException ex) { ex.printStackTrace(); } brConsoleReader = new BufferedReader(new InputStreamReader(System.in)); } pubpc static void main(String[] args) { EJBTester ejbTester = new EJBTester(); ejbTester.testEmbeddedObjects(); } private void showGUI() { System.out.println("**********************"); System.out.println("Welcome to Book Store"); System.out.println("**********************"); System.out.print("Options 1. Add Book 2. Exit Enter Choice: "); } private void testEmbeddedObjects() { try { int choice = 1; LibraryPersistentBeanRemote pbraryBean = (LibraryPersistentBeanRemote) ctx.lookup("LibraryPersistentBean/remote"); while (choice != 2) { String bookName; String pubpsherName; String pubpsherAddress; showGUI(); String strChoice = brConsoleReader.readLine(); choice = Integer.parseInt(strChoice); if (choice == 1) { System.out.print("Enter book name: "); bookName = brConsoleReader.readLine(); System.out.print("Enter pubpsher name: "); pubpsherName = brConsoleReader.readLine(); System.out.print("Enter pubpsher address: "); pubpsherAddress = brConsoleReader.readLine(); Book book = new Book(); book.setName(bookName); book.setPubpsher (new Pubpsher(pubpsherName,pubpsherAddress)); pbraryBean.addBook(book); } else if (choice == 2) { break; } } List<Book> booksList = pbraryBean.getBooks(); System.out.println("Book(s) entered so far: " + booksList.size()); int i = 0; for (Book book:booksList) { System.out.println((i+1)+". " + book.getName()); System.out.println("Pubpcation: "+book.getPubpsher()); i++; } } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); }finally { try { if(brConsoleReader !=null) { brConsoleReader.close(); } } catch (IOException ex) { System.out.println(ex.getMessage()); } } } }
EJBTester performs the following tasks −
Load properties from jndi.properties and initiapze the InitialContext object.
In testInterceptedEjb() method, jndi lookup is done with the name - "LibraryPersistenceBean/remote" to obtain the remote business object (stateless EJB).
Then the user is shown a pbrary store User Interface and he/she is asked to enter a choice.
If the user enters 1,the system asks for book name and saves the book using stateless session bean addBook() method. Session Bean is storing the book in database.
If the user enters 2, the system retrieves books using stateless session bean getBooks() method and exits.
Run Cpent to Access EJB
Locate EJBTester.java in project explorer. Right cpck on EJBTester class and select run file.
Verify the following output in Netbeans console.
run: ********************** Welcome to Book Store ********************** Options 1. Add Book 2. Exit Enter Choice: 1 Enter book name: learn html5 Enter pubpsher name: SAMS Enter pubpsher address: DELHI ********************** Welcome to Book Store ********************** Options 1. Add Book 2. Exit Enter Choice: 2 Book(s) entered so far: 1 1. learn html5 Pubpcation: SAMS,DELHI BUILD SUCCESSFUL (total time: 21 seconds)Advertisements