Aggiunto l'esempio capitolo su Enterprise JavaBean

This commit is contained in:
Fabio Scotto di Santolo
2019-03-29 16:02:49 +01:00
parent 9ae35dc078
commit 09af0ccdfb
11 changed files with 373 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
package it.plague.jeedemo;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Data
@NoArgsConstructor
@Builder
@NamedQuery(name = "Book.findAll", query = "select b from Book b")
public class Book implements Serializable {
@Id
@GeneratedValue
private Long id;
@NotNull
@Column(nullable = false)
private String title;
private Float price;
@Size(max = 2000)
@Column(length = 2000)
private String description;
private String isbn;
private Integer nbOfPage;
private Boolean illustrations;
}

View File

@@ -0,0 +1,39 @@
package it.plague.jeedemo;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.validation.constraints.NotNull;
@Stateless
@LocalBean
public class BookEJB implements BookEJBRemote {
@Inject
private EntityManager em;
@Override
public List<Book> findBooks() {
TypedQuery<Book> query = em.createNamedQuery("Book.findAll", Book.class);
return query.getResultList();
}
@Override
public @NotNull Book createBook(@NotNull Book book) {
em.persist(book);
return book;
}
@Override
public @NotNull Book updateBook(@NotNull Book book) {
return em.merge(book);
}
@Override
public void deleteBook(@NotNull Book book) {
em.remove(em.merge(book));
}
}

View File

@@ -0,0 +1,16 @@
package it.plague.jeedemo;
import java.util.List;
import javax.ejb.Remote;
@Remote
public interface BookEJBRemote {
List<Book> findBooks();
Book createBook(Book book);
void deleteBook(Book book);
Book updateBook(Book book);
}

View File

@@ -0,0 +1,56 @@
package it.plague.jeedemo;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.sql.DataSourceDefinition;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;
@Startup
@Singleton
@DataSourceDefinition(
className = "org.apache.derby.jdbc.EmbeddedDataSource",
name = "java:global/jdbc/ejbdemoDS",
user = "app",
password = "app",
databaseName = "ejbdemo",
properties = {"connectionAttributes=;create=true"})
public class DatabasePopulator {
@Inject
private BookEJB bookEJB;
private Book h2g2;
private Book lord;
@PostConstruct
private void populateDB() {
h2g2 = Book.builder()
.title("Beginning Java EE 7")
.price(35f)
.description("Great book")
.isbn("1-8763-9125-7")
.nbOfPage(605)
.illustrations(true)
.build();
lord = Book.builder()
.title("The Lord of the Rings")
.price(50.4f)
.description("SciFi")
.isbn("1-84023-742-2")
.nbOfPage(1216)
.illustrations(true)
.build();
bookEJB.createBook(h2g2);
bookEJB.createBook(lord);
}
@PreDestroy
private void clearDB() {
bookEJB.deleteBook(h2g2);
bookEJB.deleteBook(lord);
}
}

View File

@@ -0,0 +1,12 @@
package it.plague.jeedemo;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
public class DatabaseProducer {
@Produces
@PersistenceContext(unitName = "ejbdemoPU")
private EntityManager em;
}

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="ejbdemoPU" transaction-type="JTA">
<jta-data-source>java:global/jdbc/ejbdemoDS</jta-data-source>
<properties>
<property name="eclipselink.target-database" value="DERBY"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.logging.level" value="ALL"/>
</properties>
</persistence-unit>
</persistence>

View File

@@ -0,0 +1,54 @@
package it.plague.jeedemo;
import static org.junit.Assert.*;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.ejb.embeddable.EJBContainer;
import javax.naming.Context;
import org.junit.Test;
public class BookEJBIT {
@Test
public void shouldCreateABook() throws Exception {
Map<String, Object> properties = new HashMap<>();
properties.put(EJBContainer.MODULES, new File("target/classes"));
try (EJBContainer ec = EJBContainer.createEJBContainer(properties)) {
Context ctx = ec.getContext();
// Check JNDI dependencies (Datasource and EJBs)
assertNotNull(ctx.lookup("java:global/jdbc/ejbdemoDS"));
assertNotNull(ctx.lookup("java:global/classes/BookEJB!it.plague.jeedemo.BookEJBRemote"));
assertNotNull(ctx.lookup("java:global/classes/BookEJB!it.plague.jeedemo.BookEJB"));
// Looks up the EJB
BookEJB bookEJB = (BookEJB) ctx.lookup("java:global/classes/BookEJB!it.plague.jeedemo.BookEJB");
// Find all the books and makes sure there are two (inserted by the DBPopulator)
assertEquals(2, bookEJB.findBooks().size());
// Creates an instance of book;
Book book = Book.builder()
.title("H2G2")
.price(12.5f)
.description("Scifi book")
.isbn("1-24561-799-0")
.nbOfPage(354)
.illustrations(false)
.build();
book = bookEJB.createBook(book);
assertNotNull("ID should not be null", book.getId());
// Find all the books and makes sure is an extra one
assertEquals(3, bookEJB.findBooks().size());
// Removes the created book
bookEJB.deleteBook(book);
// Finds all the books and makes sure there is one less
assertEquals(2, bookEJB.findBooks().size());
}
}
}