Aggiunto l'esempio del capitolo su Java Persistence API

This commit is contained in:
Fabio Scotto di Santolo
2019-03-29 14:44:46 +01:00
parent 2898a2ff13
commit e9f8ecc30e
6 changed files with 273 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
package it.plague.jeedemo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Builder
@NamedQueries({
@NamedQuery(name = "Book.findAll", query = "select b from Book b"),
@NamedQuery(name = "Book.findOneH2G2", query = "select b from Book b where b.title = 'H2G2'")
})
public @Data
class Book {
@Id
@GeneratedValue
private Long id;
@NotNull
private String title;
private Float price;
@Size(min = 10, max = 2000)
private String description;
private String isbn;
private Integer nbOfPage;
private Boolean illustrations;
}

View File

@@ -0,0 +1,3 @@
insert into book(id, title, description, illustrations, isbn, nbofpage, price) values (1000, 'Beginning Java EE 6', 'Best Java EE book ever', 1, '1234-5678', 450, 49)
insert into book(id, title, description, illustrations, isbn, nbofpage, price) values (1001, 'Beginning Java EE 7', 'No, this is the best', 1, '5678-9012', 550, 53)
insert into book(id, title, description, illustrations, isbn, nbofpage, price) values (1010, 'The Lord of the Rings', 'One ring to rule them all', 0, '1234-5678', 222, 23)

View File

@@ -0,0 +1,40 @@
<?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="jpademoUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>it.plague.jeedemo.Book</class>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.create-source" value="metadata"/>
<property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
<property name="javax.persistence.schema-generation.create-database-schemas" value="true"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/jpademo;create=true"/>
<property name="javax.persistence.jdbc.user" value="APP"/>
<property name="javax.persistence.jdbc.password" value="APP"/>
<property name="javax.persistence.sql-load-script-source" value="META-INF/insert.sql"/>
<property name="eclipselink.logging.level" value="ALL"/>
</properties>
</persistence-unit>
<!-- TEST PERSISTENCE UNIT -->
<persistence-unit name="jpademoTestUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>it.plague.jeedemo.Book</class>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.create-source" value="metadata"/>
<property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
<property name="javax.persistence.schema-generation.create-database-schemas" value="true"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:jpademo;create=true"/>
<property name="javax.persistence.sql-load-script-source" value="META-INF/insert.sql"/>
<property name="eclipselink.logging.level" value="ALL"/>
</properties>
</persistence-unit>
</persistence>

View File

@@ -0,0 +1,70 @@
package it.plague.jeedemo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.validation.ConstraintViolationException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class BookIT {
private EntityManager em;
@Before
public void initEntityManager() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpademoTestUnit");
em = emf.createEntityManager();
}
@After
public void closeEntityManager() {
if (em != null) {
em.close();
}
}
@Test
public void shouldCreateJavaEE7Book() {
Book book = em.find(Book.class, 1001L);
assertEquals("Beginning Java EE 7", book.getTitle());
}
@Test
public void shouldCreateH2G2Book() {
Book book = Book.builder()
.title("H2G2")
.description("The Hitchhiker's Guide to the Galaxy")
.price(12.5f)
.isbn("1-84023-742-2")
.nbOfPage(354)
.illustrations(false)
.build();
em.getTransaction().begin();
em.persist(book);
em.getTransaction().commit();
assertNotNull("ID should not be null", book.getId());
book = em.createNamedQuery("Book.findOneH2G2", Book.class).getSingleResult();
assertEquals("The Hitchhiker's Guide to the Galaxy", book.getDescription());
}
@Test(expected = ConstraintViolationException.class)
public void shouldRaiseConstraintViolationCauseNullTitle() {
Book book = Book.builder()
.title(null)
.description("Null title, should fail")
.price(12.5f)
.isbn("1-84023-742-2")
.nbOfPage(354)
.illustrations(false)
.build();
em.persist(book);
}
}