Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Apress
committed
Oct 14, 2016
0 parents
commit cd5758a
Showing
405 changed files
with
25,189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Freeware License, some rights reserved | ||
|
||
Copyright (c) 2010 Antonio Goncalves | ||
|
||
Permission is hereby granted, free of charge, to anyone obtaining a copy | ||
of this software and associated documentation files (the "Software"), | ||
to work with the Software within the limits of freeware distribution and fair use. | ||
This includes the rights to use, copy, and modify the Software for personal use. | ||
Users are also allowed and encouraged to submit corrections and modifications | ||
to the Software for the benefit of other users. | ||
|
||
It is not allowed to reuse, modify, or redistribute the Software for | ||
commercial use in any way, or for a user�s educational materials such as books | ||
or blog articles without prior permission from the copyright holder. | ||
|
||
The above copyright notice and this permission notice need to be included | ||
in all copies or substantial portions of the software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#Apress Source Code | ||
|
||
This repository accompanies [*Beginning Java EE 6 with GlassFish 3*](http://www.apress.com/9781430228899) by Antonio Goncalves (Apress, 2010). | ||
|
||
![Cover image](9781430228899.jpg) | ||
|
||
Download the files as a zip using the green button, or clone the repository to your machine using Git. | ||
|
||
##Releases | ||
|
||
Release v1.0 corresponds to the code in the published book, without corrections or updates. | ||
|
||
##Contributions | ||
|
||
See the file Contributing.md for more information on how you can contribute to this repository. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
Beginning Java� EE 6 Platform with GlassFish� 3: From Novice to Professional | ||
---------------------------------------------------------------------------- | ||
Antonio Goncalves (http://www.antoniogoncalves.org) | ||
Apress (http://apress.com/book/view/9781430219545) | ||
Download code (http://kenai.com/projects/beginningee6/sources/src/show/chapters/trunk) | ||
Forum to ask questions (http://kenai.com/projects/beginningee6/) | ||
============================================================================ | ||
|
||
The code used in the book is defined in the following sub-directories : | ||
* Chapter02 : Introduction to JPA 2.0 | ||
* Chapter03 : JPA Mapping | ||
* Chapter04 : JPA Entity Manager and JPQL | ||
* Chapter05 : JPA Lifecycle and Listeners | ||
* Chapter06 : Introduction to EJB 3.1 | ||
* Chapter07 : Session beans | ||
* Chapter08 : EJB Lifecycle and Interceptors | ||
* Chapter09 : Transactions and security | ||
* Chapter10 : Introduction to JSF 2.0 | ||
* Chapter11 : JSF Pages and components | ||
* Chapter12 : Processing & JSF Navigation | ||
* Chapter13 : JMS Sender | ||
* Chapter13-MDB : JMS Message Driven Bean | ||
* Chapter14-Consumer : SOAP Web service consumer | ||
* Chapter14-Service : SOAP Web service | ||
* Chapter15-Resource : RESTful Web service | ||
|
||
To compile, package and execute the code you need the following software : | ||
* Java SE 6 : http://java.sun.com/javase/downloads | ||
* Derby 1.5 : http://db.apache.org/derby | ||
* Maven 2 : http://maven.apache.org | ||
* GlassFish v3 : http://glassfish.org |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>org.beginningee6.book</groupId> | ||
<artifactId>chapter02</artifactId> | ||
<version>2.0</version> | ||
<name>Chapter 02 - JPA</name> | ||
|
||
<parent> | ||
<groupId>org.beginningee6.book</groupId> | ||
<artifactId>chapters</artifactId> | ||
<version>2.0</version> | ||
</parent> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.eclipse.persistence</groupId> | ||
<artifactId>javax.persistence</artifactId> | ||
<version>${javax.persistence-version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.persistence</groupId> | ||
<artifactId>eclipselink</artifactId> | ||
<version>${eclipselink-version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.derby</groupId> | ||
<artifactId>derbyclient</artifactId> | ||
<version>${derby-version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.derby</groupId> | ||
<artifactId>derby</artifactId> | ||
<version>${derby-version}</version> | ||
<!--<scope>test</scope>--> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
121 changes: 121 additions & 0 deletions
121
chapter02/src/main/java/org/beginningee6/book/chapter02/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package org.beginningee6.book.chapter02; | ||
|
||
import javax.persistence.*; | ||
|
||
/** | ||
* @author Antonio Goncalves | ||
* APress Book - Beginning Java EE 6 with Glassfish | ||
* http://www.apress.com/ | ||
* http://www.antoniogoncalves.org | ||
* -- | ||
* Simple Book ,entity | ||
*/ | ||
@Entity | ||
@NamedQuery(name = "findAllBooks", query = "SELECT b FROM Book b") | ||
public class Book { | ||
|
||
// ====================================== | ||
// = Attributes = | ||
// ====================================== | ||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
@Column(nullable = false) | ||
private String title; | ||
private Float price; | ||
@Column(length = 2000) | ||
private String description; | ||
private String isbn; | ||
private Integer nbOfPage; | ||
private Boolean illustrations; | ||
|
||
// ====================================== | ||
// = Constructors = | ||
// ====================================== | ||
|
||
public Book() { | ||
} | ||
|
||
public Book(String title, Float price, String description, String isbn, Integer nbOfPage, Boolean illustrations) { | ||
this.title = title; | ||
this.price = price; | ||
this.description = description; | ||
this.isbn = isbn; | ||
this.nbOfPage = nbOfPage; | ||
this.illustrations = illustrations; | ||
} | ||
|
||
// ====================================== | ||
// = Getters & Setters = | ||
// ====================================== | ||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public Float getPrice() { | ||
return price; | ||
} | ||
|
||
public void setPrice(Float price) { | ||
this.price = price; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public String getIsbn() { | ||
return isbn; | ||
} | ||
|
||
public void setIsbn(String isbn) { | ||
this.isbn = isbn; | ||
} | ||
|
||
public Integer getNbOfPage() { | ||
return nbOfPage; | ||
} | ||
|
||
public void setNbOfPage(Integer nbOfPage) { | ||
this.nbOfPage = nbOfPage; | ||
} | ||
|
||
public Boolean getIllustrations() { | ||
return illustrations; | ||
} | ||
|
||
public void setIllustrations(Boolean illustrations) { | ||
this.illustrations = illustrations; | ||
} | ||
|
||
// ====================================== | ||
// = hash, equals, toString = | ||
// ====================================== | ||
|
||
@Override | ||
public String toString() { | ||
final StringBuilder sb = new StringBuilder(); | ||
sb.append("Book"); | ||
sb.append("{id=").append(id); | ||
sb.append(", title='").append(title).append('\''); | ||
sb.append(", price=").append(price); | ||
sb.append(", description='").append(description).append('\''); | ||
sb.append(", isbn='").append(isbn).append('\''); | ||
sb.append(", nbOfPage=").append(nbOfPage); | ||
sb.append(", illustrations=").append(illustrations); | ||
sb.append('}'); | ||
return sb.toString(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
chapter02/src/main/java/org/beginningee6/book/chapter02/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.beginningee6.book.chapter02; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
import javax.persistence.EntityTransaction; | ||
import javax.persistence.Persistence; | ||
|
||
/** | ||
* @author Antonio Goncalves | ||
* APress Book - Beginning Java EE 6 with Glassfish | ||
* http://www.apress.com/ | ||
* http://www.antoniogoncalves.org | ||
* -- | ||
*/ | ||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
|
||
// Creates an instance of book | ||
Book book = new Book(); | ||
book.setTitle("The Hitchhiker's Guide to the Galaxy"); | ||
book.setPrice(12.5F); | ||
book.setDescription("Science fiction comedy series created by Douglas Adams."); | ||
book.setIsbn("1-84023-742-2"); | ||
book.setNbOfPage(354); | ||
book.setIllustrations(false); | ||
|
||
// Gets an entity manager and a transaction | ||
EntityManagerFactory emf = Persistence.createEntityManagerFactory("chapter02PU"); | ||
EntityManager em = emf.createEntityManager(); | ||
|
||
// Persists the book to the database | ||
EntityTransaction tx = em.getTransaction(); | ||
tx.begin(); | ||
em.persist(book); | ||
tx.commit(); | ||
|
||
em.close(); | ||
emf.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<persistence xmlns="http://java.sun.com/xml/ns/persistence" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" | ||
version="2.0"> | ||
|
||
|
||
<persistence-unit name="chapter02PU" transaction-type="RESOURCE_LOCAL"> | ||
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> | ||
<class>org.beginningee6.book.chapter02.Book</class> | ||
<properties> | ||
<property name="eclipselink.target-database" value="DERBY"/> | ||
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/> | ||
<property name="eclipselink.logging.level" value="INFO"/> | ||
<!-- Embedded mode --> | ||
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/> | ||
<property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:chapter02DB;create=true"/> | ||
<!-- Client Server mode --> | ||
<!--<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>--> | ||
<!--<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/chapter02DB;create=true"/>--> | ||
<property name="javax.persistence.jdbc.user" value="APP"/> | ||
<property name="javax.persistence.jdbc.password" value="APP"/> | ||
</properties> | ||
</persistence-unit> | ||
</persistence> |
77 changes: 77 additions & 0 deletions
77
chapter02/src/test/java/org/beginningee6/book/chapter02/BookTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.beginningee6.book.chapter02; | ||
|
||
import org.beginningee6.book.chapter02.Book; | ||
import static junit.framework.Assert.assertNotNull; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
import javax.persistence.EntityTransaction; | ||
import javax.persistence.Persistence; | ||
import java.sql.SQLException; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Antonio Goncalves | ||
* APress Book - Beginning Java EE 6 with Glassfish | ||
* http://www.apress.com/ | ||
* http://www.antoniogoncalves.org | ||
* -- | ||
*/ | ||
public class BookTest { | ||
|
||
// ====================================== | ||
// = Attributes = | ||
// ====================================== | ||
private static EntityManagerFactory emf; | ||
private static EntityManager em; | ||
private static EntityTransaction tx; | ||
|
||
// ====================================== | ||
// = Lifecycle Methods = | ||
// ====================================== | ||
@BeforeClass | ||
public static void initEntityManager() throws Exception { | ||
emf = Persistence.createEntityManagerFactory("chapter02PU"); | ||
em = emf.createEntityManager(); | ||
} | ||
|
||
@AfterClass | ||
public static void closeEntityManager() throws SQLException { | ||
if (em != null) em.close(); | ||
if (emf != null) emf.close(); | ||
} | ||
|
||
@Before | ||
public void initTransaction() { | ||
tx = em.getTransaction(); | ||
} | ||
|
||
// ====================================== | ||
// = Unit tests = | ||
// ====================================== | ||
@Test | ||
public void shouldCreateABook() throws Exception { | ||
// Creates an instance of book | ||
Book book = new Book(); | ||
book.setTitle("The Hitchhiker's Guide to the Galaxy"); | ||
book.setPrice(12.5F); | ||
book.setDescription("Science fiction comedy book"); | ||
book.setIsbn("1-84023-742-2"); | ||
book.setNbOfPage(354); | ||
book.setIllustrations(false); | ||
// Persists the book to the database | ||
tx.begin(); | ||
em.persist(book); | ||
tx.commit(); | ||
assertNotNull("ID should not be null", book.getId()); | ||
// Retrieves all the books from the database | ||
List<Book> books = em.createNamedQuery("findAllBooks").getResultList(); | ||
assertEquals(1, books.size()); | ||
} | ||
} |
Oops, something went wrong.