diff --git a/4146.pdf b/4146.pdf new file mode 100644 index 0000000..e0accdd Binary files /dev/null and b/4146.pdf differ diff --git a/4147.pdf b/4147.pdf new file mode 100644 index 0000000..8eefa3b Binary files /dev/null and b/4147.pdf differ diff --git a/9781430209638.jpg b/9781430209638.jpg new file mode 100644 index 0000000..711968d Binary files /dev/null and b/9781430209638.jpg differ diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..654113f --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,27 @@ +Freeware License, some rights reserved + +Copyright (c) 2008 Yuli Vasiliev + +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. + + diff --git a/README.md b/README.md new file mode 100644 index 0000000..d3f5aad --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +#Apress Source Code + +This repository accompanies [*Beginning Database-Driven Application Development in Java EE*](http://www.apress.com/9781430209638) by Yuli Vasiliev (Apress, 2008). + +![Cover image](9781430209638.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. diff --git a/ch1/HelloWorld/WEB-INF/sun-web.xml b/ch1/HelloWorld/WEB-INF/sun-web.xml new file mode 100644 index 0000000..1bee796 --- /dev/null +++ b/ch1/HelloWorld/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /HelloWorld + \ No newline at end of file diff --git a/ch1/HelloWorld/WEB-INF/web.xml b/ch1/HelloWorld/WEB-INF/web.xml new file mode 100644 index 0000000..58000ac --- /dev/null +++ b/ch1/HelloWorld/WEB-INF/web.xml @@ -0,0 +1,12 @@ + + + + + index.jsp + + + \ No newline at end of file diff --git a/ch1/HelloWorld/helloworld.war b/ch1/HelloWorld/helloworld.war new file mode 100644 index 0000000..01b7405 Binary files /dev/null and b/ch1/HelloWorld/helloworld.war differ diff --git a/ch1/HelloWorld/index.jsp b/ch1/HelloWorld/index.jsp new file mode 100644 index 0000000..ae10708 --- /dev/null +++ b/ch1/HelloWorld/index.jsp @@ -0,0 +1,10 @@ +<%@page contentType="text/html"%> +<%@page pageEncoding="UTF-8"%> + + + Hello World Page + + +

Hello World!

+ + \ No newline at end of file diff --git a/ch1/HelloWorldWithIDE/build.xml b/ch1/HelloWorldWithIDE/build.xml new file mode 100644 index 0000000..acf7f80 --- /dev/null +++ b/ch1/HelloWorldWithIDE/build.xml @@ -0,0 +1,66 @@ + + + + + + Builds, tests, and runs the project HelloWorldWithIDE. + + + diff --git a/ch1/HelloWorldWithIDE/build/web/META-INF/MANIFEST.MF b/ch1/HelloWorldWithIDE/build/web/META-INF/MANIFEST.MF new file mode 100644 index 0000000..59499bc --- /dev/null +++ b/ch1/HelloWorldWithIDE/build/web/META-INF/MANIFEST.MF @@ -0,0 +1,2 @@ +Manifest-Version: 1.0 + diff --git a/ch1/HelloWorldWithIDE/build/web/WEB-INF/sun-web.xml b/ch1/HelloWorldWithIDE/build/web/WEB-INF/sun-web.xml new file mode 100644 index 0000000..0c5f373 --- /dev/null +++ b/ch1/HelloWorldWithIDE/build/web/WEB-INF/sun-web.xml @@ -0,0 +1,14 @@ + + + + /HelloWorldWithIDE + + + + Enable debug info compilation in the generated servlet class + + + Maintain a one-to-one correspondence between static content and the generated servlet class' java code + + + diff --git a/ch1/HelloWorldWithIDE/build/web/WEB-INF/web.xml b/ch1/HelloWorldWithIDE/build/web/WEB-INF/web.xml new file mode 100644 index 0000000..b6f2242 --- /dev/null +++ b/ch1/HelloWorldWithIDE/build/web/WEB-INF/web.xml @@ -0,0 +1,17 @@ + + + + + + 30 + + + + + index.jsp + + + diff --git a/ch1/HelloWorldWithIDE/build/web/index.jsp b/ch1/HelloWorldWithIDE/build/web/index.jsp new file mode 100644 index 0000000..7f75098 --- /dev/null +++ b/ch1/HelloWorldWithIDE/build/web/index.jsp @@ -0,0 +1,36 @@ +<%@page contentType="text/html"%> +<%@page pageEncoding="UTF-8"%> +<%-- +The taglib directive below imports the JSTL library. If you uncomment it, +you must also add the JSTL library to the project. The Add Library... action +on Libraries node in Projects view can be used to add the JSTL 1.1 library. +--%> +<%-- +<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> +--%> + + + + + + + JSP Page + + + +

Hello World!!!

+ + <%-- + This example uses JSTL, uncomment the taglib directive above. + To test, display the page like this: index.jsp?sayHello=true&name=Murphy + --%> + <%-- + + + Hello ${param.name}! + + --%> + + + diff --git a/ch1/HelloWorldWithIDE/dist/HelloWorldWithIDE.war b/ch1/HelloWorldWithIDE/dist/HelloWorldWithIDE.war new file mode 100644 index 0000000..79af749 Binary files /dev/null and b/ch1/HelloWorldWithIDE/dist/HelloWorldWithIDE.war differ diff --git a/ch1/HelloWorldWithIDE/nbproject/ant-deploy.xml b/ch1/HelloWorldWithIDE/nbproject/ant-deploy.xml new file mode 100644 index 0000000..7ec8e30 --- /dev/null +++ b/ch1/HelloWorldWithIDE/nbproject/ant-deploy.xml @@ -0,0 +1,97 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ${lefty}?xml version="1.0" encoding="UTF-8"?${righty} +${lefty}!DOCTYPE resources PUBLIC + "-//Sun Microsystems Inc.//DTD Application Server 9.0 Resource Definitions //EN" + "${sjsas.root}/lib/dtds/sun-resources_1_2.dtd"${righty} +${lefty}resources${righty} +${lefty}/resources${righty} + + + + + + + +]]> + + + + +]]> + + + + + + + ]]> + + + + + + + + ]]> + ]]> + + + + + + + + + + + + + + + + + + diff --git a/ch1/HelloWorldWithIDE/nbproject/build-impl.xml b/ch1/HelloWorldWithIDE/nbproject/build-impl.xml new file mode 100644 index 0000000..e53e06c --- /dev/null +++ b/ch1/HelloWorldWithIDE/nbproject/build-impl.xml @@ -0,0 +1,690 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set build.web.dir + Must set build.generated.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.war + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.jsp.includes + + + + + + + + + + + + + + + + + + + + Must select a file in the IDE or set jsp.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Browser not found, cannot launch the deployed application. Try to set the BROWSER environment variable. + + + Launching ${browse.url} + + + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ch1/HelloWorldWithIDE/nbproject/genfiles.properties b/ch1/HelloWorldWithIDE/nbproject/genfiles.properties new file mode 100644 index 0000000..8b4171a --- /dev/null +++ b/ch1/HelloWorldWithIDE/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=cf13b54c +build.xml.script.CRC32=d0e6b3e4 +build.xml.stylesheet.CRC32=95be3570 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=cf13b54c +nbproject/build-impl.xml.script.CRC32=fce1b4c1 +nbproject/build-impl.xml.stylesheet.CRC32=385ea7e4 diff --git a/ch1/HelloWorldWithIDE/nbproject/private/private.properties b/ch1/HelloWorldWithIDE/nbproject/private/private.properties new file mode 100644 index 0000000..640ece8 --- /dev/null +++ b/ch1/HelloWorldWithIDE/nbproject/private/private.properties @@ -0,0 +1,10 @@ +deploy.ant.properties.file=D:\\Documents and Settings\\Administrator\\.netbeans\\5.5.1\\glassfish_1.properties +j2ee.platform.classpath=C:\\glassfish\\lib\\javaee.jar:C:\\glassfish\\lib\\jsf-impl.jar:C:\\glassfish\\lib\\activation.jar:C:\\glassfish\\lib\\appserv-tags.jar:C:\\glassfish\\lib\\mail.jar:C:\\glassfish\\lib\\appserv-jstl.jar:C:\\glassfish\\lib\\webservices-tools.jar:C:\\glassfish\\lib\\webservices-rt.jar:C:\\glassfish\\lib\\appserv-ws.jar +j2ee.platform.is.jsr109=true +j2ee.platform.wscompile.classpath=C:\\glassfish\\lib\\j2ee.jar:C:\\glassfish\\lib\\saaj-api.jar:C:\\glassfish\\lib\\saaj-impl.jar:C:\\glassfish\\lib\\jaxrpc-api.jar:C:\\glassfish\\lib\\jaxrpc-impl.jar:C:\\glassfish\\lib\\endorsed\\jaxp-api.jar:C:\\glassfish\\lib\\appserv-ws.jar:C:\\glassfish\\lib\\webservices-tools.jar:C:\\glassfish\\lib\\webservices-rt.jar +j2ee.platform.wsgen.classpath=C:\\glassfish\\lib\\webservices-tools.jar:C:\\glassfish\\lib\\webservices-rt.jar:C:\\glassfish\\lib\\tools.jar:C:\\glassfish\\lib\\appserv-jstl.jar:C:\\glassfish\\lib\\javaee.jar:C:\\glassfish\\lib\\appserv-ws.jar:C:\\glassfish\\lib\\mail.jar:C:\\glassfish\\lib\\activation.jar +j2ee.platform.wsimport.classpath=C:\\glassfish\\lib\\webservices-tools.jar:C:\\glassfish\\lib\\webservices-rt.jar:C:\\glassfish\\lib\\tools.jar:C:\\glassfish\\lib\\appserv-jstl.jar:C:\\glassfish\\lib\\javaee.jar:C:\\glassfish\\lib\\appserv-ws.jar:C:\\glassfish\\lib\\mail.jar:C:\\glassfish\\lib\\activation.jar +j2ee.platform.wsit.classpath= +j2ee.server.instance=[C:\\glassfish]deployer:Sun:AppServer::localhost:4848 +jaxws.endorsed.dir=C:\\netbeans-5.5.1\\ide7\\modules\\ext\\jaxws21\\api +user.properties.file=D:\\Documents and Settings\\Administrator\\.netbeans\\5.5.1\\build.properties diff --git a/ch1/HelloWorldWithIDE/nbproject/private/private.xml b/ch1/HelloWorldWithIDE/nbproject/private/private.xml new file mode 100644 index 0000000..c1f155a --- /dev/null +++ b/ch1/HelloWorldWithIDE/nbproject/private/private.xml @@ -0,0 +1,4 @@ + + + + diff --git a/ch1/HelloWorldWithIDE/nbproject/project.properties b/ch1/HelloWorldWithIDE/nbproject/project.properties new file mode 100644 index 0000000..0537049 --- /dev/null +++ b/ch1/HelloWorldWithIDE/nbproject/project.properties @@ -0,0 +1,66 @@ +build.classes.dir=${build.web.dir}/WEB-INF/classes +build.classes.excludes=**/*.java,**/*.form +build.dir=build +build.ear.classes.dir=${build.ear.web.dir}/WEB-INF/classes +build.ear.web.dir=${build.dir}/ear-module +build.generated.dir=${build.dir}/generated +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +build.web.dir=${build.dir}/web +build.web.excludes=${build.classes.excludes} +client.urlPart= +compile.jsps=false +conf.dir=${source.root}/conf +debug.classpath=${javac.classpath}:${build.classes.dir}:${build.ear.classes.dir} +debug.test.classpath=\ + ${run.test.classpath} +display.browser=true +dist.dir=dist +dist.ear.war=${dist.dir}/${war.ear.name} +dist.javadoc.dir=${dist.dir}/javadoc +dist.war=${dist.dir}/${war.name} +j2ee.platform=1.5 +j2ee.server.type=J2EE +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.debug=true +javac.deprecation=false +javac.source=1.5 +javac.target=1.5 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir}:\ + ${libs.junit.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding= +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.preview=true +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +jspcompilation.classpath=${jspc.classpath}:${javac.classpath} +lib.dir=${web.docbase.dir}/WEB-INF/lib +no.dependencies=false +platform.active=default_platform +resource.dir=setup +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +# Space-separated list of JVM arguments used when running class with main method +# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value): +runmain.jvmargs= +source.root=src +src.dir=${source.root}/java +test.src.dir=test +war.content.additional= +war.ear.name=HelloWorldWithIDE.war +war.name=HelloWorldWithIDE.war +war.package=true +web.docbase.dir=web diff --git a/ch1/HelloWorldWithIDE/nbproject/project.xml b/ch1/HelloWorldWithIDE/nbproject/project.xml new file mode 100644 index 0000000..458abec --- /dev/null +++ b/ch1/HelloWorldWithIDE/nbproject/project.xml @@ -0,0 +1,18 @@ + + + org.netbeans.modules.web.project + + + HelloWorldWithIDE + 1.6 + + + + + + + + + + + diff --git a/ch1/HelloWorldWithIDE/src/conf/MANIFEST.MF b/ch1/HelloWorldWithIDE/src/conf/MANIFEST.MF new file mode 100644 index 0000000..59499bc --- /dev/null +++ b/ch1/HelloWorldWithIDE/src/conf/MANIFEST.MF @@ -0,0 +1,2 @@ +Manifest-Version: 1.0 + diff --git a/ch1/HelloWorldWithIDE/web/WEB-INF/sun-web.xml b/ch1/HelloWorldWithIDE/web/WEB-INF/sun-web.xml new file mode 100644 index 0000000..0c5f373 --- /dev/null +++ b/ch1/HelloWorldWithIDE/web/WEB-INF/sun-web.xml @@ -0,0 +1,14 @@ + + + + /HelloWorldWithIDE + + + + Enable debug info compilation in the generated servlet class + + + Maintain a one-to-one correspondence between static content and the generated servlet class' java code + + + diff --git a/ch1/HelloWorldWithIDE/web/WEB-INF/web.xml b/ch1/HelloWorldWithIDE/web/WEB-INF/web.xml new file mode 100644 index 0000000..b6f2242 --- /dev/null +++ b/ch1/HelloWorldWithIDE/web/WEB-INF/web.xml @@ -0,0 +1,17 @@ + + + + + + 30 + + + + + index.jsp + + + diff --git a/ch1/HelloWorldWithIDE/web/index.jsp b/ch1/HelloWorldWithIDE/web/index.jsp new file mode 100644 index 0000000..0eef11f --- /dev/null +++ b/ch1/HelloWorldWithIDE/web/index.jsp @@ -0,0 +1,28 @@ +<%@page contentType="text/html"%> +<%@page pageEncoding="UTF-8"%> + + + + + + + JSP Page + + + +

Hello World!!!

+ + <%-- + This example uses JSTL, uncomment the taglib directive above. + To test, display the page like this: index.jsp?sayHello=true&name=Murphy + --%> + <%-- + + + Hello ${param.name}! + + --%> + + + diff --git a/ch10/appmanagedem/ejbtarget/META-INF/persistence.xml b/ch10/appmanagedem/ejbtarget/META-INF/persistence.xml new file mode 100644 index 0000000..72765d1 --- /dev/null +++ b/ch10/appmanagedem/ejbtarget/META-INF/persistence.xml @@ -0,0 +1,9 @@ + + + + jdbc/mysqlpool + + + jdbc/mysqlpool + + diff --git a/ch10/appmanagedem/ejbtarget/ejbjpa/ejb/OrderTest.class b/ch10/appmanagedem/ejbtarget/ejbjpa/ejb/OrderTest.class new file mode 100644 index 0000000..46c4135 Binary files /dev/null and b/ch10/appmanagedem/ejbtarget/ejbjpa/ejb/OrderTest.class differ diff --git a/ch10/appmanagedem/ejbtarget/ejbjpa/ejb/OrderTestBean.class b/ch10/appmanagedem/ejbtarget/ejbjpa/ejb/OrderTestBean.class new file mode 100644 index 0000000..683a29c Binary files /dev/null and b/ch10/appmanagedem/ejbtarget/ejbjpa/ejb/OrderTestBean.class differ diff --git a/ch10/appmanagedem/ejbtarget/ejbjpa/entities/Customer.class b/ch10/appmanagedem/ejbtarget/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..855839f Binary files /dev/null and b/ch10/appmanagedem/ejbtarget/ejbjpa/entities/Customer.class differ diff --git a/ch10/appmanagedem/ejbtarget/ejbjpa/entities/Employee.class b/ch10/appmanagedem/ejbtarget/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..380c0cf Binary files /dev/null and b/ch10/appmanagedem/ejbtarget/ejbjpa/entities/Employee.class differ diff --git a/ch10/appmanagedem/ejbtarget/ejbjpa/entities/Order.class b/ch10/appmanagedem/ejbtarget/ejbjpa/entities/Order.class new file mode 100644 index 0000000..7c7e67c Binary files /dev/null and b/ch10/appmanagedem/ejbtarget/ejbjpa/entities/Order.class differ diff --git a/ch10/appmanagedem/ejbtarget/ejborder.jar b/ch10/appmanagedem/ejbtarget/ejborder.jar new file mode 100644 index 0000000..afb6671 Binary files /dev/null and b/ch10/appmanagedem/ejbtarget/ejborder.jar differ diff --git a/ch10/appmanagedem/src/ejbjpa/ejb/OrderTest.java b/ch10/appmanagedem/src/ejbjpa/ejb/OrderTest.java new file mode 100644 index 0000000..7acdd6d --- /dev/null +++ b/ch10/appmanagedem/src/ejbjpa/ejb/OrderTest.java @@ -0,0 +1,7 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +@Remote +public interface OrderTest { + public Integer setOrder(Integer cust_id, Integer empno); + public String changeOrderEmpTest(Integer pono, Integer empno); +} diff --git a/ch10/appmanagedem/src/ejbjpa/ejb/OrderTestBean.java b/ch10/appmanagedem/src/ejbjpa/ejb/OrderTestBean.java new file mode 100644 index 0000000..5a4d9d4 --- /dev/null +++ b/ch10/appmanagedem/src/ejbjpa/ejb/OrderTestBean.java @@ -0,0 +1,68 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.transaction.UserTransaction; +import javax.persistence.PersistenceUnit; +import javax.annotation.Resource; +import java.util.List; + +import ejbjpa.entities.*; +@Stateless +public class OrderTestBean implements OrderTest { + @PersistenceUnit(unitName="appemtest1-pu") + private EntityManagerFactory emf; + @PersistenceUnit(unitName="appemtest2-pu") + private EntityManagerFactory emf2; + @Resource + UserTransaction utx; + public Integer setOrder(Integer cust_id, Integer empno) { + Integer order_pono; + try { + EntityManager em = emf.createEntityManager(); + Customer cust = (Customer) em.find(Customer.class, cust_id); + Employee emp = (Employee) em.find(Employee.class, empno); + Order order1 = new Order(); + order1.setCustomer(cust); + order1.setEmployee(emp); + em.getTransaction().begin(); + em.persist(order1); + List orderList = em.createQuery("SELECT o FROM Order o") + .getResultList(); + em.refresh(order1); + em.flush(); + em.refresh(order1); + order_pono = order1.getPono(); + em.getTransaction().commit(); + em.close(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return order_pono; + } + public String changeOrderEmpTest(Integer pono, Integer empno) { + String order_details; + try { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + Employee emp = (Employee) em.find(Employee.class, empno); + Order order1 = (Order) em.find(Order.class, pono); + order1.setEmployee(emp); + em.merge(order1); + order_details = "order "+ order1.getPono()+ " placed via: " + order1.getEmployee().getLastname()+"
"; + EntityManager em2 = emf2.createEntityManager(); + Order order2 = (Order) em2.find(Order.class, pono); + order_details = order_details+"order "+ order2.getPono()+ " placed via: " + order2.getEmployee().getLastname()+"
"; + em.getTransaction().commit(); + em2.refresh(order2); + order_details = order_details+"order "+ order2.getPono()+ " placed via: " + order2.getEmployee().getLastname(); + em.close(); + em2.close(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return order_details; + } +} \ No newline at end of file diff --git a/ch10/appmanagedem/src/ejbjpa/entities/Customer.java b/ch10/appmanagedem/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..aa187c0 --- /dev/null +++ b/ch10/appmanagedem/src/ejbjpa/entities/Customer.java @@ -0,0 +1,64 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "CUST_NAME", nullable = false) + private String cust_name; + @Column(name = "LOC_ID") + private Integer loc_id; + @Column(name = "EMAIL", nullable = false) + private String email; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToMany(mappedBy="customer", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCust_name() { + return this.cust_name; + } + public void setCust_name(String cust_name) { + this.cust_name = cust_name; + } + public Integer getLoc_id() { + return this.loc_id; + } + public void setLoc_id(Integer loc_id) { + this.loc_id = loc_id; + } + public String getEmail() { + return this.email; + } + public void setEmail(String email) { + this.email = email; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/ch10/appmanagedem/src/ejbjpa/entities/Employee.java b/ch10/appmanagedem/src/ejbjpa/entities/Employee.java new file mode 100644 index 0000000..9751c22 --- /dev/null +++ b/ch10/appmanagedem/src/ejbjpa/entities/Employee.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "EMPLOYEES") +public class Employee implements Serializable { + @Id + @Column(name = "EMPNO") + private Integer empno; + @Column(name = "firstname", nullable = false) + private String firstname; + @Column(name = "lastname", nullable = false) + private String lastname; + @OneToMany(mappedBy="employee", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Employee() { + } + public Integer getEmpno() { + return this.empno; + } + public void setEmpno(Integer empno) { + this.empno = empno; + } + public String getFirstname() { + return this.firstname; + } + public void setFirstname(String firstname) { + this.firstname = firstname; + } + public String getLastname() { + return this.lastname; + } + public void setLastname(String lastname) { + this.lastname = lastname; + } +} diff --git a/ch10/appmanagedem/src/ejbjpa/entities/Order.java b/ch10/appmanagedem/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..b9aa6e0 --- /dev/null +++ b/ch10/appmanagedem/src/ejbjpa/entities/Order.java @@ -0,0 +1,76 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import static javax.persistence.TemporalType.DATE; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.TableGenerator; +import java.util.Date; +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + /* @TableGenerator(name = "ponoGen", + table = "PONO_GEN_TABLE", + pkColumnName = "GEN_NAME", + valueColumnName = "GEN_VALUE", + pkColumnValue = "PONO_GEN", + allocationSize = 1) */ + @Id + // @GeneratedValue(strategy=GenerationType.TABLE, generator = "ponoGen") + @GeneratedValue(strategy=GenerationType.IDENTITY) + @Column(name = "PONO") + private Integer pono; + @Column(name = "SHIPPING_DATE", nullable = false) + @Temporal(DATE) + private Date shipping_date; + @Column(name = "DELIVERY_ESTIMATE", nullable = false) + private String delivery_estimate; + @ManyToOne + @JoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ID") + private Customer customer; + @ManyToOne + @JoinColumn( + name="EMPNO", + referencedColumnName="EMPNO") + private Employee employee; + public Order() { + } + public Customer getCustomer() { + return this.customer; + } + public void setCustomer(Customer customer) { + this.customer = customer; + } + public Employee getEmployee() { + return this.employee; + } + public void setEmployee (Employee employee) { + this.employee = employee; + } + public Integer getPono () { + return this.pono; + } + public void setPono (Integer pono) { + this.pono = pono; + } + public Date getShipping_date () { + return this.shipping_date; + } + public void setShipping_date (Date shipping_date) { + this.shipping_date = shipping_date; + } + public String getDelivery_estimate () { + return this.delivery_estimate; + } + public void setDelivery_estimate (String delivery_estimate) { + this.delivery_estimate = delivery_estimate; + } +} diff --git a/ch10/appmanagedem/src/ejbjpa/servlets/EmEjbTestServlet.java b/ch10/appmanagedem/src/ejbjpa/servlets/EmEjbTestServlet.java new file mode 100644 index 0000000..7f5a3f3 --- /dev/null +++ b/ch10/appmanagedem/src/ejbjpa/servlets/EmEjbTestServlet.java @@ -0,0 +1,25 @@ +package ejbjpa.servlets; +import java.io.*; +import javax.servlet.*; +import javax.servlet.http.*; +import javax.ejb.EJB; +import ejbjpa.entities.*; +import ejbjpa.ejb.*; +public class EmEjbTestServlet extends HttpServlet { + @EJB private OrderTest orderTest; + public void doGet( + HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + response.setBufferSize(8192); + PrintWriter out = response.getWriter(); + try{ + Integer pono = orderTest.setOrder(2,1); + out.println("Created order "+ pono +"
"); + out.println(orderTest.changeOrderEmpTest(pono,2)); + } + catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/ch10/appmanagedem/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class b/ch10/appmanagedem/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class new file mode 100644 index 0000000..5555772 Binary files /dev/null and b/ch10/appmanagedem/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class differ diff --git a/ch10/appmanagedem/target/WEB-INF/lib/ejborder.jar b/ch10/appmanagedem/target/WEB-INF/lib/ejborder.jar new file mode 100644 index 0000000..afb6671 Binary files /dev/null and b/ch10/appmanagedem/target/WEB-INF/lib/ejborder.jar differ diff --git a/ch10/appmanagedem/target/WEB-INF/sun-web.xml b/ch10/appmanagedem/target/WEB-INF/sun-web.xml new file mode 100644 index 0000000..fe358bb --- /dev/null +++ b/ch10/appmanagedem/target/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /appemtest + diff --git a/ch10/appmanagedem/target/WEB-INF/web.xml b/ch10/appmanagedem/target/WEB-INF/web.xml new file mode 100644 index 0000000..c5a2358 --- /dev/null +++ b/ch10/appmanagedem/target/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + + + EmEjbTestServlet + ejbjpa.servlets.EmEjbTestServlet + + + EmEjbTestServlet + /emejbtestservlet + + + diff --git a/ch10/appmanagedem/target/appmanagedem.war b/ch10/appmanagedem/target/appmanagedem.war new file mode 100644 index 0000000..b1faa9b Binary files /dev/null and b/ch10/appmanagedem/target/appmanagedem.war differ diff --git a/ch10/callbackmethods/ejbtarget/META-INF/persistence.xml b/ch10/callbackmethods/ejbtarget/META-INF/persistence.xml new file mode 100644 index 0000000..f0b9d95 --- /dev/null +++ b/ch10/callbackmethods/ejbtarget/META-INF/persistence.xml @@ -0,0 +1,6 @@ + + + + jdbc/mysqlpool + + diff --git a/ch10/callbackmethods/ejbtarget/ejbjpa/ejb/OrderTest.class b/ch10/callbackmethods/ejbtarget/ejbjpa/ejb/OrderTest.class new file mode 100644 index 0000000..7971984 Binary files /dev/null and b/ch10/callbackmethods/ejbtarget/ejbjpa/ejb/OrderTest.class differ diff --git a/ch10/callbackmethods/ejbtarget/ejbjpa/ejb/OrderTestBean.class b/ch10/callbackmethods/ejbtarget/ejbjpa/ejb/OrderTestBean.class new file mode 100644 index 0000000..b7ef1da Binary files /dev/null and b/ch10/callbackmethods/ejbtarget/ejbjpa/ejb/OrderTestBean.class differ diff --git a/ch10/callbackmethods/ejbtarget/ejbjpa/entities/Customer.class b/ch10/callbackmethods/ejbtarget/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..855839f Binary files /dev/null and b/ch10/callbackmethods/ejbtarget/ejbjpa/entities/Customer.class differ diff --git a/ch10/callbackmethods/ejbtarget/ejbjpa/entities/Employee.class b/ch10/callbackmethods/ejbtarget/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..380c0cf Binary files /dev/null and b/ch10/callbackmethods/ejbtarget/ejbjpa/entities/Employee.class differ diff --git a/ch10/callbackmethods/ejbtarget/ejbjpa/entities/Order.class b/ch10/callbackmethods/ejbtarget/ejbjpa/entities/Order.class new file mode 100644 index 0000000..8a571c2 Binary files /dev/null and b/ch10/callbackmethods/ejbtarget/ejbjpa/entities/Order.class differ diff --git a/ch10/callbackmethods/ejbtarget/ejborder.jar b/ch10/callbackmethods/ejbtarget/ejborder.jar new file mode 100644 index 0000000..7ac8740 Binary files /dev/null and b/ch10/callbackmethods/ejbtarget/ejborder.jar differ diff --git a/ch10/callbackmethods/src/ejbjpa/ejb/OrderTest.java b/ch10/callbackmethods/src/ejbjpa/ejb/OrderTest.java new file mode 100644 index 0000000..95cf485 --- /dev/null +++ b/ch10/callbackmethods/src/ejbjpa/ejb/OrderTest.java @@ -0,0 +1,8 @@ +package ejbjpa.ejb; +import java.util.Date; + +import javax.ejb.Remote; +@Remote +public interface OrderTest { + public String[] setOrder(Integer cust_id, Integer empno, Date shipping_date, String delivery_estimate); +} diff --git a/ch10/callbackmethods/src/ejbjpa/ejb/OrderTestBean.java b/ch10/callbackmethods/src/ejbjpa/ejb/OrderTestBean.java new file mode 100644 index 0000000..6495931 --- /dev/null +++ b/ch10/callbackmethods/src/ejbjpa/ejb/OrderTestBean.java @@ -0,0 +1,36 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.Date; +import ejbjpa.entities.*; + +@Stateless +public class OrderTestBean implements OrderTest { + @PersistenceContext + private EntityManager em; + public String[] setOrder(Integer cust_id, Integer empno, Date shipping_date, String delivery_estimate) { + String[] order_details = new String[2]; + try { + Customer cust = (Customer) em.find(Customer.class, cust_id); + Employee emp = (Employee) em.find(Employee.class, empno); + Order order1 = new Order(); + order1.setCustomer(cust); + order1.setEmployee(emp); + order1.setShipping_date(shipping_date); + order1.setDelivery_estimate(delivery_estimate); + em.persist(order1); + Date date = order1.getShipping_date(); + date = order1.getShipping_date(); + order_details[1] = date.toString(); + em.flush(); + em.refresh(order1); + order_details[0] = order1.getPono().toString(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return order_details; + } +} \ No newline at end of file diff --git a/ch10/callbackmethods/src/ejbjpa/entities/Customer.java b/ch10/callbackmethods/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..aa187c0 --- /dev/null +++ b/ch10/callbackmethods/src/ejbjpa/entities/Customer.java @@ -0,0 +1,64 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "CUST_NAME", nullable = false) + private String cust_name; + @Column(name = "LOC_ID") + private Integer loc_id; + @Column(name = "EMAIL", nullable = false) + private String email; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToMany(mappedBy="customer", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCust_name() { + return this.cust_name; + } + public void setCust_name(String cust_name) { + this.cust_name = cust_name; + } + public Integer getLoc_id() { + return this.loc_id; + } + public void setLoc_id(Integer loc_id) { + this.loc_id = loc_id; + } + public String getEmail() { + return this.email; + } + public void setEmail(String email) { + this.email = email; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/ch10/callbackmethods/src/ejbjpa/entities/Employee.java b/ch10/callbackmethods/src/ejbjpa/entities/Employee.java new file mode 100644 index 0000000..9751c22 --- /dev/null +++ b/ch10/callbackmethods/src/ejbjpa/entities/Employee.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "EMPLOYEES") +public class Employee implements Serializable { + @Id + @Column(name = "EMPNO") + private Integer empno; + @Column(name = "firstname", nullable = false) + private String firstname; + @Column(name = "lastname", nullable = false) + private String lastname; + @OneToMany(mappedBy="employee", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Employee() { + } + public Integer getEmpno() { + return this.empno; + } + public void setEmpno(Integer empno) { + this.empno = empno; + } + public String getFirstname() { + return this.firstname; + } + public void setFirstname(String firstname) { + this.firstname = firstname; + } + public String getLastname() { + return this.lastname; + } + public void setLastname(String lastname) { + this.lastname = lastname; + } +} diff --git a/ch10/callbackmethods/src/ejbjpa/entities/Order.java b/ch10/callbackmethods/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..e47273e --- /dev/null +++ b/ch10/callbackmethods/src/ejbjpa/entities/Order.java @@ -0,0 +1,91 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import static javax.persistence.TemporalType.DATE; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import java.util.*; +import javax.persistence.PrePersist; + +import java.util.Date; +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + @Id + @GeneratedValue(strategy=GenerationType.IDENTITY) + @Column(name = "PONO") + private Integer pono; + @Column(name = "SHIPPING_DATE", nullable = false) + @Temporal(DATE) + private Date shipping_date; + @Column(name = "DELIVERY_ESTIMATE", nullable = false) + private String delivery_estimate; + @ManyToOne + @JoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ID") + private Customer customer; + @ManyToOne + @JoinColumn( + name="EMPNO", + referencedColumnName="EMPNO") + private Employee employee; + @PrePersist + private void setShippingDate() { + if (this.getShipping_date() == null) + { + Calendar calendar = new GregorianCalendar(); + long weekday = calendar.get(Calendar.DAY_OF_WEEK); + long days; + if (weekday == 6) { + days = 3; + } else if (weekday == 7) { + days = 2; + } else { + days =1; + } + Date date = new Date(); + long msDay = 1000 * 60 * 60 * 24; + date.setTime(date.getTime() + msDay * days); + this.setShipping_date(date); + } + } + public Order() { + } + public Customer getCustomer() { + return this.customer; + } + public void setCustomer(Customer customer) { + this.customer = customer; + } + public Employee getEmployee() { + return this.employee; + } + public void setEmployee (Employee employee) { + this.employee = employee; + } + public Integer getPono () { + return this.pono; + } + public void setPono (Integer pono) { + this.pono = pono; + } + public Date getShipping_date () { + return this.shipping_date; + } + public void setShipping_date (Date shipping_date) { + this.shipping_date = shipping_date; + } + public String getDelivery_estimate () { + return this.delivery_estimate; + } + public void setDelivery_estimate (String delivery_estimate) { + this.delivery_estimate = delivery_estimate; + } +} diff --git a/ch10/callbackmethods/src/ejbjpa/servlets/EmEjbTestServlet.java b/ch10/callbackmethods/src/ejbjpa/servlets/EmEjbTestServlet.java new file mode 100644 index 0000000..c928cea --- /dev/null +++ b/ch10/callbackmethods/src/ejbjpa/servlets/EmEjbTestServlet.java @@ -0,0 +1,33 @@ +package ejbjpa.servlets; +import java.io.*; +import javax.servlet.*; +import javax.servlet.http.*; +import javax.ejb.EJB; +import ejbjpa.entities.*; +import ejbjpa.ejb.*; +import java.util.Date; + +public class EmEjbTestServlet extends HttpServlet { + @EJB private OrderTest orderTest; + public void doGet( + HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + response.setBufferSize(8192); + PrintWriter out = response.getWriter(); + try{ + long days =5; + Date date = new Date(); + long msDay = 1000 * 60 * 60 * 24; + date.setTime(date.getTime() + msDay * days); + + String[] details = new String[2]; + details = orderTest.setOrder(2,1, date, null); + out.println("Created order pono: "+ details[0] +"
"); + out.println("Order shipping date: "+ details[1] +"
"); + } + catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/ch10/callbackmethods/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class b/ch10/callbackmethods/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class new file mode 100644 index 0000000..11e23e7 Binary files /dev/null and b/ch10/callbackmethods/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class differ diff --git a/ch10/callbackmethods/target/WEB-INF/lib/ejborder.jar b/ch10/callbackmethods/target/WEB-INF/lib/ejborder.jar new file mode 100644 index 0000000..7ac8740 Binary files /dev/null and b/ch10/callbackmethods/target/WEB-INF/lib/ejborder.jar differ diff --git a/ch10/callbackmethods/target/WEB-INF/sun-web.xml b/ch10/callbackmethods/target/WEB-INF/sun-web.xml new file mode 100644 index 0000000..7e84f31 --- /dev/null +++ b/ch10/callbackmethods/target/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /callbackemtest + diff --git a/ch10/callbackmethods/target/WEB-INF/web.xml b/ch10/callbackmethods/target/WEB-INF/web.xml new file mode 100644 index 0000000..c5a2358 --- /dev/null +++ b/ch10/callbackmethods/target/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + + + EmEjbTestServlet + ejbjpa.servlets.EmEjbTestServlet + + + EmEjbTestServlet + /emejbtestservlet + + + diff --git a/ch10/callbackmethods/target/callbackmethods.war b/ch10/callbackmethods/target/callbackmethods.war new file mode 100644 index 0000000..a7d3995 Binary files /dev/null and b/ch10/callbackmethods/target/callbackmethods.war differ diff --git a/ch10/contmanagedem/ejbtarget/META-INF/persistence.xml b/ch10/contmanagedem/ejbtarget/META-INF/persistence.xml new file mode 100644 index 0000000..0f5fb8d --- /dev/null +++ b/ch10/contmanagedem/ejbtarget/META-INF/persistence.xml @@ -0,0 +1,10 @@ + + + + jdbc/mysqlpool + + + jdbc/mysqlpool + + + diff --git a/ch10/contmanagedem/ejbtarget/ejbjpa/ejb/OrderTest.class b/ch10/contmanagedem/ejbtarget/ejbjpa/ejb/OrderTest.class new file mode 100644 index 0000000..46c4135 Binary files /dev/null and b/ch10/contmanagedem/ejbtarget/ejbjpa/ejb/OrderTest.class differ diff --git a/ch10/contmanagedem/ejbtarget/ejbjpa/ejb/OrderTestBean.class b/ch10/contmanagedem/ejbtarget/ejbjpa/ejb/OrderTestBean.class new file mode 100644 index 0000000..a053a50 Binary files /dev/null and b/ch10/contmanagedem/ejbtarget/ejbjpa/ejb/OrderTestBean.class differ diff --git a/ch10/contmanagedem/ejbtarget/ejbjpa/entities/Customer.class b/ch10/contmanagedem/ejbtarget/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..855839f Binary files /dev/null and b/ch10/contmanagedem/ejbtarget/ejbjpa/entities/Customer.class differ diff --git a/ch10/contmanagedem/ejbtarget/ejbjpa/entities/Employee.class b/ch10/contmanagedem/ejbtarget/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..380c0cf Binary files /dev/null and b/ch10/contmanagedem/ejbtarget/ejbjpa/entities/Employee.class differ diff --git a/ch10/contmanagedem/ejbtarget/ejbjpa/entities/Order.class b/ch10/contmanagedem/ejbtarget/ejbjpa/entities/Order.class new file mode 100644 index 0000000..7c7e67c Binary files /dev/null and b/ch10/contmanagedem/ejbtarget/ejbjpa/entities/Order.class differ diff --git a/ch10/contmanagedem/ejbtarget/ejborder.jar b/ch10/contmanagedem/ejbtarget/ejborder.jar new file mode 100644 index 0000000..5b3085f Binary files /dev/null and b/ch10/contmanagedem/ejbtarget/ejborder.jar differ diff --git a/ch10/contmanagedem/src/ejbjpa/ejb/OrderTest.java b/ch10/contmanagedem/src/ejbjpa/ejb/OrderTest.java new file mode 100644 index 0000000..7acdd6d --- /dev/null +++ b/ch10/contmanagedem/src/ejbjpa/ejb/OrderTest.java @@ -0,0 +1,7 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +@Remote +public interface OrderTest { + public Integer setOrder(Integer cust_id, Integer empno); + public String changeOrderEmpTest(Integer pono, Integer empno); +} diff --git a/ch10/contmanagedem/src/ejbjpa/ejb/OrderTestBean.java b/ch10/contmanagedem/src/ejbjpa/ejb/OrderTestBean.java new file mode 100644 index 0000000..0006451 --- /dev/null +++ b/ch10/contmanagedem/src/ejbjpa/ejb/OrderTestBean.java @@ -0,0 +1,49 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import ejbjpa.entities.*; +@Stateless +public class OrderTestBean implements OrderTest { + @PersistenceContext(unitName="emejbtest-pu") + private EntityManager em; + @PersistenceContext(unitName="contaneremtest-pu") + private EntityManager em2; + + public Integer setOrder(Integer cust_id, Integer empno) { + Integer order_pono; + try { + Customer cust = (Customer) em.find(Customer.class, cust_id); + Employee emp = (Employee) em.find(Employee.class, empno); + Order order1 = new Order(); + order1.setCustomer(cust); + order1.setEmployee(emp); + em.persist(order1); + em.flush(); + em.refresh(order1); + order_pono = order1.getPono(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return order_pono; + } + public String changeOrderEmpTest(Integer pono, Integer empno) { + String order_details; + try { + Employee emp = (Employee) em.find(Employee.class, empno); + Order order1 = (Order) em.find(Order.class, pono); + order1.setEmployee(emp); + em.merge(order1); + order_details = "order "+ order1.getPono()+ " placed via: " + order1.getEmployee().getLastname()+"
"; + Order order2 = (Order) em2.find(Order.class, pono); + order_details = order_details+"order "+ order2.getPono()+ " placed via: " + order2.getEmployee().getLastname()+"
"; + order_details = order_details+"order "+ order1.getPono()+ " placed via: " + order1.getEmployee().getLastname(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return order_details; + } +} \ No newline at end of file diff --git a/ch10/contmanagedem/src/ejbjpa/entities/Customer.java b/ch10/contmanagedem/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..aa187c0 --- /dev/null +++ b/ch10/contmanagedem/src/ejbjpa/entities/Customer.java @@ -0,0 +1,64 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "CUST_NAME", nullable = false) + private String cust_name; + @Column(name = "LOC_ID") + private Integer loc_id; + @Column(name = "EMAIL", nullable = false) + private String email; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToMany(mappedBy="customer", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCust_name() { + return this.cust_name; + } + public void setCust_name(String cust_name) { + this.cust_name = cust_name; + } + public Integer getLoc_id() { + return this.loc_id; + } + public void setLoc_id(Integer loc_id) { + this.loc_id = loc_id; + } + public String getEmail() { + return this.email; + } + public void setEmail(String email) { + this.email = email; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/ch10/contmanagedem/src/ejbjpa/entities/Employee.java b/ch10/contmanagedem/src/ejbjpa/entities/Employee.java new file mode 100644 index 0000000..9751c22 --- /dev/null +++ b/ch10/contmanagedem/src/ejbjpa/entities/Employee.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "EMPLOYEES") +public class Employee implements Serializable { + @Id + @Column(name = "EMPNO") + private Integer empno; + @Column(name = "firstname", nullable = false) + private String firstname; + @Column(name = "lastname", nullable = false) + private String lastname; + @OneToMany(mappedBy="employee", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Employee() { + } + public Integer getEmpno() { + return this.empno; + } + public void setEmpno(Integer empno) { + this.empno = empno; + } + public String getFirstname() { + return this.firstname; + } + public void setFirstname(String firstname) { + this.firstname = firstname; + } + public String getLastname() { + return this.lastname; + } + public void setLastname(String lastname) { + this.lastname = lastname; + } +} diff --git a/ch10/contmanagedem/src/ejbjpa/entities/Order.java b/ch10/contmanagedem/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..b9aa6e0 --- /dev/null +++ b/ch10/contmanagedem/src/ejbjpa/entities/Order.java @@ -0,0 +1,76 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import static javax.persistence.TemporalType.DATE; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.TableGenerator; +import java.util.Date; +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + /* @TableGenerator(name = "ponoGen", + table = "PONO_GEN_TABLE", + pkColumnName = "GEN_NAME", + valueColumnName = "GEN_VALUE", + pkColumnValue = "PONO_GEN", + allocationSize = 1) */ + @Id + // @GeneratedValue(strategy=GenerationType.TABLE, generator = "ponoGen") + @GeneratedValue(strategy=GenerationType.IDENTITY) + @Column(name = "PONO") + private Integer pono; + @Column(name = "SHIPPING_DATE", nullable = false) + @Temporal(DATE) + private Date shipping_date; + @Column(name = "DELIVERY_ESTIMATE", nullable = false) + private String delivery_estimate; + @ManyToOne + @JoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ID") + private Customer customer; + @ManyToOne + @JoinColumn( + name="EMPNO", + referencedColumnName="EMPNO") + private Employee employee; + public Order() { + } + public Customer getCustomer() { + return this.customer; + } + public void setCustomer(Customer customer) { + this.customer = customer; + } + public Employee getEmployee() { + return this.employee; + } + public void setEmployee (Employee employee) { + this.employee = employee; + } + public Integer getPono () { + return this.pono; + } + public void setPono (Integer pono) { + this.pono = pono; + } + public Date getShipping_date () { + return this.shipping_date; + } + public void setShipping_date (Date shipping_date) { + this.shipping_date = shipping_date; + } + public String getDelivery_estimate () { + return this.delivery_estimate; + } + public void setDelivery_estimate (String delivery_estimate) { + this.delivery_estimate = delivery_estimate; + } +} diff --git a/ch10/contmanagedem/src/ejbjpa/servlets/EmEjbTestServlet.java b/ch10/contmanagedem/src/ejbjpa/servlets/EmEjbTestServlet.java new file mode 100644 index 0000000..7f5a3f3 --- /dev/null +++ b/ch10/contmanagedem/src/ejbjpa/servlets/EmEjbTestServlet.java @@ -0,0 +1,25 @@ +package ejbjpa.servlets; +import java.io.*; +import javax.servlet.*; +import javax.servlet.http.*; +import javax.ejb.EJB; +import ejbjpa.entities.*; +import ejbjpa.ejb.*; +public class EmEjbTestServlet extends HttpServlet { + @EJB private OrderTest orderTest; + public void doGet( + HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + response.setBufferSize(8192); + PrintWriter out = response.getWriter(); + try{ + Integer pono = orderTest.setOrder(2,1); + out.println("Created order "+ pono +"
"); + out.println(orderTest.changeOrderEmpTest(pono,2)); + } + catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/ch10/contmanagedem/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class b/ch10/contmanagedem/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class new file mode 100644 index 0000000..5555772 Binary files /dev/null and b/ch10/contmanagedem/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class differ diff --git a/ch10/contmanagedem/target/WEB-INF/lib/ejborder.jar b/ch10/contmanagedem/target/WEB-INF/lib/ejborder.jar new file mode 100644 index 0000000..98ca91e Binary files /dev/null and b/ch10/contmanagedem/target/WEB-INF/lib/ejborder.jar differ diff --git a/ch10/contmanagedem/target/WEB-INF/sun-web.xml b/ch10/contmanagedem/target/WEB-INF/sun-web.xml new file mode 100644 index 0000000..7e5d052 --- /dev/null +++ b/ch10/contmanagedem/target/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /emejbtest + diff --git a/ch10/contmanagedem/target/WEB-INF/web.xml b/ch10/contmanagedem/target/WEB-INF/web.xml new file mode 100644 index 0000000..c5a2358 --- /dev/null +++ b/ch10/contmanagedem/target/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + + + EmEjbTestServlet + ejbjpa.servlets.EmEjbTestServlet + + + EmEjbTestServlet + /emejbtestservlet + + + diff --git a/ch10/contmanagedem/target/contmanagedem.war b/ch10/contmanagedem/target/contmanagedem.war new file mode 100644 index 0000000..e8b9fb8 Binary files /dev/null and b/ch10/contmanagedem/target/contmanagedem.war differ diff --git a/ch10/propagatingem/ejbtarget/META-INF/persistence.xml b/ch10/propagatingem/ejbtarget/META-INF/persistence.xml new file mode 100644 index 0000000..d0747eb --- /dev/null +++ b/ch10/propagatingem/ejbtarget/META-INF/persistence.xml @@ -0,0 +1,7 @@ + + + + jdbc/mysqlpool + + + diff --git a/ch10/propagatingem/ejbtarget/ejbjpa/ejb/PropagationTest.class b/ch10/propagatingem/ejbtarget/ejbjpa/ejb/PropagationTest.class new file mode 100644 index 0000000..7576321 Binary files /dev/null and b/ch10/propagatingem/ejbtarget/ejbjpa/ejb/PropagationTest.class differ diff --git a/ch10/propagatingem/ejbtarget/ejbjpa/ejb/PropagationTestBean.class b/ch10/propagatingem/ejbtarget/ejbjpa/ejb/PropagationTestBean.class new file mode 100644 index 0000000..2fb5f4c Binary files /dev/null and b/ch10/propagatingem/ejbtarget/ejbjpa/ejb/PropagationTestBean.class differ diff --git a/ch10/propagatingem/ejbtarget/ejbjpa/ejb/ShoppingCartTest.class b/ch10/propagatingem/ejbtarget/ejbjpa/ejb/ShoppingCartTest.class new file mode 100644 index 0000000..f583dd5 Binary files /dev/null and b/ch10/propagatingem/ejbtarget/ejbjpa/ejb/ShoppingCartTest.class differ diff --git a/ch10/propagatingem/ejbtarget/ejbjpa/ejb/ShoppingCartTestBean.class b/ch10/propagatingem/ejbtarget/ejbjpa/ejb/ShoppingCartTestBean.class new file mode 100644 index 0000000..3a543fb Binary files /dev/null and b/ch10/propagatingem/ejbtarget/ejbjpa/ejb/ShoppingCartTestBean.class differ diff --git a/ch10/propagatingem/ejbtarget/ejbjpa/entities/Book.class b/ch10/propagatingem/ejbtarget/ejbjpa/entities/Book.class new file mode 100644 index 0000000..c45580c Binary files /dev/null and b/ch10/propagatingem/ejbtarget/ejbjpa/entities/Book.class differ diff --git a/ch10/propagatingem/ejbtarget/ejbjpa/entities/ShoppingCart.class b/ch10/propagatingem/ejbtarget/ejbjpa/entities/ShoppingCart.class new file mode 100644 index 0000000..2b51d62 Binary files /dev/null and b/ch10/propagatingem/ejbtarget/ejbjpa/entities/ShoppingCart.class differ diff --git a/ch10/propagatingem/ejbtarget/ejbjpa/entities/ShoppingCartKey.class b/ch10/propagatingem/ejbtarget/ejbjpa/entities/ShoppingCartKey.class new file mode 100644 index 0000000..c13e03f Binary files /dev/null and b/ch10/propagatingem/ejbtarget/ejbjpa/entities/ShoppingCartKey.class differ diff --git a/ch10/propagatingem/ejbtarget/ejbtestcart.jar b/ch10/propagatingem/ejbtarget/ejbtestcart.jar new file mode 100644 index 0000000..468670f Binary files /dev/null and b/ch10/propagatingem/ejbtarget/ejbtestcart.jar differ diff --git a/ch10/propagatingem/src/ejbjpa/ejb/PropagationTest.java b/ch10/propagatingem/src/ejbjpa/ejb/PropagationTest.java new file mode 100644 index 0000000..975f1a3 --- /dev/null +++ b/ch10/propagatingem/src/ejbjpa/ejb/PropagationTest.java @@ -0,0 +1,6 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +@Remote +public interface PropagationTest { + public void checkShoppingCart(Integer cart_id, String book_id); +} diff --git a/ch10/propagatingem/src/ejbjpa/ejb/PropagationTestBean.java b/ch10/propagatingem/src/ejbjpa/ejb/PropagationTestBean.java new file mode 100644 index 0000000..98895ae --- /dev/null +++ b/ch10/propagatingem/src/ejbjpa/ejb/PropagationTestBean.java @@ -0,0 +1,22 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import ejbjpa.entities.*; +@Stateless +public class PropagationTestBean implements PropagationTest { + @PersistenceContext + private EntityManager em; + public void checkShoppingCart(Integer cart_id, String book_id) { + try { + ShoppingCart testcart = new ShoppingCart(); + testcart = (ShoppingCart) em.find(ShoppingCart.class, new ShoppingCartKey(cart_id, book_id)); + testcart.setUnits(testcart.getUnits()+1); + em.merge(testcart); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/ch10/propagatingem/src/ejbjpa/ejb/ShoppingCartTest.java b/ch10/propagatingem/src/ejbjpa/ejb/ShoppingCartTest.java new file mode 100644 index 0000000..a5f26b0 --- /dev/null +++ b/ch10/propagatingem/src/ejbjpa/ejb/ShoppingCartTest.java @@ -0,0 +1,6 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +@Remote +public interface ShoppingCartTest { + public String[] setShoppingCart(Integer cart_id, String book_id, Integer units, Double unit_price); +} diff --git a/ch10/propagatingem/src/ejbjpa/ejb/ShoppingCartTestBean.java b/ch10/propagatingem/src/ejbjpa/ejb/ShoppingCartTestBean.java new file mode 100644 index 0000000..bfca956 --- /dev/null +++ b/ch10/propagatingem/src/ejbjpa/ejb/ShoppingCartTestBean.java @@ -0,0 +1,39 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.ejb.EJB; +import ejbjpa.entities.*; +@Stateless +public class ShoppingCartTestBean implements ShoppingCartTest { + @PersistenceContext + private EntityManager em; + @EJB private PropagationTest test; + public String[] setShoppingCart(Integer cart_id, String book_id, Integer units, Double unit_price) { + String[] cart_details = new String[8]; + try { + Book book = (Book) em.find(Book.class, book_id); + ShoppingCart cart = new ShoppingCart(); + cart.setCart_id(cart_id); + cart.setBook(book); + cart.setUnits(units); + cart.setUnit_price(unit_price); + em.persist(cart); + cart_details[0] = cart.getCart_id().toString(); + cart_details[1] = cart.getBook().getIsbn(); + cart_details[2] = cart.getUnits().toString(); + cart_details[3] = cart.getUnit_price().toString(); + test.checkShoppingCart(cart_id, book_id); + cart_details[4] = cart.getCart_id().toString(); + cart_details[5] = cart.getBook().getIsbn(); + cart_details[6] = cart.getUnits().toString(); + cart_details[7] = cart.getUnit_price().toString(); + em.remove(cart); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return cart_details; + } +} \ No newline at end of file diff --git a/ch10/propagatingem/src/ejbjpa/entities/Book.java b/ch10/propagatingem/src/ejbjpa/entities/Book.java new file mode 100644 index 0000000..f8b30f7 --- /dev/null +++ b/ch10/propagatingem/src/ejbjpa/entities/Book.java @@ -0,0 +1,67 @@ +package ejbjpa.entities; +import java.util.List; +import javax.persistence.CascadeType; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.OneToMany; +@Entity +@Table(name = "BOOKS") +public class Book implements Serializable { + @Id + @Column(name = "ISBN") + private String isbn; + @Column(name = "TITLE", nullable = false) + private String title; + @Column(name = "AUTHOR", nullable = false) + private String author; + @Column(name = "PRICE", nullable = false) + private Double price; + @Column(name = "QUANTITY", nullable = false) + private Integer quantity; + + @OneToMany(mappedBy="book", cascade = CascadeType.ALL) + private List shoppingCarts; + public List getShoppingCarts(){ + return shoppingCarts; + } + public void setShoppingCarts(List shoppingCarts) { + this.shoppingCarts = shoppingCarts; + } + + public Book() { + } + public String getIsbn() { + return this.isbn; + } + public void setIsbn(String isbn) { + this.isbn = isbn; + } + public String getTitle() { + return this.title; + } + public void setTitle(String title) { + this.title = title; + } + public String getAuthor() { + return this.author; + } + public void setAuthor(String author) { + this.author = author; + } + + public Double getPrice() { + return this.price; + } + public void setPrice(Double price) { + this.price = price; + } + public Integer getQuantity() { + return this.quantity; + } + public void setQuantity(Integer quantity) { + this.quantity = quantity; + } +} diff --git a/ch10/propagatingem/src/ejbjpa/entities/ShoppingCart.java b/ch10/propagatingem/src/ejbjpa/entities/ShoppingCart.java new file mode 100644 index 0000000..a3da937 --- /dev/null +++ b/ch10/propagatingem/src/ejbjpa/entities/ShoppingCart.java @@ -0,0 +1,56 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.IdClass; +import javax.persistence.Table; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; + +@Entity +@Table(name = "SHOPPINGCARTS") +@IdClass(value = ejbjpa.entities.ShoppingCartKey.class) +public class ShoppingCart implements Serializable { + @Id + @Column(name = "CART_ID") + private Integer cart_id; + @Id + @Column(name = "BOOK_ID", insertable=false, updatable=false) + private String book_id; + @Column(name = "UNITS", nullable = false) + private Integer units; + @Column(name = "UNIT_PRICE", nullable = false) + private Double unit_price; + @ManyToOne + @JoinColumn( + name="BOOK_ID", + referencedColumnName="ISBN") + private Book book; + public ShoppingCart() { + } + public Book getBook() { + return this.book; + } + public void setBook(Book book) { + this.book = book; + } + public Integer getCart_id() { + return this.cart_id; + } + public void setCart_id(Integer cart_id) { + this.cart_id = cart_id; + } + public Integer getUnits () { + return this.units; + } + public void setUnits(Integer units) { + this.units = units; + } + public Double getUnit_price() { + return this.unit_price; + } + public void setUnit_price(Double unit_price) { + this.unit_price = unit_price; + } +} diff --git a/ch10/propagatingem/src/ejbjpa/entities/ShoppingCartKey.java b/ch10/propagatingem/src/ejbjpa/entities/ShoppingCartKey.java new file mode 100644 index 0000000..5c29dbe --- /dev/null +++ b/ch10/propagatingem/src/ejbjpa/entities/ShoppingCartKey.java @@ -0,0 +1,37 @@ +package ejbjpa.entities; +import java.io.Serializable; + +public final class ShoppingCartKey implements Serializable { + public Integer cart_id; + public String book_id; + public ShoppingCartKey() {} + public ShoppingCartKey(Integer cart_id, String book_id) { + this.cart_id = cart_id; + this.book_id = book_id; + } + +public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof ShoppingCartKey)) { + return false; + } + ShoppingCartKey other = (ShoppingCartKey) obj; + if (cart_id != null && other.cart_id!= null && this.cart_id.equals(other.cart_id)) { + return (book_id != null && other.book_id!= null && this.book_id.equals(other.book_id)); + } + return false; +} + +public int hashCode() { + if (cart_id!=null && book_id!=null) { + return (cart_id.hashCode() ^ book_id.hashCode()); + } + return 0; +} + +} \ No newline at end of file diff --git a/ch10/propagatingem/src/ejbjpa/servlets/EmEjbTestServlet.java b/ch10/propagatingem/src/ejbjpa/servlets/EmEjbTestServlet.java new file mode 100644 index 0000000..f892219 --- /dev/null +++ b/ch10/propagatingem/src/ejbjpa/servlets/EmEjbTestServlet.java @@ -0,0 +1,33 @@ +package ejbjpa.servlets; +import java.io.*; +import javax.servlet.*; +import javax.servlet.http.*; +import javax.ejb.EJB; +import ejbjpa.entities.*; +import ejbjpa.ejb.*; +public class EmEjbTestServlet extends HttpServlet { + @EJB private ShoppingCartTest cartTest; + public void doGet( + HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + response.setBufferSize(8192); + PrintWriter out = response.getWriter(); + try{ + String[] details = new String[8]; + details = cartTest.setShoppingCart(2,"1430209631", 1, 44.99); + out.println("Cart id : "+ details[0] +"
"); + out.println("Book id : "+ details[1] +"
"); + out.println("Units : "+ details[2] +"
"); + out.println("Unit_price: "+ details[3] +"
"); + out.println("-------------------"+"
"); + out.println("Cart id : "+ details[4] +"
"); + out.println("Book id : "+ details[5] +"
"); + out.println("Units : "+ details[6] +"
"); + out.println("Unit_price: "+ details[7] +"
"); + } + catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/ch10/propagatingem/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class b/ch10/propagatingem/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class new file mode 100644 index 0000000..5397214 Binary files /dev/null and b/ch10/propagatingem/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class differ diff --git a/ch10/propagatingem/target/WEB-INF/lib/ejbtestcart.jar b/ch10/propagatingem/target/WEB-INF/lib/ejbtestcart.jar new file mode 100644 index 0000000..468670f Binary files /dev/null and b/ch10/propagatingem/target/WEB-INF/lib/ejbtestcart.jar differ diff --git a/ch10/propagatingem/target/WEB-INF/sun-web.xml b/ch10/propagatingem/target/WEB-INF/sun-web.xml new file mode 100644 index 0000000..385fa7a --- /dev/null +++ b/ch10/propagatingem/target/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /propagatingtest + diff --git a/ch10/propagatingem/target/WEB-INF/web.xml b/ch10/propagatingem/target/WEB-INF/web.xml new file mode 100644 index 0000000..c5a2358 --- /dev/null +++ b/ch10/propagatingem/target/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + + + EmEjbTestServlet + ejbjpa.servlets.EmEjbTestServlet + + + EmEjbTestServlet + /emejbtestservlet + + + diff --git a/ch10/propagatingem/target/propagatingem.war b/ch10/propagatingem/target/propagatingem.war new file mode 100644 index 0000000..2e905ce Binary files /dev/null and b/ch10/propagatingem/target/propagatingem.war differ diff --git a/ch11/jpqlexample/ejbtarget/META-INF/persistence.xml b/ch11/jpqlexample/ejbtarget/META-INF/persistence.xml new file mode 100644 index 0000000..e966ebb --- /dev/null +++ b/ch11/jpqlexample/ejbtarget/META-INF/persistence.xml @@ -0,0 +1,6 @@ + + + + jdbc/mysqlpool + + diff --git a/ch11/jpqlexample/ejbtarget/ejbjpa/ejb/JpqlTest.class b/ch11/jpqlexample/ejbtarget/ejbjpa/ejb/JpqlTest.class new file mode 100644 index 0000000..dd9d85a Binary files /dev/null and b/ch11/jpqlexample/ejbtarget/ejbjpa/ejb/JpqlTest.class differ diff --git a/ch11/jpqlexample/ejbtarget/ejbjpa/ejb/JpqlTestBean.class b/ch11/jpqlexample/ejbtarget/ejbjpa/ejb/JpqlTestBean.class new file mode 100644 index 0000000..e299cce Binary files /dev/null and b/ch11/jpqlexample/ejbtarget/ejbjpa/ejb/JpqlTestBean.class differ diff --git a/ch11/jpqlexample/ejbtarget/ejbjpa/entities/Customer.class b/ch11/jpqlexample/ejbtarget/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..855839f Binary files /dev/null and b/ch11/jpqlexample/ejbtarget/ejbjpa/entities/Customer.class differ diff --git a/ch11/jpqlexample/ejbtarget/ejbjpa/entities/Employee.class b/ch11/jpqlexample/ejbtarget/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..380c0cf Binary files /dev/null and b/ch11/jpqlexample/ejbtarget/ejbjpa/entities/Employee.class differ diff --git a/ch11/jpqlexample/ejbtarget/ejbjpa/entities/Order.class b/ch11/jpqlexample/ejbtarget/ejbjpa/entities/Order.class new file mode 100644 index 0000000..7c7e67c Binary files /dev/null and b/ch11/jpqlexample/ejbtarget/ejbjpa/entities/Order.class differ diff --git a/ch11/jpqlexample/ejbtarget/ejbjpql.jar b/ch11/jpqlexample/ejbtarget/ejbjpql.jar new file mode 100644 index 0000000..6957d80 Binary files /dev/null and b/ch11/jpqlexample/ejbtarget/ejbjpql.jar differ diff --git a/ch11/jpqlexample/src/ejbjpa/ejb/JpqlTest.java b/ch11/jpqlexample/src/ejbjpa/ejb/JpqlTest.java new file mode 100644 index 0000000..5008d80 --- /dev/null +++ b/ch11/jpqlexample/src/ejbjpa/ejb/JpqlTest.java @@ -0,0 +1,9 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +import java.util.List; +import ejbjpa.entities.*; + +@Remote +public interface JpqlTest { + public List getEmployees(); +} diff --git a/ch11/jpqlexample/src/ejbjpa/ejb/JpqlTestBean.java b/ch11/jpqlexample/src/ejbjpa/ejb/JpqlTestBean.java new file mode 100644 index 0000000..21b717e --- /dev/null +++ b/ch11/jpqlexample/src/ejbjpa/ejb/JpqlTestBean.java @@ -0,0 +1,23 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.List; +import ejbjpa.entities.*; +@Stateless +public class JpqlTestBean implements JpqlTest { + @PersistenceContext + private EntityManager em; + public List getEmployees() { + List employees = null; + try { + employees = (List)em.createQuery("SELECT e FROM Employee e") + .getResultList(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return employees; + } +} \ No newline at end of file diff --git a/ch11/jpqlexample/src/ejbjpa/ejb/_JpqlTestBean.java b/ch11/jpqlexample/src/ejbjpa/ejb/_JpqlTestBean.java new file mode 100644 index 0000000..beebfc1 --- /dev/null +++ b/ch11/jpqlexample/src/ejbjpa/ejb/_JpqlTestBean.java @@ -0,0 +1,24 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.List; +import javax.persistence.Query; +import ejbjpa.entities.*; +@Stateless +public class JpqlTestBean implements JpqlTest { + @PersistenceContext + private EntityManager em; + public List getEmployees() { + List employees = null; + try { + Query query = em.createQuery("SELECT e FROM Employee e"); + employees = (List)query.getResultList(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return employees; + } +} \ No newline at end of file diff --git a/ch11/jpqlexample/src/ejbjpa/entities/Customer.java b/ch11/jpqlexample/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..aa187c0 --- /dev/null +++ b/ch11/jpqlexample/src/ejbjpa/entities/Customer.java @@ -0,0 +1,64 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "CUST_NAME", nullable = false) + private String cust_name; + @Column(name = "LOC_ID") + private Integer loc_id; + @Column(name = "EMAIL", nullable = false) + private String email; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToMany(mappedBy="customer", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCust_name() { + return this.cust_name; + } + public void setCust_name(String cust_name) { + this.cust_name = cust_name; + } + public Integer getLoc_id() { + return this.loc_id; + } + public void setLoc_id(Integer loc_id) { + this.loc_id = loc_id; + } + public String getEmail() { + return this.email; + } + public void setEmail(String email) { + this.email = email; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/ch11/jpqlexample/src/ejbjpa/entities/Employee.java b/ch11/jpqlexample/src/ejbjpa/entities/Employee.java new file mode 100644 index 0000000..9751c22 --- /dev/null +++ b/ch11/jpqlexample/src/ejbjpa/entities/Employee.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "EMPLOYEES") +public class Employee implements Serializable { + @Id + @Column(name = "EMPNO") + private Integer empno; + @Column(name = "firstname", nullable = false) + private String firstname; + @Column(name = "lastname", nullable = false) + private String lastname; + @OneToMany(mappedBy="employee", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Employee() { + } + public Integer getEmpno() { + return this.empno; + } + public void setEmpno(Integer empno) { + this.empno = empno; + } + public String getFirstname() { + return this.firstname; + } + public void setFirstname(String firstname) { + this.firstname = firstname; + } + public String getLastname() { + return this.lastname; + } + public void setLastname(String lastname) { + this.lastname = lastname; + } +} diff --git a/ch11/jpqlexample/src/ejbjpa/entities/Order.java b/ch11/jpqlexample/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..b9aa6e0 --- /dev/null +++ b/ch11/jpqlexample/src/ejbjpa/entities/Order.java @@ -0,0 +1,76 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import static javax.persistence.TemporalType.DATE; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.TableGenerator; +import java.util.Date; +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + /* @TableGenerator(name = "ponoGen", + table = "PONO_GEN_TABLE", + pkColumnName = "GEN_NAME", + valueColumnName = "GEN_VALUE", + pkColumnValue = "PONO_GEN", + allocationSize = 1) */ + @Id + // @GeneratedValue(strategy=GenerationType.TABLE, generator = "ponoGen") + @GeneratedValue(strategy=GenerationType.IDENTITY) + @Column(name = "PONO") + private Integer pono; + @Column(name = "SHIPPING_DATE", nullable = false) + @Temporal(DATE) + private Date shipping_date; + @Column(name = "DELIVERY_ESTIMATE", nullable = false) + private String delivery_estimate; + @ManyToOne + @JoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ID") + private Customer customer; + @ManyToOne + @JoinColumn( + name="EMPNO", + referencedColumnName="EMPNO") + private Employee employee; + public Order() { + } + public Customer getCustomer() { + return this.customer; + } + public void setCustomer(Customer customer) { + this.customer = customer; + } + public Employee getEmployee() { + return this.employee; + } + public void setEmployee (Employee employee) { + this.employee = employee; + } + public Integer getPono () { + return this.pono; + } + public void setPono (Integer pono) { + this.pono = pono; + } + public Date getShipping_date () { + return this.shipping_date; + } + public void setShipping_date (Date shipping_date) { + this.shipping_date = shipping_date; + } + public String getDelivery_estimate () { + return this.delivery_estimate; + } + public void setDelivery_estimate (String delivery_estimate) { + this.delivery_estimate = delivery_estimate; + } +} diff --git a/ch11/jpqlexample/src/ejbjpa/servlets/JpqlTestServlet.java b/ch11/jpqlexample/src/ejbjpa/servlets/JpqlTestServlet.java new file mode 100644 index 0000000..206f40d --- /dev/null +++ b/ch11/jpqlexample/src/ejbjpa/servlets/JpqlTestServlet.java @@ -0,0 +1,34 @@ +package ejbjpa.servlets; +import java.io.*; +import javax.servlet.*; +import javax.servlet.http.*; +import javax.ejb.EJB; +import java.util.List; +import java.util.Iterator; +import ejbjpa.entities.*; +import ejbjpa.ejb.*; +public class JpqlTestServlet extends HttpServlet { + @EJB private JpqlTest jpqlTest; + public void doGet( + HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + response.setBufferSize(8192); + PrintWriter out = response.getWriter(); + try{ + List employees = jpqlTest.getEmployees(); + Iterator i = employees.iterator(); + Employee employee; + while (i.hasNext()) { + employee = (Employee) i.next(); + out.println("Employee id: "+ employee.getEmpno() +"
"); + out.println("First name: "+ employee.getFirstname() +"
"); + out.println("Last name: "+ employee.getLastname() +"
"); + out.println("----------"+ "
"); + } + } + catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/ch11/jpqlexample/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class b/ch11/jpqlexample/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class new file mode 100644 index 0000000..99066dc Binary files /dev/null and b/ch11/jpqlexample/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class differ diff --git a/ch11/jpqlexample/target/WEB-INF/classes/ejbjpa/servlets/JpqlTestServlet.class b/ch11/jpqlexample/target/WEB-INF/classes/ejbjpa/servlets/JpqlTestServlet.class new file mode 100644 index 0000000..df526a2 Binary files /dev/null and b/ch11/jpqlexample/target/WEB-INF/classes/ejbjpa/servlets/JpqlTestServlet.class differ diff --git a/ch11/jpqlexample/target/WEB-INF/lib/ejbjpql.jar b/ch11/jpqlexample/target/WEB-INF/lib/ejbjpql.jar new file mode 100644 index 0000000..6957d80 Binary files /dev/null and b/ch11/jpqlexample/target/WEB-INF/lib/ejbjpql.jar differ diff --git a/ch11/jpqlexample/target/WEB-INF/sun-web.xml b/ch11/jpqlexample/target/WEB-INF/sun-web.xml new file mode 100644 index 0000000..079ba76 --- /dev/null +++ b/ch11/jpqlexample/target/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /jpqltest + diff --git a/ch11/jpqlexample/target/WEB-INF/web.xml b/ch11/jpqlexample/target/WEB-INF/web.xml new file mode 100644 index 0000000..ea7183c --- /dev/null +++ b/ch11/jpqlexample/target/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + + + JpqlTestServlet + ejbjpa.servlets.JpqlTestServlet + + + JpqlTestServlet + /jpqltestservlet + + + diff --git a/ch11/jpqlexample/target/jpqlexample.war b/ch11/jpqlexample/target/jpqlexample.war new file mode 100644 index 0000000..3d6cb24 Binary files /dev/null and b/ch11/jpqlexample/target/jpqlexample.war differ diff --git a/ch11/jpqljoins/ejbtarget/META-INF/persistence.xml b/ch11/jpqljoins/ejbtarget/META-INF/persistence.xml new file mode 100644 index 0000000..206e25f --- /dev/null +++ b/ch11/jpqljoins/ejbtarget/META-INF/persistence.xml @@ -0,0 +1,12 @@ + + + + jdbc/mysqlpool + + + jdbc/mysqlpool + + + jdbc/mysqlpool + + diff --git a/ch11/jpqljoins/ejbtarget/ejbjpa/ejb/JpqlJoinsTest.class b/ch11/jpqljoins/ejbtarget/ejbjpa/ejb/JpqlJoinsTest.class new file mode 100644 index 0000000..9608886 Binary files /dev/null and b/ch11/jpqljoins/ejbtarget/ejbjpa/ejb/JpqlJoinsTest.class differ diff --git a/ch11/jpqljoins/ejbtarget/ejbjpa/ejb/JpqlJoinsTestBean.class b/ch11/jpqljoins/ejbtarget/ejbjpa/ejb/JpqlJoinsTestBean.class new file mode 100644 index 0000000..45f52a5 Binary files /dev/null and b/ch11/jpqljoins/ejbtarget/ejbjpa/ejb/JpqlJoinsTestBean.class differ diff --git a/ch11/jpqljoins/ejbtarget/ejbjpa/entities/Customer.class b/ch11/jpqljoins/ejbtarget/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..855839f Binary files /dev/null and b/ch11/jpqljoins/ejbtarget/ejbjpa/entities/Customer.class differ diff --git a/ch11/jpqljoins/ejbtarget/ejbjpa/entities/Employee.class b/ch11/jpqljoins/ejbtarget/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..380c0cf Binary files /dev/null and b/ch11/jpqljoins/ejbtarget/ejbjpa/entities/Employee.class differ diff --git a/ch11/jpqljoins/ejbtarget/ejbjpa/entities/Order.class b/ch11/jpqljoins/ejbtarget/ejbjpa/entities/Order.class new file mode 100644 index 0000000..7c7e67c Binary files /dev/null and b/ch11/jpqljoins/ejbtarget/ejbjpa/entities/Order.class differ diff --git a/ch11/jpqljoins/ejbtarget/ejbjpqljoins.jar b/ch11/jpqljoins/ejbtarget/ejbjpqljoins.jar new file mode 100644 index 0000000..36b21bc Binary files /dev/null and b/ch11/jpqljoins/ejbtarget/ejbjpqljoins.jar differ diff --git a/ch11/jpqljoins/src/ejbjpa/ejb/JpqlJoinsTest.java b/ch11/jpqljoins/src/ejbjpa/ejb/JpqlJoinsTest.java new file mode 100644 index 0000000..58f5b80 --- /dev/null +++ b/ch11/jpqljoins/src/ejbjpa/ejb/JpqlJoinsTest.java @@ -0,0 +1,8 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; + +@Remote +public interface JpqlJoinsTest { + public Integer[] countOrders(Integer empno); + +} diff --git a/ch11/jpqljoins/src/ejbjpa/ejb/JpqlJoinsTestBean.java b/ch11/jpqljoins/src/ejbjpa/ejb/JpqlJoinsTestBean.java new file mode 100644 index 0000000..874403e --- /dev/null +++ b/ch11/jpqljoins/src/ejbjpa/ejb/JpqlJoinsTestBean.java @@ -0,0 +1,64 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; +import java.util.List; +import ejbjpa.entities.*; +@Stateless +public class JpqlJoinsTestBean implements JpqlJoinsTest { + @PersistenceUnit(unitName="jpqljoins0-pu") + private EntityManagerFactory emf0; + @PersistenceUnit(unitName="jpqljoins1-pu") + private EntityManagerFactory emf1; + @PersistenceUnit(unitName="jpqljoins2-pu") + private EntityManagerFactory emf2; + public Integer[] countOrders(Integer empno){ + List orders0 = null; + List orders1 = null; + List orders2 = null; + EntityManager em0 = emf0.createEntityManager(); + EntityManager em1 = emf1.createEntityManager(); + EntityManager em2 = emf2.createEntityManager(); + Integer[] numOfOrders= new Integer[3]; + try { + //perform queries + Employee employee0 = (Employee)em0.createQuery("SELECT e FROM Employee e WHERE e.empno=:empno") + .setHint("toplink.refresh", "true") + .setParameter("empno", empno) + .getSingleResult(); + Employee employee1 = (Employee)em1.createQuery("SELECT DISTINCT e FROM Employee e LEFT JOIN FETCH e.orders WHERE e.empno=:empno") + .setHint("toplink.refresh", "true") + .setParameter("empno", empno) + .getSingleResult(); + + Employee employee2 = (Employee)em2.createQuery("SELECT e FROM Employee e WHERE e.empno=:empno") + .setHint("toplink.refresh", "true") + .setParameter("empno", empno) + .getSingleResult(); + //count the number of orders for the employee retrieved with the first query + orders0 = (List)employee0.getOrders(); + numOfOrders[0] = orders0.size(); + //increase the number of orders for the employee by 1 and commit the change + Order order = new Order(); + order.setEmployee(employee0); + em0.getTransaction().begin(); + em0.persist(order); + em0.flush(); + em0.getTransaction().commit(); + //count the number of orders for the employee retrieved with the second and third queries + orders1 = (List)employee1.getOrders(); + numOfOrders[1] = orders1.size(); + orders2 = (List)employee2.getOrders(); + numOfOrders[2] = orders2.size(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + em0.close(); + em1.close(); + em2.close(); + return numOfOrders; + } +} \ No newline at end of file diff --git a/ch11/jpqljoins/src/ejbjpa/entities/Customer.java b/ch11/jpqljoins/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..aa187c0 --- /dev/null +++ b/ch11/jpqljoins/src/ejbjpa/entities/Customer.java @@ -0,0 +1,64 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "CUST_NAME", nullable = false) + private String cust_name; + @Column(name = "LOC_ID") + private Integer loc_id; + @Column(name = "EMAIL", nullable = false) + private String email; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToMany(mappedBy="customer", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCust_name() { + return this.cust_name; + } + public void setCust_name(String cust_name) { + this.cust_name = cust_name; + } + public Integer getLoc_id() { + return this.loc_id; + } + public void setLoc_id(Integer loc_id) { + this.loc_id = loc_id; + } + public String getEmail() { + return this.email; + } + public void setEmail(String email) { + this.email = email; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/ch11/jpqljoins/src/ejbjpa/entities/Employee.java b/ch11/jpqljoins/src/ejbjpa/entities/Employee.java new file mode 100644 index 0000000..9751c22 --- /dev/null +++ b/ch11/jpqljoins/src/ejbjpa/entities/Employee.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "EMPLOYEES") +public class Employee implements Serializable { + @Id + @Column(name = "EMPNO") + private Integer empno; + @Column(name = "firstname", nullable = false) + private String firstname; + @Column(name = "lastname", nullable = false) + private String lastname; + @OneToMany(mappedBy="employee", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Employee() { + } + public Integer getEmpno() { + return this.empno; + } + public void setEmpno(Integer empno) { + this.empno = empno; + } + public String getFirstname() { + return this.firstname; + } + public void setFirstname(String firstname) { + this.firstname = firstname; + } + public String getLastname() { + return this.lastname; + } + public void setLastname(String lastname) { + this.lastname = lastname; + } +} diff --git a/ch11/jpqljoins/src/ejbjpa/entities/Order.java b/ch11/jpqljoins/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..b9aa6e0 --- /dev/null +++ b/ch11/jpqljoins/src/ejbjpa/entities/Order.java @@ -0,0 +1,76 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import static javax.persistence.TemporalType.DATE; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.TableGenerator; +import java.util.Date; +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + /* @TableGenerator(name = "ponoGen", + table = "PONO_GEN_TABLE", + pkColumnName = "GEN_NAME", + valueColumnName = "GEN_VALUE", + pkColumnValue = "PONO_GEN", + allocationSize = 1) */ + @Id + // @GeneratedValue(strategy=GenerationType.TABLE, generator = "ponoGen") + @GeneratedValue(strategy=GenerationType.IDENTITY) + @Column(name = "PONO") + private Integer pono; + @Column(name = "SHIPPING_DATE", nullable = false) + @Temporal(DATE) + private Date shipping_date; + @Column(name = "DELIVERY_ESTIMATE", nullable = false) + private String delivery_estimate; + @ManyToOne + @JoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ID") + private Customer customer; + @ManyToOne + @JoinColumn( + name="EMPNO", + referencedColumnName="EMPNO") + private Employee employee; + public Order() { + } + public Customer getCustomer() { + return this.customer; + } + public void setCustomer(Customer customer) { + this.customer = customer; + } + public Employee getEmployee() { + return this.employee; + } + public void setEmployee (Employee employee) { + this.employee = employee; + } + public Integer getPono () { + return this.pono; + } + public void setPono (Integer pono) { + this.pono = pono; + } + public Date getShipping_date () { + return this.shipping_date; + } + public void setShipping_date (Date shipping_date) { + this.shipping_date = shipping_date; + } + public String getDelivery_estimate () { + return this.delivery_estimate; + } + public void setDelivery_estimate (String delivery_estimate) { + this.delivery_estimate = delivery_estimate; + } +} diff --git a/ch11/jpqljoins/src/ejbjpa/servlets/JpqlJoinsTestServlet.java b/ch11/jpqljoins/src/ejbjpa/servlets/JpqlJoinsTestServlet.java new file mode 100644 index 0000000..9608bcc --- /dev/null +++ b/ch11/jpqljoins/src/ejbjpa/servlets/JpqlJoinsTestServlet.java @@ -0,0 +1,27 @@ +package ejbjpa.servlets; +import java.io.*; +import javax.servlet.*; +import javax.servlet.http.*; +import javax.ejb.EJB; +import ejbjpa.entities.*; +import ejbjpa.ejb.*; +public class JpqlJoinsTestServlet extends HttpServlet { + @EJB private JpqlJoinsTest jpqlJoinsTest; + public void doGet( + HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + response.setBufferSize(8192); + PrintWriter out = response.getWriter(); + Integer[] numOfOrders= new Integer[3]; + numOfOrders=jpqlJoinsTest.countOrders(1); + try{ + out.println("Number of orders associated with employee 1: "+numOfOrders[0] +"
"); + out.println("Number of orders returned by the JOIN query after increasing: "+numOfOrders[1] +"
"); + out.println("Number of orders you actually got after increasing: "+numOfOrders[2] +"
"); + } + catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/ch11/jpqljoins/target/WEB-INF/classes/ejbjpa/servlets/JpqlJoinsTestServlet.class b/ch11/jpqljoins/target/WEB-INF/classes/ejbjpa/servlets/JpqlJoinsTestServlet.class new file mode 100644 index 0000000..65f85d1 Binary files /dev/null and b/ch11/jpqljoins/target/WEB-INF/classes/ejbjpa/servlets/JpqlJoinsTestServlet.class differ diff --git a/ch11/jpqljoins/target/WEB-INF/lib/ejbjpqljoins.jar b/ch11/jpqljoins/target/WEB-INF/lib/ejbjpqljoins.jar new file mode 100644 index 0000000..8d0d8c3 Binary files /dev/null and b/ch11/jpqljoins/target/WEB-INF/lib/ejbjpqljoins.jar differ diff --git a/ch11/jpqljoins/target/WEB-INF/sun-web.xml b/ch11/jpqljoins/target/WEB-INF/sun-web.xml new file mode 100644 index 0000000..72c8478 --- /dev/null +++ b/ch11/jpqljoins/target/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /jpqljoins + diff --git a/ch11/jpqljoins/target/WEB-INF/web.xml b/ch11/jpqljoins/target/WEB-INF/web.xml new file mode 100644 index 0000000..cdb9b08 --- /dev/null +++ b/ch11/jpqljoins/target/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + + + JpqlJoinsTestServlet + ejbjpa.servlets.JpqlJoinsTestServlet + + + JpqlJoinsTestServlet + /jpqljoinstestservlet + + + diff --git a/ch11/jpqljoins/target/jpqljoins.war b/ch11/jpqljoins/target/jpqljoins.war new file mode 100644 index 0000000..8dc625b Binary files /dev/null and b/ch11/jpqljoins/target/jpqljoins.war differ diff --git a/ch11/jpqlrelationships/ejbtarget/META-INF/persistence.xml b/ch11/jpqlrelationships/ejbtarget/META-INF/persistence.xml new file mode 100644 index 0000000..d9fb9e6 --- /dev/null +++ b/ch11/jpqlrelationships/ejbtarget/META-INF/persistence.xml @@ -0,0 +1,6 @@ + + + + jdbc/mysqlpool + + diff --git a/ch11/jpqlrelationships/ejbtarget/ejbjpa/ejb/JpqlTest.class b/ch11/jpqlrelationships/ejbtarget/ejbjpa/ejb/JpqlTest.class new file mode 100644 index 0000000..fef0797 Binary files /dev/null and b/ch11/jpqlrelationships/ejbtarget/ejbjpa/ejb/JpqlTest.class differ diff --git a/ch11/jpqlrelationships/ejbtarget/ejbjpa/ejb/JpqlTestBean.class b/ch11/jpqlrelationships/ejbtarget/ejbjpa/ejb/JpqlTestBean.class new file mode 100644 index 0000000..044f7e9 Binary files /dev/null and b/ch11/jpqlrelationships/ejbtarget/ejbjpa/ejb/JpqlTestBean.class differ diff --git a/ch11/jpqlrelationships/ejbtarget/ejbjpa/entities/Customer.class b/ch11/jpqlrelationships/ejbtarget/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..855839f Binary files /dev/null and b/ch11/jpqlrelationships/ejbtarget/ejbjpa/entities/Customer.class differ diff --git a/ch11/jpqlrelationships/ejbtarget/ejbjpa/entities/Employee.class b/ch11/jpqlrelationships/ejbtarget/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..380c0cf Binary files /dev/null and b/ch11/jpqlrelationships/ejbtarget/ejbjpa/entities/Employee.class differ diff --git a/ch11/jpqlrelationships/ejbtarget/ejbjpa/entities/Order.class b/ch11/jpqlrelationships/ejbtarget/ejbjpa/entities/Order.class new file mode 100644 index 0000000..7c7e67c Binary files /dev/null and b/ch11/jpqlrelationships/ejbtarget/ejbjpa/entities/Order.class differ diff --git a/ch11/jpqlrelationships/ejbtarget/ejbjpql.jar b/ch11/jpqlrelationships/ejbtarget/ejbjpql.jar new file mode 100644 index 0000000..8454c59 Binary files /dev/null and b/ch11/jpqlrelationships/ejbtarget/ejbjpql.jar differ diff --git a/ch11/jpqlrelationships/src/ejbjpa/ejb/JpqlTest.java b/ch11/jpqlrelationships/src/ejbjpa/ejb/JpqlTest.java new file mode 100644 index 0000000..6886aed --- /dev/null +++ b/ch11/jpqlrelationships/src/ejbjpa/ejb/JpqlTest.java @@ -0,0 +1,11 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +import java.util.List; +import ejbjpa.entities.*; + +@Remote +public interface JpqlTest { + public List getEmployees(); + public boolean checkIfManaged(); + +} diff --git a/ch11/jpqlrelationships/src/ejbjpa/ejb/JpqlTestBean.java b/ch11/jpqlrelationships/src/ejbjpa/ejb/JpqlTestBean.java new file mode 100644 index 0000000..97c7676 --- /dev/null +++ b/ch11/jpqlrelationships/src/ejbjpa/ejb/JpqlTestBean.java @@ -0,0 +1,62 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.List; +import java.util.Iterator; +import ejbjpa.entities.*; +@Stateless +public class JpqlTestBean implements JpqlTest { + @PersistenceContext + private EntityManager em; + public List getEmployees() { + List employees = null; + try { + employees = (List)em.createQuery("SELECT e FROM Employee e") + .getResultList(); + Employee employee; + Iterator i = employees.iterator(); + while (i.hasNext()) { + employee = (Employee) i.next(); + if (!(em.contains(employee))) { + throw new EJBException("PC does not contain this employee object"); + } + } + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return employees; + } + public boolean checkIfManaged(){ + List employees = null; + List orders = null; + try { + employees = (List)em.createQuery("SELECT e FROM Employee e") + .getResultList(); + Employee employee; + Order order; + Iterator i = employees.iterator(); + Iterator j; + while (i.hasNext()) { + employee = (Employee) i.next(); + if (!em.contains(employee)) { + return false; + } + orders = (List)employee.getOrders(); + j= orders.iterator(); + while (j.hasNext()) { + order = (Order) j.next(); + em.remove(order); + if (!em.contains(order)) { + return false; + } + } + } + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return true; + } +} \ No newline at end of file diff --git a/ch11/jpqlrelationships/src/ejbjpa/entities/Customer.java b/ch11/jpqlrelationships/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..aa187c0 --- /dev/null +++ b/ch11/jpqlrelationships/src/ejbjpa/entities/Customer.java @@ -0,0 +1,64 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "CUST_NAME", nullable = false) + private String cust_name; + @Column(name = "LOC_ID") + private Integer loc_id; + @Column(name = "EMAIL", nullable = false) + private String email; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToMany(mappedBy="customer", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCust_name() { + return this.cust_name; + } + public void setCust_name(String cust_name) { + this.cust_name = cust_name; + } + public Integer getLoc_id() { + return this.loc_id; + } + public void setLoc_id(Integer loc_id) { + this.loc_id = loc_id; + } + public String getEmail() { + return this.email; + } + public void setEmail(String email) { + this.email = email; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/ch11/jpqlrelationships/src/ejbjpa/entities/Employee.java b/ch11/jpqlrelationships/src/ejbjpa/entities/Employee.java new file mode 100644 index 0000000..9751c22 --- /dev/null +++ b/ch11/jpqlrelationships/src/ejbjpa/entities/Employee.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "EMPLOYEES") +public class Employee implements Serializable { + @Id + @Column(name = "EMPNO") + private Integer empno; + @Column(name = "firstname", nullable = false) + private String firstname; + @Column(name = "lastname", nullable = false) + private String lastname; + @OneToMany(mappedBy="employee", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Employee() { + } + public Integer getEmpno() { + return this.empno; + } + public void setEmpno(Integer empno) { + this.empno = empno; + } + public String getFirstname() { + return this.firstname; + } + public void setFirstname(String firstname) { + this.firstname = firstname; + } + public String getLastname() { + return this.lastname; + } + public void setLastname(String lastname) { + this.lastname = lastname; + } +} diff --git a/ch11/jpqlrelationships/src/ejbjpa/entities/Order.java b/ch11/jpqlrelationships/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..b9aa6e0 --- /dev/null +++ b/ch11/jpqlrelationships/src/ejbjpa/entities/Order.java @@ -0,0 +1,76 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import static javax.persistence.TemporalType.DATE; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.TableGenerator; +import java.util.Date; +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + /* @TableGenerator(name = "ponoGen", + table = "PONO_GEN_TABLE", + pkColumnName = "GEN_NAME", + valueColumnName = "GEN_VALUE", + pkColumnValue = "PONO_GEN", + allocationSize = 1) */ + @Id + // @GeneratedValue(strategy=GenerationType.TABLE, generator = "ponoGen") + @GeneratedValue(strategy=GenerationType.IDENTITY) + @Column(name = "PONO") + private Integer pono; + @Column(name = "SHIPPING_DATE", nullable = false) + @Temporal(DATE) + private Date shipping_date; + @Column(name = "DELIVERY_ESTIMATE", nullable = false) + private String delivery_estimate; + @ManyToOne + @JoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ID") + private Customer customer; + @ManyToOne + @JoinColumn( + name="EMPNO", + referencedColumnName="EMPNO") + private Employee employee; + public Order() { + } + public Customer getCustomer() { + return this.customer; + } + public void setCustomer(Customer customer) { + this.customer = customer; + } + public Employee getEmployee() { + return this.employee; + } + public void setEmployee (Employee employee) { + this.employee = employee; + } + public Integer getPono () { + return this.pono; + } + public void setPono (Integer pono) { + this.pono = pono; + } + public Date getShipping_date () { + return this.shipping_date; + } + public void setShipping_date (Date shipping_date) { + this.shipping_date = shipping_date; + } + public String getDelivery_estimate () { + return this.delivery_estimate; + } + public void setDelivery_estimate (String delivery_estimate) { + this.delivery_estimate = delivery_estimate; + } +} diff --git a/ch11/jpqlrelationships/src/ejbjpa/servlets/JpqlTestServlet.java b/ch11/jpqlrelationships/src/ejbjpa/servlets/JpqlTestServlet.java new file mode 100644 index 0000000..46a1682 --- /dev/null +++ b/ch11/jpqlrelationships/src/ejbjpa/servlets/JpqlTestServlet.java @@ -0,0 +1,44 @@ +package ejbjpa.servlets; +import java.io.*; +import javax.servlet.*; +import javax.servlet.http.*; +import javax.ejb.EJB; +import java.util.List; +import java.util.Iterator; +import ejbjpa.entities.*; +import ejbjpa.ejb.*; +import javax.annotation.Resource; +import javax.transaction.UserTransaction; + +public class JpqlTestServlet extends HttpServlet { + @EJB private JpqlTest jpqlTest; + @Resource + UserTransaction utx; + + public void doGet( + HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + response.setBufferSize(8192); + PrintWriter out = response.getWriter(); + try{ + List employees = jpqlTest.getEmployees(); + Iterator i = employees.iterator(); + Employee employee; + while (i.hasNext()) { + employee = (Employee) i.next(); + out.println("Employee id: "+ employee.getEmpno() +"
"); + out.println("First name: "+ employee.getFirstname() +"
"); + out.println("Last name: "+ employee.getLastname() +"
"); + out.println("----------"+ "
"); + } + utx.begin(); + out.println("All employee entities were managed during the checkIfManaged call: "+ jpqlTest.checkIfManaged() +"
"); + utx.rollback(); + + } + catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/ch11/jpqlrelationships/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class b/ch11/jpqlrelationships/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class new file mode 100644 index 0000000..99066dc Binary files /dev/null and b/ch11/jpqlrelationships/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class differ diff --git a/ch11/jpqlrelationships/target/WEB-INF/classes/ejbjpa/servlets/JpqlTestServlet.class b/ch11/jpqlrelationships/target/WEB-INF/classes/ejbjpa/servlets/JpqlTestServlet.class new file mode 100644 index 0000000..02c1276 Binary files /dev/null and b/ch11/jpqlrelationships/target/WEB-INF/classes/ejbjpa/servlets/JpqlTestServlet.class differ diff --git a/ch11/jpqlrelationships/target/WEB-INF/lib/ejbjpql.jar b/ch11/jpqlrelationships/target/WEB-INF/lib/ejbjpql.jar new file mode 100644 index 0000000..8454c59 Binary files /dev/null and b/ch11/jpqlrelationships/target/WEB-INF/lib/ejbjpql.jar differ diff --git a/ch11/jpqlrelationships/target/WEB-INF/sun-web.xml b/ch11/jpqlrelationships/target/WEB-INF/sun-web.xml new file mode 100644 index 0000000..b92dea3 --- /dev/null +++ b/ch11/jpqlrelationships/target/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /jpqlrelationship + diff --git a/ch11/jpqlrelationships/target/WEB-INF/web.xml b/ch11/jpqlrelationships/target/WEB-INF/web.xml new file mode 100644 index 0000000..ea7183c --- /dev/null +++ b/ch11/jpqlrelationships/target/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + + + JpqlTestServlet + ejbjpa.servlets.JpqlTestServlet + + + JpqlTestServlet + /jpqltestservlet + + + diff --git a/ch11/jpqlrelationships/target/jpqlrelationship.war b/ch11/jpqlrelationships/target/jpqlrelationship.war new file mode 100644 index 0000000..f9d1400 Binary files /dev/null and b/ch11/jpqlrelationships/target/jpqlrelationship.war differ diff --git a/ch11/nativequery/ejbtarget/META-INF/persistence.xml b/ch11/nativequery/ejbtarget/META-INF/persistence.xml new file mode 100644 index 0000000..705c882 --- /dev/null +++ b/ch11/nativequery/ejbtarget/META-INF/persistence.xml @@ -0,0 +1,6 @@ + + + + jdbc/mysqlpool + + diff --git a/ch11/nativequery/ejbtarget/ejbjpa/ejb/NativeQueryTest.class b/ch11/nativequery/ejbtarget/ejbjpa/ejb/NativeQueryTest.class new file mode 100644 index 0000000..c949f17 Binary files /dev/null and b/ch11/nativequery/ejbtarget/ejbjpa/ejb/NativeQueryTest.class differ diff --git a/ch11/nativequery/ejbtarget/ejbjpa/ejb/NativeQueryTestBean.class b/ch11/nativequery/ejbtarget/ejbjpa/ejb/NativeQueryTestBean.class new file mode 100644 index 0000000..cb2afe4 Binary files /dev/null and b/ch11/nativequery/ejbtarget/ejbjpa/ejb/NativeQueryTestBean.class differ diff --git a/ch11/nativequery/ejbtarget/ejbjpa/entities/Customer.class b/ch11/nativequery/ejbtarget/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..855839f Binary files /dev/null and b/ch11/nativequery/ejbtarget/ejbjpa/entities/Customer.class differ diff --git a/ch11/nativequery/ejbtarget/ejbjpa/entities/Employee.class b/ch11/nativequery/ejbtarget/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..0da76fa Binary files /dev/null and b/ch11/nativequery/ejbtarget/ejbjpa/entities/Employee.class differ diff --git a/ch11/nativequery/ejbtarget/ejbjpa/entities/Order.class b/ch11/nativequery/ejbtarget/ejbjpa/entities/Order.class new file mode 100644 index 0000000..7c7e67c Binary files /dev/null and b/ch11/nativequery/ejbtarget/ejbjpa/entities/Order.class differ diff --git a/ch11/nativequery/ejbtarget/ejbsql.jar b/ch11/nativequery/ejbtarget/ejbsql.jar new file mode 100644 index 0000000..1b50aa3 Binary files /dev/null and b/ch11/nativequery/ejbtarget/ejbsql.jar differ diff --git a/ch11/nativequery/ejbtarget/nativequery.war b/ch11/nativequery/ejbtarget/nativequery.war new file mode 100644 index 0000000..6e6007d Binary files /dev/null and b/ch11/nativequery/ejbtarget/nativequery.war differ diff --git a/ch11/nativequery/src/ejbjpa/ejb/NativeQueryTest.java b/ch11/nativequery/src/ejbjpa/ejb/NativeQueryTest.java new file mode 100644 index 0000000..e121783 --- /dev/null +++ b/ch11/nativequery/src/ejbjpa/ejb/NativeQueryTest.java @@ -0,0 +1,9 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +import java.util.List; +import ejbjpa.entities.*; + +@Remote +public interface NativeQueryTest { + public List getEmployees(); +} diff --git a/ch11/nativequery/src/ejbjpa/ejb/NativeQueryTestBean.java b/ch11/nativequery/src/ejbjpa/ejb/NativeQueryTestBean.java new file mode 100644 index 0000000..946a8c3 --- /dev/null +++ b/ch11/nativequery/src/ejbjpa/ejb/NativeQueryTestBean.java @@ -0,0 +1,23 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.List; +import ejbjpa.entities.*; +@Stateless +public class NativeQueryTestBean implements NativeQueryTest { + @PersistenceContext + private EntityManager em; + public List getEmployees() { + List employees = null; + try { + employees = (List)em.createNativeQuery("SELECT * FROM employees", ejbjpa.entities.Employee.class) + .getResultList(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return employees; + } +} \ No newline at end of file diff --git a/ch11/nativequery/src/ejbjpa/entities/Customer.java b/ch11/nativequery/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..aa187c0 --- /dev/null +++ b/ch11/nativequery/src/ejbjpa/entities/Customer.java @@ -0,0 +1,64 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "CUST_NAME", nullable = false) + private String cust_name; + @Column(name = "LOC_ID") + private Integer loc_id; + @Column(name = "EMAIL", nullable = false) + private String email; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToMany(mappedBy="customer", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCust_name() { + return this.cust_name; + } + public void setCust_name(String cust_name) { + this.cust_name = cust_name; + } + public Integer getLoc_id() { + return this.loc_id; + } + public void setLoc_id(Integer loc_id) { + this.loc_id = loc_id; + } + public String getEmail() { + return this.email; + } + public void setEmail(String email) { + this.email = email; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/ch11/nativequery/src/ejbjpa/entities/Employee.java b/ch11/nativequery/src/ejbjpa/entities/Employee.java new file mode 100644 index 0000000..77f107e --- /dev/null +++ b/ch11/nativequery/src/ejbjpa/entities/Employee.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "EMPLOYEES") +public class Employee implements Serializable { + @Id + @Column(name = "empno") + private Integer empno; + @Column(name = "firstname", nullable = false) + private String firstname; + @Column(name = "lastname", nullable = false) + private String lastname; + @OneToMany(mappedBy="employee", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Employee() { + } + public Integer getEmpno() { + return this.empno; + } + public void setEmpno(Integer empno) { + this.empno = empno; + } + public String getFirstname() { + return this.firstname; + } + public void setFirstname(String firstname) { + this.firstname = firstname; + } + public String getLastname() { + return this.lastname; + } + public void setLastname(String lastname) { + this.lastname = lastname; + } +} diff --git a/ch11/nativequery/src/ejbjpa/entities/Order.java b/ch11/nativequery/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..b9aa6e0 --- /dev/null +++ b/ch11/nativequery/src/ejbjpa/entities/Order.java @@ -0,0 +1,76 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import static javax.persistence.TemporalType.DATE; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.TableGenerator; +import java.util.Date; +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + /* @TableGenerator(name = "ponoGen", + table = "PONO_GEN_TABLE", + pkColumnName = "GEN_NAME", + valueColumnName = "GEN_VALUE", + pkColumnValue = "PONO_GEN", + allocationSize = 1) */ + @Id + // @GeneratedValue(strategy=GenerationType.TABLE, generator = "ponoGen") + @GeneratedValue(strategy=GenerationType.IDENTITY) + @Column(name = "PONO") + private Integer pono; + @Column(name = "SHIPPING_DATE", nullable = false) + @Temporal(DATE) + private Date shipping_date; + @Column(name = "DELIVERY_ESTIMATE", nullable = false) + private String delivery_estimate; + @ManyToOne + @JoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ID") + private Customer customer; + @ManyToOne + @JoinColumn( + name="EMPNO", + referencedColumnName="EMPNO") + private Employee employee; + public Order() { + } + public Customer getCustomer() { + return this.customer; + } + public void setCustomer(Customer customer) { + this.customer = customer; + } + public Employee getEmployee() { + return this.employee; + } + public void setEmployee (Employee employee) { + this.employee = employee; + } + public Integer getPono () { + return this.pono; + } + public void setPono (Integer pono) { + this.pono = pono; + } + public Date getShipping_date () { + return this.shipping_date; + } + public void setShipping_date (Date shipping_date) { + this.shipping_date = shipping_date; + } + public String getDelivery_estimate () { + return this.delivery_estimate; + } + public void setDelivery_estimate (String delivery_estimate) { + this.delivery_estimate = delivery_estimate; + } +} diff --git a/ch11/nativequery/src/ejbjpa/servlets/NativeQueryTestServlet.java b/ch11/nativequery/src/ejbjpa/servlets/NativeQueryTestServlet.java new file mode 100644 index 0000000..977dea1 --- /dev/null +++ b/ch11/nativequery/src/ejbjpa/servlets/NativeQueryTestServlet.java @@ -0,0 +1,34 @@ +package ejbjpa.servlets; +import java.io.*; +import javax.servlet.*; +import javax.servlet.http.*; +import javax.ejb.EJB; +import java.util.List; +import java.util.Iterator; +import ejbjpa.entities.*; +import ejbjpa.ejb.*; +public class NativeQueryTestServlet extends HttpServlet { + @EJB private NativeQueryTest nativeQueryTest; + public void doGet( + HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + response.setBufferSize(8192); + PrintWriter out = response.getWriter(); + try{ + List employees = nativeQueryTest.getEmployees(); + Iterator i = employees.iterator(); + Employee employee; + while (i.hasNext()) { + employee = (Employee) i.next(); + out.println("Employee id: "+ employee.getEmpno() +"
"); + out.println("First name: "+ employee.getFirstname() +"
"); + out.println("Last name: "+ employee.getLastname() +"
"); + out.println("----------"+ "
"); + } + } + catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/ch11/nativequery/target/WEB-INF/classes/ejbjpa/ejb/NativeQueryTest.class b/ch11/nativequery/target/WEB-INF/classes/ejbjpa/ejb/NativeQueryTest.class new file mode 100644 index 0000000..c949f17 Binary files /dev/null and b/ch11/nativequery/target/WEB-INF/classes/ejbjpa/ejb/NativeQueryTest.class differ diff --git a/ch11/nativequery/target/WEB-INF/classes/ejbjpa/ejb/NativeQueryTestBean.class b/ch11/nativequery/target/WEB-INF/classes/ejbjpa/ejb/NativeQueryTestBean.class new file mode 100644 index 0000000..cb2afe4 Binary files /dev/null and b/ch11/nativequery/target/WEB-INF/classes/ejbjpa/ejb/NativeQueryTestBean.class differ diff --git a/ch11/nativequery/target/WEB-INF/classes/ejbjpa/entities/Customer.class b/ch11/nativequery/target/WEB-INF/classes/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..855839f Binary files /dev/null and b/ch11/nativequery/target/WEB-INF/classes/ejbjpa/entities/Customer.class differ diff --git a/ch11/nativequery/target/WEB-INF/classes/ejbjpa/entities/Employee.class b/ch11/nativequery/target/WEB-INF/classes/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..0da76fa Binary files /dev/null and b/ch11/nativequery/target/WEB-INF/classes/ejbjpa/entities/Employee.class differ diff --git a/ch11/nativequery/target/WEB-INF/classes/ejbjpa/entities/Order.class b/ch11/nativequery/target/WEB-INF/classes/ejbjpa/entities/Order.class new file mode 100644 index 0000000..7c7e67c Binary files /dev/null and b/ch11/nativequery/target/WEB-INF/classes/ejbjpa/entities/Order.class differ diff --git a/ch11/nativequery/target/WEB-INF/classes/ejbjpa/servlets/NativeQueryTestServlet.class b/ch11/nativequery/target/WEB-INF/classes/ejbjpa/servlets/NativeQueryTestServlet.class new file mode 100644 index 0000000..1f6b373 Binary files /dev/null and b/ch11/nativequery/target/WEB-INF/classes/ejbjpa/servlets/NativeQueryTestServlet.class differ diff --git a/ch11/nativequery/target/WEB-INF/lib/ejbsql.jar b/ch11/nativequery/target/WEB-INF/lib/ejbsql.jar new file mode 100644 index 0000000..1b50aa3 Binary files /dev/null and b/ch11/nativequery/target/WEB-INF/lib/ejbsql.jar differ diff --git a/ch11/nativequery/target/WEB-INF/sun-web.xml b/ch11/nativequery/target/WEB-INF/sun-web.xml new file mode 100644 index 0000000..6d07958 --- /dev/null +++ b/ch11/nativequery/target/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /nativequerytest + diff --git a/ch11/nativequery/target/WEB-INF/web.xml b/ch11/nativequery/target/WEB-INF/web.xml new file mode 100644 index 0000000..7b76ca4 --- /dev/null +++ b/ch11/nativequery/target/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + + + NativeQueryTestServlet + ejbjpa.servlets.NativeQueryTestServlet + + + NativeQueryTestServlet + /nativequerytestservlet + + + diff --git a/ch11/nativequery/target/nativequery.war b/ch11/nativequery/target/nativequery.war new file mode 100644 index 0000000..b815594 Binary files /dev/null and b/ch11/nativequery/target/nativequery.war differ diff --git a/ch11/persistencecontext/ejbtarget/META-INF/persistence.xml b/ch11/persistencecontext/ejbtarget/META-INF/persistence.xml new file mode 100644 index 0000000..e966ebb --- /dev/null +++ b/ch11/persistencecontext/ejbtarget/META-INF/persistence.xml @@ -0,0 +1,6 @@ + + + + jdbc/mysqlpool + + diff --git a/ch11/persistencecontext/ejbtarget/ejbjpa/ejb/JpqlTest.class b/ch11/persistencecontext/ejbtarget/ejbjpa/ejb/JpqlTest.class new file mode 100644 index 0000000..fef0797 Binary files /dev/null and b/ch11/persistencecontext/ejbtarget/ejbjpa/ejb/JpqlTest.class differ diff --git a/ch11/persistencecontext/ejbtarget/ejbjpa/ejb/JpqlTestBean.class b/ch11/persistencecontext/ejbtarget/ejbjpa/ejb/JpqlTestBean.class new file mode 100644 index 0000000..becf8ba Binary files /dev/null and b/ch11/persistencecontext/ejbtarget/ejbjpa/ejb/JpqlTestBean.class differ diff --git a/ch11/persistencecontext/ejbtarget/ejbjpa/entities/Customer.class b/ch11/persistencecontext/ejbtarget/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..855839f Binary files /dev/null and b/ch11/persistencecontext/ejbtarget/ejbjpa/entities/Customer.class differ diff --git a/ch11/persistencecontext/ejbtarget/ejbjpa/entities/Employee.class b/ch11/persistencecontext/ejbtarget/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..380c0cf Binary files /dev/null and b/ch11/persistencecontext/ejbtarget/ejbjpa/entities/Employee.class differ diff --git a/ch11/persistencecontext/ejbtarget/ejbjpa/entities/Order.class b/ch11/persistencecontext/ejbtarget/ejbjpa/entities/Order.class new file mode 100644 index 0000000..7c7e67c Binary files /dev/null and b/ch11/persistencecontext/ejbtarget/ejbjpa/entities/Order.class differ diff --git a/ch11/persistencecontext/ejbtarget/ejbjpql.jar b/ch11/persistencecontext/ejbtarget/ejbjpql.jar new file mode 100644 index 0000000..bb7d74d Binary files /dev/null and b/ch11/persistencecontext/ejbtarget/ejbjpql.jar differ diff --git a/ch11/persistencecontext/src/ejbjpa/ejb/JpqlTest.java b/ch11/persistencecontext/src/ejbjpa/ejb/JpqlTest.java new file mode 100644 index 0000000..6886aed --- /dev/null +++ b/ch11/persistencecontext/src/ejbjpa/ejb/JpqlTest.java @@ -0,0 +1,11 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +import java.util.List; +import ejbjpa.entities.*; + +@Remote +public interface JpqlTest { + public List getEmployees(); + public boolean checkIfManaged(); + +} diff --git a/ch11/persistencecontext/src/ejbjpa/ejb/JpqlTestBean.java b/ch11/persistencecontext/src/ejbjpa/ejb/JpqlTestBean.java new file mode 100644 index 0000000..e164abc --- /dev/null +++ b/ch11/persistencecontext/src/ejbjpa/ejb/JpqlTestBean.java @@ -0,0 +1,49 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.List; +import java.util.Iterator; +import ejbjpa.entities.*; +@Stateless +public class JpqlTestBean implements JpqlTest { + @PersistenceContext + private EntityManager em; + public List getEmployees() { + List employees = null; + try { + employees = (List)em.createQuery("SELECT e FROM Employee e") + .getResultList(); + Employee employee; + Iterator i = employees.iterator(); + while (i.hasNext()) { + employee = (Employee) i.next(); + if (!(em.contains(employee))) { + throw new EJBException("PC does not contain this employee object"); + } + } + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return employees; + } + public boolean checkIfManaged(){ + List employees = null; + try { + employees = (List)em.createQuery("SELECT e FROM Employee e") + .getResultList(); + Employee employee; + Iterator i = employees.iterator(); + while (i.hasNext()) { + employee = (Employee) i.next(); + if (!em.contains(employee)) { + return false; + } } + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return true; + } +} \ No newline at end of file diff --git a/ch11/persistencecontext/src/ejbjpa/entities/Customer.java b/ch11/persistencecontext/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..aa187c0 --- /dev/null +++ b/ch11/persistencecontext/src/ejbjpa/entities/Customer.java @@ -0,0 +1,64 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "CUST_NAME", nullable = false) + private String cust_name; + @Column(name = "LOC_ID") + private Integer loc_id; + @Column(name = "EMAIL", nullable = false) + private String email; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToMany(mappedBy="customer", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCust_name() { + return this.cust_name; + } + public void setCust_name(String cust_name) { + this.cust_name = cust_name; + } + public Integer getLoc_id() { + return this.loc_id; + } + public void setLoc_id(Integer loc_id) { + this.loc_id = loc_id; + } + public String getEmail() { + return this.email; + } + public void setEmail(String email) { + this.email = email; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/ch11/persistencecontext/src/ejbjpa/entities/Employee.java b/ch11/persistencecontext/src/ejbjpa/entities/Employee.java new file mode 100644 index 0000000..9751c22 --- /dev/null +++ b/ch11/persistencecontext/src/ejbjpa/entities/Employee.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "EMPLOYEES") +public class Employee implements Serializable { + @Id + @Column(name = "EMPNO") + private Integer empno; + @Column(name = "firstname", nullable = false) + private String firstname; + @Column(name = "lastname", nullable = false) + private String lastname; + @OneToMany(mappedBy="employee", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Employee() { + } + public Integer getEmpno() { + return this.empno; + } + public void setEmpno(Integer empno) { + this.empno = empno; + } + public String getFirstname() { + return this.firstname; + } + public void setFirstname(String firstname) { + this.firstname = firstname; + } + public String getLastname() { + return this.lastname; + } + public void setLastname(String lastname) { + this.lastname = lastname; + } +} diff --git a/ch11/persistencecontext/src/ejbjpa/entities/Order.java b/ch11/persistencecontext/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..b9aa6e0 --- /dev/null +++ b/ch11/persistencecontext/src/ejbjpa/entities/Order.java @@ -0,0 +1,76 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import static javax.persistence.TemporalType.DATE; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.TableGenerator; +import java.util.Date; +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + /* @TableGenerator(name = "ponoGen", + table = "PONO_GEN_TABLE", + pkColumnName = "GEN_NAME", + valueColumnName = "GEN_VALUE", + pkColumnValue = "PONO_GEN", + allocationSize = 1) */ + @Id + // @GeneratedValue(strategy=GenerationType.TABLE, generator = "ponoGen") + @GeneratedValue(strategy=GenerationType.IDENTITY) + @Column(name = "PONO") + private Integer pono; + @Column(name = "SHIPPING_DATE", nullable = false) + @Temporal(DATE) + private Date shipping_date; + @Column(name = "DELIVERY_ESTIMATE", nullable = false) + private String delivery_estimate; + @ManyToOne + @JoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ID") + private Customer customer; + @ManyToOne + @JoinColumn( + name="EMPNO", + referencedColumnName="EMPNO") + private Employee employee; + public Order() { + } + public Customer getCustomer() { + return this.customer; + } + public void setCustomer(Customer customer) { + this.customer = customer; + } + public Employee getEmployee() { + return this.employee; + } + public void setEmployee (Employee employee) { + this.employee = employee; + } + public Integer getPono () { + return this.pono; + } + public void setPono (Integer pono) { + this.pono = pono; + } + public Date getShipping_date () { + return this.shipping_date; + } + public void setShipping_date (Date shipping_date) { + this.shipping_date = shipping_date; + } + public String getDelivery_estimate () { + return this.delivery_estimate; + } + public void setDelivery_estimate (String delivery_estimate) { + this.delivery_estimate = delivery_estimate; + } +} diff --git a/ch11/persistencecontext/src/ejbjpa/servlets/JpqlTestServlet.java b/ch11/persistencecontext/src/ejbjpa/servlets/JpqlTestServlet.java new file mode 100644 index 0000000..c8b15b3 --- /dev/null +++ b/ch11/persistencecontext/src/ejbjpa/servlets/JpqlTestServlet.java @@ -0,0 +1,35 @@ +package ejbjpa.servlets; +import java.io.*; +import javax.servlet.*; +import javax.servlet.http.*; +import javax.ejb.EJB; +import java.util.List; +import java.util.Iterator; +import ejbjpa.entities.*; +import ejbjpa.ejb.*; +public class JpqlTestServlet extends HttpServlet { + @EJB private JpqlTest jpqlTest; + public void doGet( + HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + response.setBufferSize(8192); + PrintWriter out = response.getWriter(); + try{ + List employees = jpqlTest.getEmployees(); + Iterator i = employees.iterator(); + Employee employee; + while (i.hasNext()) { + employee = (Employee) i.next(); + out.println("Employee id: "+ employee.getEmpno() +"
"); + out.println("First name: "+ employee.getFirstname() +"
"); + out.println("Last name: "+ employee.getLastname() +"
"); + out.println("----------"+ "
"); + } + out.println("All employee entities were managed during the checkIfManaged call: "+ jpqlTest.checkIfManaged() +"
"); + } + catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/ch11/persistencecontext/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class b/ch11/persistencecontext/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class new file mode 100644 index 0000000..99066dc Binary files /dev/null and b/ch11/persistencecontext/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class differ diff --git a/ch11/persistencecontext/target/WEB-INF/classes/ejbjpa/servlets/JpqlTestServlet.class b/ch11/persistencecontext/target/WEB-INF/classes/ejbjpa/servlets/JpqlTestServlet.class new file mode 100644 index 0000000..b2c66fa Binary files /dev/null and b/ch11/persistencecontext/target/WEB-INF/classes/ejbjpa/servlets/JpqlTestServlet.class differ diff --git a/ch11/persistencecontext/target/WEB-INF/lib/ejbjpql.jar b/ch11/persistencecontext/target/WEB-INF/lib/ejbjpql.jar new file mode 100644 index 0000000..bb7d74d Binary files /dev/null and b/ch11/persistencecontext/target/WEB-INF/lib/ejbjpql.jar differ diff --git a/ch11/persistencecontext/target/WEB-INF/sun-web.xml b/ch11/persistencecontext/target/WEB-INF/sun-web.xml new file mode 100644 index 0000000..a31b8fe --- /dev/null +++ b/ch11/persistencecontext/target/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /jpqlcontexttest + diff --git a/ch11/persistencecontext/target/WEB-INF/web.xml b/ch11/persistencecontext/target/WEB-INF/web.xml new file mode 100644 index 0000000..ea7183c --- /dev/null +++ b/ch11/persistencecontext/target/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + + + JpqlTestServlet + ejbjpa.servlets.JpqlTestServlet + + + JpqlTestServlet + /jpqltestservlet + + + diff --git a/ch11/persistencecontext/target/persistencecontext.war b/ch11/persistencecontext/target/persistencecontext.war new file mode 100644 index 0000000..e9d0093 Binary files /dev/null and b/ch11/persistencecontext/target/persistencecontext.war differ diff --git a/ch12/sampleapp/ejbtarget/META-INF/persistence.xml b/ch12/sampleapp/ejbtarget/META-INF/persistence.xml new file mode 100644 index 0000000..d3228b8 --- /dev/null +++ b/ch12/sampleapp/ejbtarget/META-INF/persistence.xml @@ -0,0 +1,6 @@ + + + + jdbc/mysqlpool + + diff --git a/ch12/sampleapp/ejbtarget/appejb.jar b/ch12/sampleapp/ejbtarget/appejb.jar new file mode 100644 index 0000000..f408900 Binary files /dev/null and b/ch12/sampleapp/ejbtarget/appejb.jar differ diff --git a/ch12/sampleapp/ejbtarget/ejbjpa/ejb/Cart.class b/ch12/sampleapp/ejbtarget/ejbjpa/ejb/Cart.class new file mode 100644 index 0000000..439fad7 Binary files /dev/null and b/ch12/sampleapp/ejbtarget/ejbjpa/ejb/Cart.class differ diff --git a/ch12/sampleapp/ejbtarget/ejbjpa/ejb/CartBean.class b/ch12/sampleapp/ejbtarget/ejbjpa/ejb/CartBean.class new file mode 100644 index 0000000..8036cc9 Binary files /dev/null and b/ch12/sampleapp/ejbtarget/ejbjpa/ejb/CartBean.class differ diff --git a/ch12/sampleapp/ejbtarget/ejbjpa/ejb/OrderBean.class b/ch12/sampleapp/ejbtarget/ejbjpa/ejb/OrderBean.class new file mode 100644 index 0000000..740847e Binary files /dev/null and b/ch12/sampleapp/ejbtarget/ejbjpa/ejb/OrderBean.class differ diff --git a/ch12/sampleapp/ejbtarget/ejbjpa/ejb/OrderSample.class b/ch12/sampleapp/ejbtarget/ejbjpa/ejb/OrderSample.class new file mode 100644 index 0000000..d2d2de6 Binary files /dev/null and b/ch12/sampleapp/ejbtarget/ejbjpa/ejb/OrderSample.class differ diff --git a/ch12/sampleapp/ejbtarget/ejbjpa/entities/Customer.class b/ch12/sampleapp/ejbtarget/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..855839f Binary files /dev/null and b/ch12/sampleapp/ejbtarget/ejbjpa/entities/Customer.class differ diff --git a/ch12/sampleapp/ejbtarget/ejbjpa/entities/Employee.class b/ch12/sampleapp/ejbtarget/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..380c0cf Binary files /dev/null and b/ch12/sampleapp/ejbtarget/ejbjpa/entities/Employee.class differ diff --git a/ch12/sampleapp/ejbtarget/ejbjpa/entities/Order.class b/ch12/sampleapp/ejbtarget/ejbjpa/entities/Order.class new file mode 100644 index 0000000..7030ba9 Binary files /dev/null and b/ch12/sampleapp/ejbtarget/ejbjpa/entities/Order.class differ diff --git a/ch12/sampleapp/ejbtarget/ejbjpa/entities/ShoppingCart.class b/ch12/sampleapp/ejbtarget/ejbjpa/entities/ShoppingCart.class new file mode 100644 index 0000000..f74950a Binary files /dev/null and b/ch12/sampleapp/ejbtarget/ejbjpa/entities/ShoppingCart.class differ diff --git a/ch12/sampleapp/ejbtarget/ejbjpa/entities/ShoppingCartKey.class b/ch12/sampleapp/ejbtarget/ejbjpa/entities/ShoppingCartKey.class new file mode 100644 index 0000000..c13e03f Binary files /dev/null and b/ch12/sampleapp/ejbtarget/ejbjpa/entities/ShoppingCartKey.class differ diff --git a/ch12/sampleapp/src/ejbjpa/ejb/Cart.java b/ch12/sampleapp/src/ejbjpa/ejb/Cart.java new file mode 100644 index 0000000..babd296 --- /dev/null +++ b/ch12/sampleapp/src/ejbjpa/ejb/Cart.java @@ -0,0 +1,14 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +import java.util.List; +import ejbjpa.entities.*; + +@Remote +public interface Cart { + public void initialize(Integer cust_id); + public void addItem(String item_id, Integer quantity, Double price); + public void removeItem(String item_id); + public List getItems(); + public Integer emptyCart(); + public void clearCartInstance(); +} diff --git a/ch12/sampleapp/src/ejbjpa/ejb/CartBean.java b/ch12/sampleapp/src/ejbjpa/ejb/CartBean.java new file mode 100644 index 0000000..f575592 --- /dev/null +++ b/ch12/sampleapp/src/ejbjpa/ejb/CartBean.java @@ -0,0 +1,61 @@ +package ejbjpa.ejb; +import java.util.List; +import javax.ejb.Remove; +import javax.ejb.Stateful; +import javax.ejb.EJBException; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.PersistenceContextType; +import ejbjpa.entities.*; +@Stateful +public class CartBean implements Cart { +@PersistenceContext(type=PersistenceContextType.EXTENDED) +EntityManager em; +Integer custId; +List items; +public void initialize(Integer cust_id) { + if (cust_id == null) { + throw new EJBException("Null cust_id provided."); + } else { + custId = cust_id; + } + } + public void addItem(String item_id, Integer quantity, Double price) { + ShoppingCart cart = (ShoppingCart) em.find(ShoppingCart.class, new ShoppingCartKey(custId, item_id)); + if(cart == null){ + cart = new ShoppingCart(); + cart.setCart_id(custId); + cart.setBook_id(item_id); + cart.setUnits(quantity); + cart.setUnit_price(price); + em.persist(cart); + } else { + throw new EJBException("This item is already in cart."); + } + } + public void removeItem(String item_id) { + ShoppingCart cart = (ShoppingCart) em.find(ShoppingCart.class, new ShoppingCartKey(custId, item_id)); + if(cart == null){ + throw new EJBException("This item is not in cart."); + } else { + em.remove(cart); + } + } +public List getItems() { + items = (List)em.createQuery("SELECT s FROM ShoppingCart s WHERE s.cart_id =:cust_id") + .setParameter("cust_id", custId) + .getResultList(); + return items; +} +@Remove +public Integer emptyCart() { + Integer num =0; + num = em.createQuery("DELETE FROM ShoppingCart s WHERE s.cart_id =:cust_id") + .setParameter("cust_id", custId) + .executeUpdate(); + return num; +} +@Remove +public void clearCartInstance() { + } +} diff --git a/ch12/sampleapp/src/ejbjpa/ejb/OrderBean.java b/ch12/sampleapp/src/ejbjpa/ejb/OrderBean.java new file mode 100644 index 0000000..1072ced --- /dev/null +++ b/ch12/sampleapp/src/ejbjpa/ejb/OrderBean.java @@ -0,0 +1,36 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.List; +import ejbjpa.entities.*; +@Stateless +public class OrderBean implements OrderSample { + @PersistenceContext + private EntityManager em; + public void placeOrder(Integer cust_id, + Integer empno) { + try { + Customer cust = (Customer) em.find(Customer.class, cust_id); + Employee emp = (Employee) em.find(Employee.class, empno); + Order order = new Order(); + order.setCustomer(cust); + order.setEmployee(emp); + em.persist(order); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + } + public List getOrdersList() { + List orders = null; + try { + orders = (List)em.createQuery("SELECT o FROM Order o") + .getResultList(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return orders; + } +} diff --git a/ch12/sampleapp/src/ejbjpa/ejb/OrderSample.java b/ch12/sampleapp/src/ejbjpa/ejb/OrderSample.java new file mode 100644 index 0000000..2661b52 --- /dev/null +++ b/ch12/sampleapp/src/ejbjpa/ejb/OrderSample.java @@ -0,0 +1,11 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +import java.util.List; +import ejbjpa.entities.*; + +@Remote +public interface OrderSample { + public void placeOrder(Integer cust_id, Integer empno); +public List getOrdersList() ; + +} diff --git a/ch12/sampleapp/src/ejbjpa/entities/Customer.java b/ch12/sampleapp/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..aa187c0 --- /dev/null +++ b/ch12/sampleapp/src/ejbjpa/entities/Customer.java @@ -0,0 +1,64 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "CUST_NAME", nullable = false) + private String cust_name; + @Column(name = "LOC_ID") + private Integer loc_id; + @Column(name = "EMAIL", nullable = false) + private String email; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToMany(mappedBy="customer", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCust_name() { + return this.cust_name; + } + public void setCust_name(String cust_name) { + this.cust_name = cust_name; + } + public Integer getLoc_id() { + return this.loc_id; + } + public void setLoc_id(Integer loc_id) { + this.loc_id = loc_id; + } + public String getEmail() { + return this.email; + } + public void setEmail(String email) { + this.email = email; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/ch12/sampleapp/src/ejbjpa/entities/Employee.java b/ch12/sampleapp/src/ejbjpa/entities/Employee.java new file mode 100644 index 0000000..9751c22 --- /dev/null +++ b/ch12/sampleapp/src/ejbjpa/entities/Employee.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "EMPLOYEES") +public class Employee implements Serializable { + @Id + @Column(name = "EMPNO") + private Integer empno; + @Column(name = "firstname", nullable = false) + private String firstname; + @Column(name = "lastname", nullable = false) + private String lastname; + @OneToMany(mappedBy="employee", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Employee() { + } + public Integer getEmpno() { + return this.empno; + } + public void setEmpno(Integer empno) { + this.empno = empno; + } + public String getFirstname() { + return this.firstname; + } + public void setFirstname(String firstname) { + this.firstname = firstname; + } + public String getLastname() { + return this.lastname; + } + public void setLastname(String lastname) { + this.lastname = lastname; + } +} diff --git a/ch12/sampleapp/src/ejbjpa/entities/Order.java b/ch12/sampleapp/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..c550cad --- /dev/null +++ b/ch12/sampleapp/src/ejbjpa/entities/Order.java @@ -0,0 +1,65 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import static javax.persistence.TemporalType.DATE; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import java.util.Date; +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + @Id + @Column(name = "PONO") + private Integer pono; + @Column(name = "SHIPPING_DATE", nullable = false) + @Temporal(DATE) + private Date shipping_date; + @Column(name = "DELIVERY_ESTIMATE", nullable = false) + private String delivery_estimate; + @ManyToOne + @JoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ID") + private Customer customer; + @ManyToOne + @JoinColumn( + name="EMPNO", + referencedColumnName="EMPNO") + private Employee employee; + public Order() { + } + public Customer getCustomer() { + return this.customer; + } + public void setCustomer(Customer customer) { + this.customer = customer; + } + public Employee getEmployee() { + return this.employee; + } + public void setEmployee (Employee employee) { + this.employee = employee; + } + public Integer getPono () { + return this.pono; + } + public void setPono (Integer pono) { + this.pono = pono; + } + public Date getShipping_date () { + return this.shipping_date; + } + public void setShipping_date (Date shipping_date) { + this.shipping_date = shipping_date; + } + public String getDelivery_estimate () { + return this.delivery_estimate; + } + public void setDelivery_estimate (String delivery_estimate) { + this.delivery_estimate = delivery_estimate; + } +} diff --git a/ch12/sampleapp/src/ejbjpa/entities/ShoppingCart.java b/ch12/sampleapp/src/ejbjpa/entities/ShoppingCart.java new file mode 100644 index 0000000..7d169f2 --- /dev/null +++ b/ch12/sampleapp/src/ejbjpa/entities/ShoppingCart.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.IdClass; +import javax.persistence.Table; +@Entity +@Table(name = "SHOPPINGCARTS") +@IdClass(value = ejbjpa.entities.ShoppingCartKey.class) +public class ShoppingCart implements Serializable { + @Id + @Column(name = "CART_ID") + private Integer cart_id; + @Id + @Column(name = "BOOK_ID") + private String book_id; + @Column(name = "UNITS", nullable = false) + private Integer units; + @Column(name = "UNIT_PRICE", nullable = false) + private Double unit_price; + public ShoppingCart() { + } + public Integer getCart_id() { + return this.cart_id; + } + public void setCart_id(Integer cart_id) { + this.cart_id = cart_id; + } + public String getBook_id() { + return this.book_id; + } + public void setBook_id(String book_id) { + this.book_id = book_id; + } + public Integer getUnits () { + return this.units; + } + public void setUnits(Integer units) { + this.units = units; + } + public Double getUnit_price() { + return this.unit_price; + } + public void setUnit_price(Double unit_price) { + this.unit_price = unit_price; + } +} diff --git a/ch12/sampleapp/src/ejbjpa/entities/ShoppingCartKey.java b/ch12/sampleapp/src/ejbjpa/entities/ShoppingCartKey.java new file mode 100644 index 0000000..5c29dbe --- /dev/null +++ b/ch12/sampleapp/src/ejbjpa/entities/ShoppingCartKey.java @@ -0,0 +1,37 @@ +package ejbjpa.entities; +import java.io.Serializable; + +public final class ShoppingCartKey implements Serializable { + public Integer cart_id; + public String book_id; + public ShoppingCartKey() {} + public ShoppingCartKey(Integer cart_id, String book_id) { + this.cart_id = cart_id; + this.book_id = book_id; + } + +public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof ShoppingCartKey)) { + return false; + } + ShoppingCartKey other = (ShoppingCartKey) obj; + if (cart_id != null && other.cart_id!= null && this.cart_id.equals(other.cart_id)) { + return (book_id != null && other.book_id!= null && this.book_id.equals(other.book_id)); + } + return false; +} + +public int hashCode() { + if (cart_id!=null && book_id!=null) { + return (cart_id.hashCode() ^ book_id.hashCode()); + } + return 0; +} + +} \ No newline at end of file diff --git a/ch12/sampletest/src/ejbjpa/servlets/TestSampleServlet.java b/ch12/sampletest/src/ejbjpa/servlets/TestSampleServlet.java new file mode 100644 index 0000000..33ad3cd --- /dev/null +++ b/ch12/sampletest/src/ejbjpa/servlets/TestSampleServlet.java @@ -0,0 +1,47 @@ +package ejbjpa.servlets; +import java.io.*; +import javax.servlet.*; +import javax.servlet.http.*; +import javax.ejb.EJB; +import javax.naming.InitialContext; +import java.util.List; +import java.util.Iterator; +import ejbjpa.entities.*; +import ejbjpa.ejb.*; +public class TestSampleServlet extends HttpServlet { + @EJB(name="ejb/CartBean", beanInterface=Cart.class) + public void doGet( + HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + response.setBufferSize(8192); + PrintWriter out = response.getWriter(); + HttpSession session = request.getSession(); + Cart cart = (Cart)session.getAttribute("cartatt"); + try{ + if (cart == null) { + cart = (Cart) (new InitialContext()).lookup("java:comp/env/ejb/CartBean"); + session.setAttribute("cartatt", cart); + cart.initialize(2); + out.println("Cart initialized" +"
"); + } + cart.addItem("1430209631", 1, 29.69); + cart.addItem("1590595300", 2, 32.99); + List items = cart.getItems(); + Iterator i = items.iterator(); + ShoppingCart shoppingCart; + while (i.hasNext()) { + shoppingCart = (ShoppingCart) i.next(); + out.println("Cart id: "+ shoppingCart.getCart_id() +"
"); + out.println("Book id: "+ shoppingCart.getBook_id() +"
"); + out.println("Quantity: "+ shoppingCart.getUnits() +"
"); + out.println("Unit price: "+ shoppingCart.getUnit_price() +"
"); + out.println("----------"+ "
"); + } + } + catch (Exception e){ + e.printStackTrace(); + session.removeAttribute("cartatt"); + } + } +} diff --git a/ch12/sampletest/src/ejbjpa/servlets/TestSampleServletCont.java b/ch12/sampletest/src/ejbjpa/servlets/TestSampleServletCont.java new file mode 100644 index 0000000..7816aca --- /dev/null +++ b/ch12/sampletest/src/ejbjpa/servlets/TestSampleServletCont.java @@ -0,0 +1,55 @@ +package ejbjpa.servlets; +import java.io.*; +import javax.servlet.*; +import javax.servlet.http.*; +import javax.ejb.EJB; +import javax.naming.InitialContext; +import java.util.List; +import java.util.Iterator; +import javax.ejb.EJBException; +import ejbjpa.entities.*; +import ejbjpa.ejb.*; +public class TestSampleServletCont extends HttpServlet { + @EJB(name="ejb/CartBean", beanInterface=Cart.class) + public void doGet( + HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + response.setBufferSize(8192); + PrintWriter out = response.getWriter(); + HttpSession session = request.getSession(); + Cart cart = (Cart)session.getAttribute("cartatt"); + try{ + if (cart == null) { + cart = (Cart) (new InitialContext()).lookup("java:comp/env/ejb/CartBean"); + session.setAttribute("cartatt", cart); + cart.initialize(2); + out.println("Cart initialized" +"
"); + } + out.println("Remove the first item from the cart "+ "
"); + cart.removeItem("1430209631"); + cart.removeItem("1590595300"); + cart.addItem("1590595300", 2, 32.99); + + List items = cart.getItems(); + ShoppingCart shoppingCart; + Iterator i = items.iterator(); + while (i.hasNext()) { + shoppingCart = (ShoppingCart) i.next(); + out.println("Cart id: "+ shoppingCart.getCart_id() +"
"); + out.println("Book id: "+ shoppingCart.getBook_id() +"
"); + out.println("Quantity: "+ shoppingCart.getUnits() +"
"); + out.println("Unit price: "+ shoppingCart.getUnit_price() +"
"); + out.println("----------"+ "
"); + } + Integer num = cart.emptyCart(); + out.println(num + " item(s) removed " + "
"); + } + catch (Exception e){ + e.printStackTrace(); + } + finally { + session.removeAttribute("cartatt"); + } + } +} diff --git a/ch12/sampletest/target/WEB-INF/classes/ejbjpa/servlets/TestSampleServlet.class b/ch12/sampletest/target/WEB-INF/classes/ejbjpa/servlets/TestSampleServlet.class new file mode 100644 index 0000000..a628f0b Binary files /dev/null and b/ch12/sampletest/target/WEB-INF/classes/ejbjpa/servlets/TestSampleServlet.class differ diff --git a/ch12/sampletest/target/WEB-INF/classes/ejbjpa/servlets/TestSampleServletCont.class b/ch12/sampletest/target/WEB-INF/classes/ejbjpa/servlets/TestSampleServletCont.class new file mode 100644 index 0000000..a4d7296 Binary files /dev/null and b/ch12/sampletest/target/WEB-INF/classes/ejbjpa/servlets/TestSampleServletCont.class differ diff --git a/ch12/sampletest/target/WEB-INF/lib/appejb.jar b/ch12/sampletest/target/WEB-INF/lib/appejb.jar new file mode 100644 index 0000000..f408900 Binary files /dev/null and b/ch12/sampletest/target/WEB-INF/lib/appejb.jar differ diff --git a/ch12/sampletest/target/WEB-INF/sun-web.xml b/ch12/sampletest/target/WEB-INF/sun-web.xml new file mode 100644 index 0000000..f355dc9 --- /dev/null +++ b/ch12/sampletest/target/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /sampletest + diff --git a/ch12/sampletest/target/WEB-INF/web.xml b/ch12/sampletest/target/WEB-INF/web.xml new file mode 100644 index 0000000..e839117 --- /dev/null +++ b/ch12/sampletest/target/WEB-INF/web.xml @@ -0,0 +1,33 @@ + + + + TestSampleServlet + ejbjpa.servlets.TestSampleServlet + + + TestSampleServlet + /testsampleservlet + + + TestSampleServletCont + ejbjpa.servlets.TestSampleServletCont + + + TestSampleServletCont + /testsampleservletcont + + + + 30 + + + + ejb/CartBean + Session + ejbjpa.ejb.Cart + + + diff --git a/ch12/sampletest/target/sampletest.war b/ch12/sampletest/target/sampletest.war new file mode 100644 index 0000000..e07320d Binary files /dev/null and b/ch12/sampletest/target/sampletest.war differ diff --git a/ch13/twomanagers/ejbtarget/META-INF/persistence.xml b/ch13/twomanagers/ejbtarget/META-INF/persistence.xml new file mode 100644 index 0000000..950fb27 --- /dev/null +++ b/ch13/twomanagers/ejbtarget/META-INF/persistence.xml @@ -0,0 +1,9 @@ + + + + jdbc/mysqlpool + + + jdbc/mysqlpool + + diff --git a/ch13/twomanagers/ejbtarget/ejbjpa/ejb/OrderTest.class b/ch13/twomanagers/ejbtarget/ejbjpa/ejb/OrderTest.class new file mode 100644 index 0000000..4c4db4d Binary files /dev/null and b/ch13/twomanagers/ejbtarget/ejbjpa/ejb/OrderTest.class differ diff --git a/ch13/twomanagers/ejbtarget/ejbjpa/ejb/OrderTestBean.class b/ch13/twomanagers/ejbtarget/ejbjpa/ejb/OrderTestBean.class new file mode 100644 index 0000000..02ea4f4 Binary files /dev/null and b/ch13/twomanagers/ejbtarget/ejbjpa/ejb/OrderTestBean.class differ diff --git a/ch13/twomanagers/ejbtarget/ejbjpa/entities/Customer.class b/ch13/twomanagers/ejbtarget/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..855839f Binary files /dev/null and b/ch13/twomanagers/ejbtarget/ejbjpa/entities/Customer.class differ diff --git a/ch13/twomanagers/ejbtarget/ejbjpa/entities/Employee.class b/ch13/twomanagers/ejbtarget/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..380c0cf Binary files /dev/null and b/ch13/twomanagers/ejbtarget/ejbjpa/entities/Employee.class differ diff --git a/ch13/twomanagers/ejbtarget/ejbjpa/entities/Order.class b/ch13/twomanagers/ejbtarget/ejbjpa/entities/Order.class new file mode 100644 index 0000000..7c7e67c Binary files /dev/null and b/ch13/twomanagers/ejbtarget/ejbjpa/entities/Order.class differ diff --git a/ch13/twomanagers/ejbtarget/ejborder.jar b/ch13/twomanagers/ejbtarget/ejborder.jar new file mode 100644 index 0000000..5ca5b25 Binary files /dev/null and b/ch13/twomanagers/ejbtarget/ejborder.jar differ diff --git a/ch13/twomanagers/src/ejbjpa/ejb/OrderTest.java b/ch13/twomanagers/src/ejbjpa/ejb/OrderTest.java new file mode 100644 index 0000000..89cdb5d --- /dev/null +++ b/ch13/twomanagers/src/ejbjpa/ejb/OrderTest.java @@ -0,0 +1,7 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +@Remote +public interface OrderTest { + public Integer setOrder(Integer cust_id, Integer empno); + public String changeOrderEmpTest(Integer pono, Integer empno, Integer custid); +} diff --git a/ch13/twomanagers/src/ejbjpa/ejb/OrderTestBean.java b/ch13/twomanagers/src/ejbjpa/ejb/OrderTestBean.java new file mode 100644 index 0000000..89f3e04 --- /dev/null +++ b/ch13/twomanagers/src/ejbjpa/ejb/OrderTestBean.java @@ -0,0 +1,68 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.annotation.Resource; +import javax.ejb.SessionContext; + +import ejbjpa.entities.*; +@Stateless +public class OrderTestBean implements OrderTest { + @PersistenceContext(unitName="contaneremtest1-pu") + private EntityManager em1; + @PersistenceContext(unitName="contaneremtest2-pu") + private EntityManager em2; + // + @Resource + private SessionContext ctx; + // + public Integer setOrder(Integer cust_id, Integer empno) { + Integer order_pono; + try { + Customer cust = (Customer) em1.find(Customer.class, cust_id); + Employee emp = (Employee) em1.find(Employee.class, empno); + Order order1 = new Order(); + order1.setCustomer(cust); + order1.setEmployee(emp); + em1.persist(order1); + em1.flush(); + em1.refresh(order1); + order_pono = order1.getPono(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return order_pono; + } + public String changeOrderEmpTest(Integer pono, Integer empno, Integer custid) { + String order_details; + try { + //finds the order + Order order1 = (Order) em1.find(Order.class, pono); + //shows the original order + order_details = "order "+ order1.getPono()+ " emp: " + order1.getEmployee().getLastname()+" cust: " + order1.getCustomer().getCust_name()+"
"; + //updates order1 and synchronize it to the database + Employee emp = (Employee) em1.find(Employee.class, empno); + order1.setEmployee(emp); + em1.flush(); + em1.refresh(order1); + //shows the change in order1 obtained from the database + order_details = order_details+"order "+ order1.getPono()+ " emp: " + order1.getEmployee().getLastname()+" cust: " + order1.getCustomer().getCust_name()+"
"; + //obtains the same order with the other EntityManager + Order order2 = (Order) em2.find(Order.class, pono); + //change order2 and synchronize it to the database + Customer cust = (Customer) em1.find(Customer.class, custid); + order2.setCustomer(cust); + em2.flush(); + em2.refresh(order2); + //shows the change in order2 obtained from the database + order_details = order_details+"order "+ order2.getPono()+ " emp: " + order2.getEmployee().getLastname()+" cust: " + order2.getCustomer().getCust_name()+"
"; + // + ctx.setRollbackOnly(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return order_details; + } +} diff --git a/ch13/twomanagers/src/ejbjpa/entities/Customer.java b/ch13/twomanagers/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..aa187c0 --- /dev/null +++ b/ch13/twomanagers/src/ejbjpa/entities/Customer.java @@ -0,0 +1,64 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "CUST_NAME", nullable = false) + private String cust_name; + @Column(name = "LOC_ID") + private Integer loc_id; + @Column(name = "EMAIL", nullable = false) + private String email; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToMany(mappedBy="customer", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCust_name() { + return this.cust_name; + } + public void setCust_name(String cust_name) { + this.cust_name = cust_name; + } + public Integer getLoc_id() { + return this.loc_id; + } + public void setLoc_id(Integer loc_id) { + this.loc_id = loc_id; + } + public String getEmail() { + return this.email; + } + public void setEmail(String email) { + this.email = email; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/ch13/twomanagers/src/ejbjpa/entities/Employee.java b/ch13/twomanagers/src/ejbjpa/entities/Employee.java new file mode 100644 index 0000000..9751c22 --- /dev/null +++ b/ch13/twomanagers/src/ejbjpa/entities/Employee.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "EMPLOYEES") +public class Employee implements Serializable { + @Id + @Column(name = "EMPNO") + private Integer empno; + @Column(name = "firstname", nullable = false) + private String firstname; + @Column(name = "lastname", nullable = false) + private String lastname; + @OneToMany(mappedBy="employee", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Employee() { + } + public Integer getEmpno() { + return this.empno; + } + public void setEmpno(Integer empno) { + this.empno = empno; + } + public String getFirstname() { + return this.firstname; + } + public void setFirstname(String firstname) { + this.firstname = firstname; + } + public String getLastname() { + return this.lastname; + } + public void setLastname(String lastname) { + this.lastname = lastname; + } +} diff --git a/ch13/twomanagers/src/ejbjpa/entities/Order.java b/ch13/twomanagers/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..b9aa6e0 --- /dev/null +++ b/ch13/twomanagers/src/ejbjpa/entities/Order.java @@ -0,0 +1,76 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import static javax.persistence.TemporalType.DATE; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.TableGenerator; +import java.util.Date; +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + /* @TableGenerator(name = "ponoGen", + table = "PONO_GEN_TABLE", + pkColumnName = "GEN_NAME", + valueColumnName = "GEN_VALUE", + pkColumnValue = "PONO_GEN", + allocationSize = 1) */ + @Id + // @GeneratedValue(strategy=GenerationType.TABLE, generator = "ponoGen") + @GeneratedValue(strategy=GenerationType.IDENTITY) + @Column(name = "PONO") + private Integer pono; + @Column(name = "SHIPPING_DATE", nullable = false) + @Temporal(DATE) + private Date shipping_date; + @Column(name = "DELIVERY_ESTIMATE", nullable = false) + private String delivery_estimate; + @ManyToOne + @JoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ID") + private Customer customer; + @ManyToOne + @JoinColumn( + name="EMPNO", + referencedColumnName="EMPNO") + private Employee employee; + public Order() { + } + public Customer getCustomer() { + return this.customer; + } + public void setCustomer(Customer customer) { + this.customer = customer; + } + public Employee getEmployee() { + return this.employee; + } + public void setEmployee (Employee employee) { + this.employee = employee; + } + public Integer getPono () { + return this.pono; + } + public void setPono (Integer pono) { + this.pono = pono; + } + public Date getShipping_date () { + return this.shipping_date; + } + public void setShipping_date (Date shipping_date) { + this.shipping_date = shipping_date; + } + public String getDelivery_estimate () { + return this.delivery_estimate; + } + public void setDelivery_estimate (String delivery_estimate) { + this.delivery_estimate = delivery_estimate; + } +} diff --git a/ch13/twomanagers/src/ejbjpa/servlets/EmEjbTestServlet.java b/ch13/twomanagers/src/ejbjpa/servlets/EmEjbTestServlet.java new file mode 100644 index 0000000..7166d13 --- /dev/null +++ b/ch13/twomanagers/src/ejbjpa/servlets/EmEjbTestServlet.java @@ -0,0 +1,29 @@ +package ejbjpa.servlets; +import java.io.*; +import javax.servlet.*; +import javax.servlet.http.*; +import javax.ejb.EJB; +import ejbjpa.entities.*; +import ejbjpa.ejb.*; +public class EmEjbTestServlet extends HttpServlet { + @EJB private OrderTest orderTest; + public void doGet( + HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + response.setBufferSize(8192); + PrintWriter out = response.getWriter(); + Integer custid1=1; + Integer custid2=2; + Integer empno1=1; + Integer empno2=2; + try{ + Integer pono = orderTest.setOrder(custid1,empno1); + out.println("Created order "+ pono +"
"); + out.println(orderTest.changeOrderEmpTest(pono,empno2, custid2)); + } + catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/ch13/twomanagers/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class b/ch13/twomanagers/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class new file mode 100644 index 0000000..176e65c Binary files /dev/null and b/ch13/twomanagers/target/WEB-INF/classes/ejbjpa/servlets/EmEjbTestServlet.class differ diff --git a/ch13/twomanagers/target/WEB-INF/lib/ejborder.jar b/ch13/twomanagers/target/WEB-INF/lib/ejborder.jar new file mode 100644 index 0000000..5ca5b25 Binary files /dev/null and b/ch13/twomanagers/target/WEB-INF/lib/ejborder.jar differ diff --git a/ch13/twomanagers/target/WEB-INF/sun-web.xml b/ch13/twomanagers/target/WEB-INF/sun-web.xml new file mode 100644 index 0000000..7e5d052 --- /dev/null +++ b/ch13/twomanagers/target/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /emejbtest + diff --git a/ch13/twomanagers/target/WEB-INF/web.xml b/ch13/twomanagers/target/WEB-INF/web.xml new file mode 100644 index 0000000..c5a2358 --- /dev/null +++ b/ch13/twomanagers/target/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + + + EmEjbTestServlet + ejbjpa.servlets.EmEjbTestServlet + + + EmEjbTestServlet + /emejbtestservlet + + + diff --git a/ch13/twomanagers/target/contmanagedem.war b/ch13/twomanagers/target/contmanagedem.war new file mode 100644 index 0000000..bea821a Binary files /dev/null and b/ch13/twomanagers/target/contmanagedem.war differ diff --git a/ch14/sampleapp/src/ejbjpa/jsfbeans/BookJSFBean.java b/ch14/sampleapp/src/ejbjpa/jsfbeans/BookJSFBean.java new file mode 100644 index 0000000..f553bd0 --- /dev/null +++ b/ch14/sampleapp/src/ejbjpa/jsfbeans/BookJSFBean.java @@ -0,0 +1,28 @@ +package ejbjpa.jsfbeans; +import javax.ejb.EJB; +import javax.naming.InitialContext; +import javax.faces.context.FacesContext; +import javax.servlet.http.HttpSession; +import java.util.List; +import ejbjpa.ejb.*; +import ejbjpa.entities.*; + +import java.sql.*; +import javax.sql.DataSource; + +public class BookJSFBean { + private Connection connDb; + public void openConnection() throws Exception { + if(connDb != null) + return; + DataSource dataSource = (DataSource) (new InitialContext()).lookup("java:comp/env/jdbc/mysqlpool"); + connDb = dataSource.getConnection(); + } + public ResultSet getAllBooks() throws Exception { + ResultSet rslt = null; + this.openConnection(); + Statement stmt = connDb.createStatement(); + rslt = stmt.executeQuery("SELECT * FROM books"); + return rslt; + } +} diff --git a/ch14/sampleapp/src/ejbjpa/jsfbeans/OrderJSFBean.java b/ch14/sampleapp/src/ejbjpa/jsfbeans/OrderJSFBean.java new file mode 100644 index 0000000..961d8ae --- /dev/null +++ b/ch14/sampleapp/src/ejbjpa/jsfbeans/OrderJSFBean.java @@ -0,0 +1,73 @@ +package ejbjpa.jsfbeans; +import javax.ejb.EJB; +import javax.naming.InitialContext; +import javax.faces.context.FacesContext; +import javax.servlet.http.HttpSession; +import java.util.List; +import java.util.Map; +import ejbjpa.ejb.*; +import ejbjpa.entities.*; + +@EJB(name="ejb/CartBean", beanInterface=Cart.class) +public class OrderJSFBean { + private Cart cart; + @EJB + private OrderSample order; + private List cartItems; + private Integer custId; + public OrderJSFBean() { + custId = Integer.parseInt( + FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName()); + try{ + if (cart == null) { + cart = (Cart) (new InitialContext()).lookup("java:comp/env/ejb/CartBean"); + } + cart.initialize(custId); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public Integer getCustId() { + return custId; + } + public List getCartItems() { + cartItems = null; + try { + cartItems = cart.getItems(); + } catch (Exception e) { + e.printStackTrace(); + } + return cartItems; + } + public void addToCart() { + try { + FacesContext cxt = FacesContext.getCurrentInstance(); + Map params = cxt.getExternalContext().getRequestParameterMap(); + String isbn = (String)params.get("isbn"); + String price_str = (String)params.get("price"); + Double price =new Double(price_str); + cart.addItem(isbn, 1, price); + } catch (Exception e) { + e.printStackTrace(); + } + } + public void removeFromCart() { + try { + FacesContext cxt = FacesContext.getCurrentInstance(); + Map params = cxt.getExternalContext().getRequestParameterMap(); + String itemId = (String)params.get("itemId"); + cart.removeItem(itemId); + } catch (Exception e) { + e.printStackTrace(); + } + } + public String ProceedToCheckout() { + try { + order.placeOrder(custId, 1); + } catch (Exception e) { + e.printStackTrace(); + } + return "continue"; + } +} diff --git a/ch14/sampleapp/target/WEB-INF/classes/ejbjpa/jsfbeans/BookJSFBean.class b/ch14/sampleapp/target/WEB-INF/classes/ejbjpa/jsfbeans/BookJSFBean.class new file mode 100644 index 0000000..9e4347e Binary files /dev/null and b/ch14/sampleapp/target/WEB-INF/classes/ejbjpa/jsfbeans/BookJSFBean.class differ diff --git a/ch14/sampleapp/target/WEB-INF/classes/ejbjpa/jsfbeans/OrderJSFBean.class b/ch14/sampleapp/target/WEB-INF/classes/ejbjpa/jsfbeans/OrderJSFBean.class new file mode 100644 index 0000000..8dd32d5 Binary files /dev/null and b/ch14/sampleapp/target/WEB-INF/classes/ejbjpa/jsfbeans/OrderJSFBean.class differ diff --git a/ch14/sampleapp/target/WEB-INF/faces-config.xml b/ch14/sampleapp/target/WEB-INF/faces-config.xml new file mode 100644 index 0000000..18c7537 --- /dev/null +++ b/ch14/sampleapp/target/WEB-INF/faces-config.xml @@ -0,0 +1,34 @@ + + + + OrderJSFBean + ejbjpa.jsfbeans.OrderJSFBean + + session + + + book + ejbjpa.jsfbeans.BookJSFBean + + session + + + + + By clicking the "Move to cart" button on the index.jsp page you move to showcart.jsp + + showcart + /showcart.jsp + + + + Clicking the "Continue shopping" turns you back to index.jsp showing the list of books available + + continue + /index.jsp + + + diff --git a/ch14/sampleapp/target/WEB-INF/lib/appejb.jar b/ch14/sampleapp/target/WEB-INF/lib/appejb.jar new file mode 100644 index 0000000..df2d8f5 Binary files /dev/null and b/ch14/sampleapp/target/WEB-INF/lib/appejb.jar differ diff --git a/ch14/sampleapp/target/WEB-INF/sun-web.xml b/ch14/sampleapp/target/WEB-INF/sun-web.xml new file mode 100644 index 0000000..34edc32 --- /dev/null +++ b/ch14/sampleapp/target/WEB-INF/sun-web.xml @@ -0,0 +1,9 @@ + + + + /sampleapp + + testRole + testRole + + diff --git a/ch14/sampleapp/target/WEB-INF/web.xml b/ch14/sampleapp/target/WEB-INF/web.xml new file mode 100644 index 0000000..4e4a90b --- /dev/null +++ b/ch14/sampleapp/target/WEB-INF/web.xml @@ -0,0 +1,56 @@ + + + + + testing web app + /* + POST + GET + + + testRole + + + NONE + + + + FORM + myjdbc + + /login.jsp + /login_error.jsp + + + + testRole + + + FacesServlet + FacesServlet + javax.faces.webapp.FacesServlet + 1 + + + FacesServlet + *.faces + + + index.faces + + + ejb/CartBean + Session + ejbjpa.ejb.Cart + + + jdbc/mysqlpool + javax.sql.DataSource + Container + Shareable + + + diff --git a/ch14/sampleapp/target/index.jsp b/ch14/sampleapp/target/index.jsp new file mode 100644 index 0000000..c81f6b5 --- /dev/null +++ b/ch14/sampleapp/target/index.jsp @@ -0,0 +1,53 @@ +<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> +<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> + + + + + +

List of books

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

+ + + \ No newline at end of file diff --git a/ch14/sampleapp/target/jsfapp.war b/ch14/sampleapp/target/jsfapp.war new file mode 100644 index 0000000..f6645b4 Binary files /dev/null and b/ch14/sampleapp/target/jsfapp.war differ diff --git a/ch14/sampleapp/target/login.jsp b/ch14/sampleapp/target/login.jsp new file mode 100644 index 0000000..f6592cd --- /dev/null +++ b/ch14/sampleapp/target/login.jsp @@ -0,0 +1,12 @@ +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> +<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> + + Login Page +

Please login:

+
+

Enter Customer ID:

+

Enter Password:

+ + +
+ diff --git a/ch14/sampleapp/target/login_error.jsp b/ch14/sampleapp/target/login_error.jsp new file mode 100644 index 0000000..6348134 --- /dev/null +++ b/ch14/sampleapp/target/login_error.jsp @@ -0,0 +1,10 @@ +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> + + Login error page + + +

User name or password is wrong.

+

Please enter a valid user name and password. Click here to try again.

+ + + diff --git a/ch14/sampleapp/target/showcart.jsp b/ch14/sampleapp/target/showcart.jsp new file mode 100644 index 0000000..b3932c1 --- /dev/null +++ b/ch14/sampleapp/target/showcart.jsp @@ -0,0 +1,42 @@ +<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> +<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> + + + + + +

Your shopping cart items to buy now

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + +

+ + + + \ No newline at end of file diff --git a/ch14/sampleapp/target/stylesheet.css b/ch14/sampleapp/target/stylesheet.css new file mode 100644 index 0000000..ee758ed --- /dev/null +++ b/ch14/sampleapp/target/stylesheet.css @@ -0,0 +1,21 @@ +.header { +text-align: center; +font-family: arial; +font-style: bold; +color: Black; +background: seashell; +padding: 10px; +} +.evenCol { +text-align: center; +font-family: arial; +font-size: 10pt; +background: Snow; + +} +.oddCol { +text-align: center; +font-family: arial; +font-size: 10pt; +background: whitesmoke; +} diff --git a/ch15/sampleapp/ejbtarget/META-INF/persistence.xml b/ch15/sampleapp/ejbtarget/META-INF/persistence.xml new file mode 100644 index 0000000..ff56965 --- /dev/null +++ b/ch15/sampleapp/ejbtarget/META-INF/persistence.xml @@ -0,0 +1,6 @@ + + + + jdbc/mysqlpool + + diff --git a/ch15/sampleapp/ejbtarget/appejb.jar b/ch15/sampleapp/ejbtarget/appejb.jar new file mode 100644 index 0000000..40adba3 Binary files /dev/null and b/ch15/sampleapp/ejbtarget/appejb.jar differ diff --git a/ch15/sampleapp/ejbtarget/ejbjpa/ejb/Cart.class b/ch15/sampleapp/ejbtarget/ejbjpa/ejb/Cart.class new file mode 100644 index 0000000..439fad7 Binary files /dev/null and b/ch15/sampleapp/ejbtarget/ejbjpa/ejb/Cart.class differ diff --git a/ch15/sampleapp/ejbtarget/ejbjpa/ejb/CartBean.class b/ch15/sampleapp/ejbtarget/ejbjpa/ejb/CartBean.class new file mode 100644 index 0000000..cee0beb Binary files /dev/null and b/ch15/sampleapp/ejbtarget/ejbjpa/ejb/CartBean.class differ diff --git a/ch15/sampleapp/ejbtarget/ejbjpa/ejb/OrderBean.class b/ch15/sampleapp/ejbtarget/ejbjpa/ejb/OrderBean.class new file mode 100644 index 0000000..740847e Binary files /dev/null and b/ch15/sampleapp/ejbtarget/ejbjpa/ejb/OrderBean.class differ diff --git a/ch15/sampleapp/ejbtarget/ejbjpa/ejb/OrderSample.class b/ch15/sampleapp/ejbtarget/ejbjpa/ejb/OrderSample.class new file mode 100644 index 0000000..d2d2de6 Binary files /dev/null and b/ch15/sampleapp/ejbtarget/ejbjpa/ejb/OrderSample.class differ diff --git a/ch15/sampleapp/ejbtarget/ejbjpa/entities/Customer.class b/ch15/sampleapp/ejbtarget/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..855839f Binary files /dev/null and b/ch15/sampleapp/ejbtarget/ejbjpa/entities/Customer.class differ diff --git a/ch15/sampleapp/ejbtarget/ejbjpa/entities/Employee.class b/ch15/sampleapp/ejbtarget/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..380c0cf Binary files /dev/null and b/ch15/sampleapp/ejbtarget/ejbjpa/entities/Employee.class differ diff --git a/ch15/sampleapp/ejbtarget/ejbjpa/entities/Order.class b/ch15/sampleapp/ejbtarget/ejbjpa/entities/Order.class new file mode 100644 index 0000000..7030ba9 Binary files /dev/null and b/ch15/sampleapp/ejbtarget/ejbjpa/entities/Order.class differ diff --git a/ch15/sampleapp/ejbtarget/ejbjpa/entities/ShoppingCart.class b/ch15/sampleapp/ejbtarget/ejbjpa/entities/ShoppingCart.class new file mode 100644 index 0000000..f74950a Binary files /dev/null and b/ch15/sampleapp/ejbtarget/ejbjpa/entities/ShoppingCart.class differ diff --git a/ch15/sampleapp/ejbtarget/ejbjpa/entities/ShoppingCartKey.class b/ch15/sampleapp/ejbtarget/ejbjpa/entities/ShoppingCartKey.class new file mode 100644 index 0000000..c13e03f Binary files /dev/null and b/ch15/sampleapp/ejbtarget/ejbjpa/entities/ShoppingCartKey.class differ diff --git a/ch15/sampleapp/src/ejbjpa/ejb/Cart.java b/ch15/sampleapp/src/ejbjpa/ejb/Cart.java new file mode 100644 index 0000000..babd296 --- /dev/null +++ b/ch15/sampleapp/src/ejbjpa/ejb/Cart.java @@ -0,0 +1,14 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +import java.util.List; +import ejbjpa.entities.*; + +@Remote +public interface Cart { + public void initialize(Integer cust_id); + public void addItem(String item_id, Integer quantity, Double price); + public void removeItem(String item_id); + public List getItems(); + public Integer emptyCart(); + public void clearCartInstance(); +} diff --git a/ch15/sampleapp/src/ejbjpa/ejb/CartBean.java b/ch15/sampleapp/src/ejbjpa/ejb/CartBean.java new file mode 100644 index 0000000..cfed6db --- /dev/null +++ b/ch15/sampleapp/src/ejbjpa/ejb/CartBean.java @@ -0,0 +1,64 @@ +package ejbjpa.ejb; +import java.util.List; +import javax.ejb.Remove; +import javax.ejb.Stateful; +import javax.ejb.EJBException; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.PersistenceContextType; +import ejbjpa.entities.*; +@Stateful +public class CartBean implements Cart { +@PersistenceContext(type=PersistenceContextType.EXTENDED) +EntityManager em; +Integer custId; +List items; +public void initialize(Integer cust_id) { + if (cust_id == null) { + throw new EJBException("Null cust_id provided."); + } else { + custId = cust_id; + } + } + public void addItem(String item_id, Integer quantity, Double price) { + ShoppingCart cart = (ShoppingCart) em.find(ShoppingCart.class, new ShoppingCartKey(custId, item_id)); + if(cart != null) + { + em.remove(cart); + em.flush(); + } + cart = new ShoppingCart(); + cart.setCart_id(custId); + cart.setBook_id(item_id); + cart.setUnits(quantity); + cart.setUnit_price(price); + em.persist(cart); + } + public void removeItem(String item_id) { + ShoppingCart cart = (ShoppingCart) em.find(ShoppingCart.class, new ShoppingCartKey(custId, item_id)); + if(cart == null){ + throw new EJBException("This item is not in cart."); + } else { + em.remove(cart); + } + } +public List getItems() { + items = (List)em.createQuery("SELECT s FROM ShoppingCart s WHERE s.cart_id =:cust_id") + .setParameter("cust_id", custId) + .getResultList(); + em.clear(); + return items; +} +@Remove +public Integer emptyCart() { + Integer num =0; + num = em.createQuery("DELETE FROM ShoppingCart s WHERE s.cart_id =:cust_id") + .setParameter("cust_id", custId) + .executeUpdate(); + return num; +} + + @Remove + public void clearCartInstance() { + } +} diff --git a/ch15/sampleapp/src/ejbjpa/ejb/OrderBean.java b/ch15/sampleapp/src/ejbjpa/ejb/OrderBean.java new file mode 100644 index 0000000..1072ced --- /dev/null +++ b/ch15/sampleapp/src/ejbjpa/ejb/OrderBean.java @@ -0,0 +1,36 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.List; +import ejbjpa.entities.*; +@Stateless +public class OrderBean implements OrderSample { + @PersistenceContext + private EntityManager em; + public void placeOrder(Integer cust_id, + Integer empno) { + try { + Customer cust = (Customer) em.find(Customer.class, cust_id); + Employee emp = (Employee) em.find(Employee.class, empno); + Order order = new Order(); + order.setCustomer(cust); + order.setEmployee(emp); + em.persist(order); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + } + public List getOrdersList() { + List orders = null; + try { + orders = (List)em.createQuery("SELECT o FROM Order o") + .getResultList(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return orders; + } +} diff --git a/ch15/sampleapp/src/ejbjpa/ejb/OrderSample.java b/ch15/sampleapp/src/ejbjpa/ejb/OrderSample.java new file mode 100644 index 0000000..2661b52 --- /dev/null +++ b/ch15/sampleapp/src/ejbjpa/ejb/OrderSample.java @@ -0,0 +1,11 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +import java.util.List; +import ejbjpa.entities.*; + +@Remote +public interface OrderSample { + public void placeOrder(Integer cust_id, Integer empno); +public List getOrdersList() ; + +} diff --git a/ch15/sampleapp/src/ejbjpa/entities/Customer.java b/ch15/sampleapp/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..aa187c0 --- /dev/null +++ b/ch15/sampleapp/src/ejbjpa/entities/Customer.java @@ -0,0 +1,64 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "CUST_NAME", nullable = false) + private String cust_name; + @Column(name = "LOC_ID") + private Integer loc_id; + @Column(name = "EMAIL", nullable = false) + private String email; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToMany(mappedBy="customer", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCust_name() { + return this.cust_name; + } + public void setCust_name(String cust_name) { + this.cust_name = cust_name; + } + public Integer getLoc_id() { + return this.loc_id; + } + public void setLoc_id(Integer loc_id) { + this.loc_id = loc_id; + } + public String getEmail() { + return this.email; + } + public void setEmail(String email) { + this.email = email; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/ch15/sampleapp/src/ejbjpa/entities/Employee.java b/ch15/sampleapp/src/ejbjpa/entities/Employee.java new file mode 100644 index 0000000..9751c22 --- /dev/null +++ b/ch15/sampleapp/src/ejbjpa/entities/Employee.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "EMPLOYEES") +public class Employee implements Serializable { + @Id + @Column(name = "EMPNO") + private Integer empno; + @Column(name = "firstname", nullable = false) + private String firstname; + @Column(name = "lastname", nullable = false) + private String lastname; + @OneToMany(mappedBy="employee", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Employee() { + } + public Integer getEmpno() { + return this.empno; + } + public void setEmpno(Integer empno) { + this.empno = empno; + } + public String getFirstname() { + return this.firstname; + } + public void setFirstname(String firstname) { + this.firstname = firstname; + } + public String getLastname() { + return this.lastname; + } + public void setLastname(String lastname) { + this.lastname = lastname; + } +} diff --git a/ch15/sampleapp/src/ejbjpa/entities/Order.java b/ch15/sampleapp/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..c550cad --- /dev/null +++ b/ch15/sampleapp/src/ejbjpa/entities/Order.java @@ -0,0 +1,65 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import static javax.persistence.TemporalType.DATE; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import java.util.Date; +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + @Id + @Column(name = "PONO") + private Integer pono; + @Column(name = "SHIPPING_DATE", nullable = false) + @Temporal(DATE) + private Date shipping_date; + @Column(name = "DELIVERY_ESTIMATE", nullable = false) + private String delivery_estimate; + @ManyToOne + @JoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ID") + private Customer customer; + @ManyToOne + @JoinColumn( + name="EMPNO", + referencedColumnName="EMPNO") + private Employee employee; + public Order() { + } + public Customer getCustomer() { + return this.customer; + } + public void setCustomer(Customer customer) { + this.customer = customer; + } + public Employee getEmployee() { + return this.employee; + } + public void setEmployee (Employee employee) { + this.employee = employee; + } + public Integer getPono () { + return this.pono; + } + public void setPono (Integer pono) { + this.pono = pono; + } + public Date getShipping_date () { + return this.shipping_date; + } + public void setShipping_date (Date shipping_date) { + this.shipping_date = shipping_date; + } + public String getDelivery_estimate () { + return this.delivery_estimate; + } + public void setDelivery_estimate (String delivery_estimate) { + this.delivery_estimate = delivery_estimate; + } +} diff --git a/ch15/sampleapp/src/ejbjpa/entities/ShoppingCart.java b/ch15/sampleapp/src/ejbjpa/entities/ShoppingCart.java new file mode 100644 index 0000000..7d169f2 --- /dev/null +++ b/ch15/sampleapp/src/ejbjpa/entities/ShoppingCart.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.IdClass; +import javax.persistence.Table; +@Entity +@Table(name = "SHOPPINGCARTS") +@IdClass(value = ejbjpa.entities.ShoppingCartKey.class) +public class ShoppingCart implements Serializable { + @Id + @Column(name = "CART_ID") + private Integer cart_id; + @Id + @Column(name = "BOOK_ID") + private String book_id; + @Column(name = "UNITS", nullable = false) + private Integer units; + @Column(name = "UNIT_PRICE", nullable = false) + private Double unit_price; + public ShoppingCart() { + } + public Integer getCart_id() { + return this.cart_id; + } + public void setCart_id(Integer cart_id) { + this.cart_id = cart_id; + } + public String getBook_id() { + return this.book_id; + } + public void setBook_id(String book_id) { + this.book_id = book_id; + } + public Integer getUnits () { + return this.units; + } + public void setUnits(Integer units) { + this.units = units; + } + public Double getUnit_price() { + return this.unit_price; + } + public void setUnit_price(Double unit_price) { + this.unit_price = unit_price; + } +} diff --git a/ch15/sampleapp/src/ejbjpa/entities/ShoppingCartKey.java b/ch15/sampleapp/src/ejbjpa/entities/ShoppingCartKey.java new file mode 100644 index 0000000..5c29dbe --- /dev/null +++ b/ch15/sampleapp/src/ejbjpa/entities/ShoppingCartKey.java @@ -0,0 +1,37 @@ +package ejbjpa.entities; +import java.io.Serializable; + +public final class ShoppingCartKey implements Serializable { + public Integer cart_id; + public String book_id; + public ShoppingCartKey() {} + public ShoppingCartKey(Integer cart_id, String book_id) { + this.cart_id = cart_id; + this.book_id = book_id; + } + +public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof ShoppingCartKey)) { + return false; + } + ShoppingCartKey other = (ShoppingCartKey) obj; + if (cart_id != null && other.cart_id!= null && this.cart_id.equals(other.cart_id)) { + return (book_id != null && other.book_id!= null && this.book_id.equals(other.book_id)); + } + return false; +} + +public int hashCode() { + if (cart_id!=null && book_id!=null) { + return (cart_id.hashCode() ^ book_id.hashCode()); + } + return 0; +} + +} \ No newline at end of file diff --git a/ch15/sampleapp/src/ejbjpa/jsfbeans/OrderJSFBean.java b/ch15/sampleapp/src/ejbjpa/jsfbeans/OrderJSFBean.java new file mode 100644 index 0000000..52ae351 --- /dev/null +++ b/ch15/sampleapp/src/ejbjpa/jsfbeans/OrderJSFBean.java @@ -0,0 +1,74 @@ +package ejbjpa.jsfbeans; +import javax.ejb.EJB; +import javax.naming.InitialContext; +import javax.faces.context.FacesContext; +import javax.servlet.http.HttpSession; +import java.util.List; +import java.util.Map; +import ejbjpa.ejb.*; +import ejbjpa.entities.*; + +@EJB(name="ejb/CartBean", beanInterface=Cart.class) +public class OrderJSFBean { + private Cart cart; + @EJB + private OrderSample order; + private List cartItems; + private Integer custId; + public OrderJSFBean() { + custId = Integer.parseInt( + FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName()); + try{ + if (cart == null) { + cart = (Cart) (new InitialContext()).lookup("java:comp/env/ejb/CartBean"); + } + cart.initialize(custId); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public Integer getCustId() { + return custId; + } + public List getCartItems() { + cartItems = null; + try { + cartItems = cart.getItems(); + } catch (Exception e) { + e.printStackTrace(); + } + return cartItems; + } + public void addToCart() { + try { + FacesContext cxt = FacesContext.getCurrentInstance(); + Map params = cxt.getExternalContext().getRequestParameterMap(); + String isbn = (String)params.get("isbn"); + String price_str = (String)params.get("price"); + Double price =new Double(price_str); + cart.addItem(isbn, 1, price); + } catch (Exception e) { + e.printStackTrace(); + } + } + public void removeFromCart() { + try { + FacesContext cxt = FacesContext.getCurrentInstance(); + Map params = cxt.getExternalContext().getRequestParameterMap(); + String itemId = (String)params.get("itemId"); + cart.removeItem(itemId); + } catch (Exception e) { + e.printStackTrace(); + } + } + public String ProceedToCheckout() { + try { + order.placeOrder(custId, 1); + } catch (Exception e) { + e.printStackTrace(); + } + return "continue"; + } + +} diff --git a/ch15/sampleapp/src/ejbjpa/jsfbeans/ProductJSFBean.java b/ch15/sampleapp/src/ejbjpa/jsfbeans/ProductJSFBean.java new file mode 100644 index 0000000..d9b5a01 --- /dev/null +++ b/ch15/sampleapp/src/ejbjpa/jsfbeans/ProductJSFBean.java @@ -0,0 +1,38 @@ +package ejbjpa.jsfbeans; +import javax.ejb.EJB; +import javax.naming.InitialContext; +import javax.faces.context.FacesContext; +import javax.servlet.http.HttpSession; +import java.util.List; +import ejbjpa.ejb.*; +import ejbjpa.entities.*; + +import java.sql.*; +import javax.sql.DataSource; + +public class ProductJSFBean { + private Connection connDb; + public void openConnection() throws Exception { + if(connDb != null) + return; + DataSource dataSource = (DataSource) (new InitialContext()).lookup("java:comp/env/jdbc/mysqlpool"); + connDb = dataSource.getConnection(); + } + public ResultSet getAllProducts() throws Exception { + ResultSet rslt = null; + try{ + this.openConnection(); + Statement stmt = connDb.createStatement(); + rslt = stmt.executeQuery("SELECT * FROM books"); + return rslt; + } finally { + // closeConnection(); + } + } + public void closeConnection() throws Exception { + if (connDb != null) { + connDb.close(); + } + } + +} diff --git a/ch15/sampleapp/webtarget/WEB-INF/classes/ejbjpa/jsfbeans/OrderJSFBean.class b/ch15/sampleapp/webtarget/WEB-INF/classes/ejbjpa/jsfbeans/OrderJSFBean.class new file mode 100644 index 0000000..8dd32d5 Binary files /dev/null and b/ch15/sampleapp/webtarget/WEB-INF/classes/ejbjpa/jsfbeans/OrderJSFBean.class differ diff --git a/ch15/sampleapp/webtarget/WEB-INF/classes/ejbjpa/jsfbeans/ProductJSFBean.class b/ch15/sampleapp/webtarget/WEB-INF/classes/ejbjpa/jsfbeans/ProductJSFBean.class new file mode 100644 index 0000000..c4df6e5 Binary files /dev/null and b/ch15/sampleapp/webtarget/WEB-INF/classes/ejbjpa/jsfbeans/ProductJSFBean.class differ diff --git a/ch15/sampleapp/webtarget/WEB-INF/faces-config.xml b/ch15/sampleapp/webtarget/WEB-INF/faces-config.xml new file mode 100644 index 0000000..5f15d63 --- /dev/null +++ b/ch15/sampleapp/webtarget/WEB-INF/faces-config.xml @@ -0,0 +1,38 @@ + + + + + + OrderJSFBean + ejbjpa.jsfbeans.OrderJSFBean + + session + + + product + ejbjpa.jsfbeans.ProductJSFBean + + session + + + + + + By clicking the "Move to cart" button on the index.jsp page you move to showcart.jsp + + showcart + /showcart.jsp + + + + Clicking the "Continue shopping" turns you back to index.jsp showing the list of books available + + continue + /index.jsp + + + + diff --git a/ch15/sampleapp/webtarget/WEB-INF/lib/appejb.jar b/ch15/sampleapp/webtarget/WEB-INF/lib/appejb.jar new file mode 100644 index 0000000..40adba3 Binary files /dev/null and b/ch15/sampleapp/webtarget/WEB-INF/lib/appejb.jar differ diff --git a/ch15/sampleapp/webtarget/WEB-INF/sun-web.xml b/ch15/sampleapp/webtarget/WEB-INF/sun-web.xml new file mode 100644 index 0000000..34edc32 --- /dev/null +++ b/ch15/sampleapp/webtarget/WEB-INF/sun-web.xml @@ -0,0 +1,9 @@ + + + + /sampleapp + + testRole + testRole + + diff --git a/ch15/sampleapp/webtarget/WEB-INF/web.xml b/ch15/sampleapp/webtarget/WEB-INF/web.xml new file mode 100644 index 0000000..7bd19f2 --- /dev/null +++ b/ch15/sampleapp/webtarget/WEB-INF/web.xml @@ -0,0 +1,59 @@ + + + + + + testing web app + /* + POST + GET + + + testRole + + + NONE + + + + FORM + myjdbc + + /login.jsp + /login_error.jsp + + + + testRole + + + FacesServlet + FacesServlet + javax.faces.webapp.FacesServlet + 1 + + + + FacesServlet + *.faces + + + index.faces + + + ejb/CartBean + Session + ejbjpa.ejb.Cart + + + jdbc/mysqlpool + javax.sql.DataSource + Container + Shareable + + + + diff --git a/ch15/sampleapp/webtarget/index.jsp b/ch15/sampleapp/webtarget/index.jsp new file mode 100644 index 0000000..d33ba83 --- /dev/null +++ b/ch15/sampleapp/webtarget/index.jsp @@ -0,0 +1,53 @@ +<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> +<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> + + + + + +

List of books

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

+ + + \ No newline at end of file diff --git a/ch15/sampleapp/webtarget/jsfapp.war b/ch15/sampleapp/webtarget/jsfapp.war new file mode 100644 index 0000000..9f7971f Binary files /dev/null and b/ch15/sampleapp/webtarget/jsfapp.war differ diff --git a/ch15/sampleapp/webtarget/login.jsp b/ch15/sampleapp/webtarget/login.jsp new file mode 100644 index 0000000..29cb5e4 --- /dev/null +++ b/ch15/sampleapp/webtarget/login.jsp @@ -0,0 +1,13 @@ +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> +<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> + +Login Page +

Please login:

+
+

Enter Customer ID:

+

Enter Password:

+ + +
+ + diff --git a/ch15/sampleapp/webtarget/login_error.jsp b/ch15/sampleapp/webtarget/login_error.jsp new file mode 100644 index 0000000..4dbf663 --- /dev/null +++ b/ch15/sampleapp/webtarget/login_error.jsp @@ -0,0 +1,10 @@ +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> + +Login error page + + +

Invalid user name or password.

+

Please enter a user name or password that is authorized to access this application. For this application, this means a user has been created in the file realm and has been assigned to the group of user. Click here to Try Again + + + diff --git a/ch15/sampleapp/webtarget/showcart.jsp b/ch15/sampleapp/webtarget/showcart.jsp new file mode 100644 index 0000000..b3932c1 --- /dev/null +++ b/ch15/sampleapp/webtarget/showcart.jsp @@ -0,0 +1,42 @@ +<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> +<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> + + + + + +

Your shopping cart items to buy now

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + +

+ + + + \ No newline at end of file diff --git a/ch15/sampleapp/webtarget/stylesheet.css b/ch15/sampleapp/webtarget/stylesheet.css new file mode 100644 index 0000000..ee758ed --- /dev/null +++ b/ch15/sampleapp/webtarget/stylesheet.css @@ -0,0 +1,21 @@ +.header { +text-align: center; +font-family: arial; +font-style: bold; +color: Black; +background: seashell; +padding: 10px; +} +.evenCol { +text-align: center; +font-family: arial; +font-size: 10pt; +background: Snow; + +} +.oddCol { +text-align: center; +font-family: arial; +font-size: 10pt; +background: whitesmoke; +} diff --git a/ch2/EJBJPAProject/AppClient/dist/appclient.jar b/ch2/EJBJPAProject/AppClient/dist/appclient.jar new file mode 100644 index 0000000..3231f81 Binary files /dev/null and b/ch2/EJBJPAProject/AppClient/dist/appclient.jar differ diff --git a/ch2/EJBJPAProject/AppClient/src/ejbjpa/client/EmployeeSessionClient.java b/ch2/EJBJPAProject/AppClient/src/ejbjpa/client/EmployeeSessionClient.java new file mode 100644 index 0000000..6289764 --- /dev/null +++ b/ch2/EJBJPAProject/AppClient/src/ejbjpa/client/EmployeeSessionClient.java @@ -0,0 +1,12 @@ +package ejbjpa.client; + +import javax.ejb.EJB; +import ejbjpa.ejb.EmployeeSession; +public class EmployeeSessionClient { + @EJB + private static EmployeeSession employeeSession; + public static void main (String[] args) + { + System.out.println(employeeSession.getEmplastname(10)); + } +} \ No newline at end of file diff --git a/ch2/EJBJPAProject/AppClient/target/META-INF/MANIFEST.MF b/ch2/EJBJPAProject/AppClient/target/META-INF/MANIFEST.MF new file mode 100644 index 0000000..1ca0c90 --- /dev/null +++ b/ch2/EJBJPAProject/AppClient/target/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: ejbjpa.client.EmployeeSessionClient + diff --git a/ch2/EJBJPAProject/AppClient/target/ejbjpa/client/EmployeeSessionClient.class b/ch2/EJBJPAProject/AppClient/target/ejbjpa/client/EmployeeSessionClient.class new file mode 100644 index 0000000..c2d633b Binary files /dev/null and b/ch2/EJBJPAProject/AppClient/target/ejbjpa/client/EmployeeSessionClient.class differ diff --git a/ch2/EJBJPAProject/EJBJPA/dist/ejbjpa.jar b/ch2/EJBJPAProject/EJBJPA/dist/ejbjpa.jar new file mode 100644 index 0000000..177ad5a Binary files /dev/null and b/ch2/EJBJPAProject/EJBJPA/dist/ejbjpa.jar differ diff --git a/ch2/EJBJPAProject/EJBJPA/src/ejbjpa/ejb/EmployeeSession.java b/ch2/EJBJPAProject/EJBJPA/src/ejbjpa/ejb/EmployeeSession.java new file mode 100644 index 0000000..74842a9 --- /dev/null +++ b/ch2/EJBJPAProject/EJBJPA/src/ejbjpa/ejb/EmployeeSession.java @@ -0,0 +1,6 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +@Remote +public interface EmployeeSession { + public String getEmplastname(Integer empno); +} diff --git a/ch2/EJBJPAProject/EJBJPA/src/ejbjpa/ejb/EmployeeSessionBean.java b/ch2/EJBJPAProject/EJBJPA/src/ejbjpa/ejb/EmployeeSessionBean.java new file mode 100644 index 0000000..5124761 --- /dev/null +++ b/ch2/EJBJPAProject/EJBJPA/src/ejbjpa/ejb/EmployeeSessionBean.java @@ -0,0 +1,25 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; +import ejbjpa.entities.*; + +@Stateless +public class EmployeeSessionBean implements EmployeeSession { + @PersistenceUnit(unitName = "ejbjpa-pu") + private EntityManagerFactory emf; + public String getEmplastname(Integer empno) { + String fullname; + try { + EntityManager em = emf.createEntityManager(); + Employee emp = em.find(Employee.class, empno); + fullname = emp.getFirstname()+" "+emp.getLastname(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return fullname; + } +} \ No newline at end of file diff --git a/ch2/EJBJPAProject/EJBJPA/src/ejbjpa/entities/Employee.java b/ch2/EJBJPAProject/EJBJPA/src/ejbjpa/entities/Employee.java new file mode 100644 index 0000000..26690f8 --- /dev/null +++ b/ch2/EJBJPAProject/EJBJPA/src/ejbjpa/entities/Employee.java @@ -0,0 +1,37 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +@Entity +@Table(name = "EMPLOYEES") +public class Employee implements Serializable { + @Id + @Column(name = "EMPNO") + private Integer empno; + @Column(name = "FIRSTNAME", nullable = false) + private String firstname; + @Column(name = "LASTNAME", nullable = false) + private String lastname; + public Employee() { + } + public Integer getEmpno() { + return this.empno; + } + public void setEmpno(Integer empno) { + this.empno = empno; + } + public String getFirstname() { + return this.firstname; + } + public void setFirstname(String firstname) { + this.firstname = firstname; + } + public String getLastname() { + return this.lastname; + } + public void setLastname(String lastname) { + this.lastname = lastname; + } +} diff --git a/ch2/EJBJPAProject/EJBJPA/target/META-INF/persistence.xml b/ch2/EJBJPAProject/EJBJPA/target/META-INF/persistence.xml new file mode 100644 index 0000000..2786fc8 --- /dev/null +++ b/ch2/EJBJPAProject/EJBJPA/target/META-INF/persistence.xml @@ -0,0 +1,7 @@ + + + + jdbc/myderbypool +ejbjpa.entities.Employee + + diff --git a/ch2/EJBJPAProject/EJBJPA/target/ejbjpa/ejb/EmployeeSession.class b/ch2/EJBJPAProject/EJBJPA/target/ejbjpa/ejb/EmployeeSession.class new file mode 100644 index 0000000..368a5c9 Binary files /dev/null and b/ch2/EJBJPAProject/EJBJPA/target/ejbjpa/ejb/EmployeeSession.class differ diff --git a/ch2/EJBJPAProject/EJBJPA/target/ejbjpa/ejb/EmployeeSessionBean.class b/ch2/EJBJPAProject/EJBJPA/target/ejbjpa/ejb/EmployeeSessionBean.class new file mode 100644 index 0000000..c8adcc9 Binary files /dev/null and b/ch2/EJBJPAProject/EJBJPA/target/ejbjpa/ejb/EmployeeSessionBean.class differ diff --git a/ch2/EJBJPAProject/EJBJPA/target/ejbjpa/entities/Employee.class b/ch2/EJBJPAProject/EJBJPA/target/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..973c7d4 Binary files /dev/null and b/ch2/EJBJPAProject/EJBJPA/target/ejbjpa/entities/Employee.class differ diff --git a/ch2/EJBJPAProject/dist/ejbjpaapp.jar b/ch2/EJBJPAProject/dist/ejbjpaapp.jar new file mode 100644 index 0000000..1e08a51 Binary files /dev/null and b/ch2/EJBJPAProject/dist/ejbjpaapp.jar differ diff --git a/ch2/EJBJPAProject/target/META-INF/application.xml b/ch2/EJBJPAProject/target/META-INF/application.xml new file mode 100644 index 0000000..b1784cd --- /dev/null +++ b/ch2/EJBJPAProject/target/META-INF/application.xml @@ -0,0 +1,10 @@ + + + ejbjpa + + ejbjpa.jar + + + appclient.jar + + diff --git a/ch2/EJBJPAProject/target/appclient.jar b/ch2/EJBJPAProject/target/appclient.jar new file mode 100644 index 0000000..3231f81 Binary files /dev/null and b/ch2/EJBJPAProject/target/appclient.jar differ diff --git a/ch2/EJBJPAProject/target/ejbjpa.jar b/ch2/EJBJPAProject/target/ejbjpa.jar new file mode 100644 index 0000000..177ad5a Binary files /dev/null and b/ch2/EJBJPAProject/target/ejbjpa.jar differ diff --git a/ch2/HelloWorldProject/HelloWorldClient/dist/helloworldclient.jar b/ch2/HelloWorldProject/HelloWorldClient/dist/helloworldclient.jar new file mode 100644 index 0000000..686c734 Binary files /dev/null and b/ch2/HelloWorldProject/HelloWorldClient/dist/helloworldclient.jar differ diff --git a/ch2/HelloWorldProject/HelloWorldClient/src/application-client.xml b/ch2/HelloWorldProject/HelloWorldClient/src/application-client.xml new file mode 100644 index 0000000..89169e8 --- /dev/null +++ b/ch2/HelloWorldProject/HelloWorldClient/src/application-client.xml @@ -0,0 +1,8 @@ + + + helloworldejbclient + + helloworld.client.HelloWorldClient/helloWorld + helloworld.ejb.HelloWorld + + diff --git a/ch2/HelloWorldProject/HelloWorldClient/src/helloworld/client/HelloWorldClient.java b/ch2/HelloWorldProject/HelloWorldClient/src/helloworld/client/HelloWorldClient.java new file mode 100644 index 0000000..c308cbb --- /dev/null +++ b/ch2/HelloWorldProject/HelloWorldClient/src/helloworld/client/HelloWorldClient.java @@ -0,0 +1,12 @@ +package helloworld.client; + +import javax.ejb.EJB; +import helloworld.ejb.HelloWorld; +public class HelloWorldClient { + @EJB + private static HelloWorld helloWorld; + public static void main (String[] args) + { + System.out.println(helloWorld.outputHelloWorld()); + } +} \ No newline at end of file diff --git a/ch2/HelloWorldProject/HelloWorldClient/target/META-INF/MANIFEST.MF b/ch2/HelloWorldProject/HelloWorldClient/target/META-INF/MANIFEST.MF new file mode 100644 index 0000000..c834a8e --- /dev/null +++ b/ch2/HelloWorldProject/HelloWorldClient/target/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: helloworld.client.HelloWorldClient + diff --git a/ch2/HelloWorldProject/HelloWorldClient/target/helloworld/client/HelloWorldClient.class b/ch2/HelloWorldProject/HelloWorldClient/target/helloworld/client/HelloWorldClient.class new file mode 100644 index 0000000..044cf89 Binary files /dev/null and b/ch2/HelloWorldProject/HelloWorldClient/target/helloworld/client/HelloWorldClient.class differ diff --git a/ch2/HelloWorldProject/HelloWorldEJB/dist/helloworldejb.jar b/ch2/HelloWorldProject/HelloWorldEJB/dist/helloworldejb.jar new file mode 100644 index 0000000..07e6877 Binary files /dev/null and b/ch2/HelloWorldProject/HelloWorldEJB/dist/helloworldejb.jar differ diff --git a/ch2/HelloWorldProject/HelloWorldEJB/src/helloworld/ejb/HelloWorld.java b/ch2/HelloWorldProject/HelloWorldEJB/src/helloworld/ejb/HelloWorld.java new file mode 100644 index 0000000..6a7b4b7 --- /dev/null +++ b/ch2/HelloWorldProject/HelloWorldEJB/src/helloworld/ejb/HelloWorld.java @@ -0,0 +1,6 @@ +package helloworld.ejb; +import javax.ejb.Remote; +@Remote +public interface HelloWorld { + public String outputHelloWorld(); +} diff --git a/ch2/HelloWorldProject/HelloWorldEJB/src/helloworld/ejb/HelloWorldBean.java b/ch2/HelloWorldProject/HelloWorldEJB/src/helloworld/ejb/HelloWorldBean.java new file mode 100644 index 0000000..b67304f --- /dev/null +++ b/ch2/HelloWorldProject/HelloWorldEJB/src/helloworld/ejb/HelloWorldBean.java @@ -0,0 +1,8 @@ +package helloworld.ejb; +import javax.ejb.Stateless; +@Stateless +public class HelloWorldBean implements helloworld.ejb.HelloWorld { + public String outputHelloWorld() { + return "Hello World!"; + } +} \ No newline at end of file diff --git a/ch2/HelloWorldProject/HelloWorldEJB/target/helloworld/ejb/HelloWorld.class b/ch2/HelloWorldProject/HelloWorldEJB/target/helloworld/ejb/HelloWorld.class new file mode 100644 index 0000000..2c858d5 Binary files /dev/null and b/ch2/HelloWorldProject/HelloWorldEJB/target/helloworld/ejb/HelloWorld.class differ diff --git a/ch2/HelloWorldProject/HelloWorldEJB/target/helloworld/ejb/HelloWorldBean.class b/ch2/HelloWorldProject/HelloWorldEJB/target/helloworld/ejb/HelloWorldBean.class new file mode 100644 index 0000000..0d21a33 Binary files /dev/null and b/ch2/HelloWorldProject/HelloWorldEJB/target/helloworld/ejb/HelloWorldBean.class differ diff --git a/ch2/HelloWorldProject/dist/helloworldapp.jar b/ch2/HelloWorldProject/dist/helloworldapp.jar new file mode 100644 index 0000000..60149db Binary files /dev/null and b/ch2/HelloWorldProject/dist/helloworldapp.jar differ diff --git a/ch2/HelloWorldProject/target/META-INF/application.xml b/ch2/HelloWorldProject/target/META-INF/application.xml new file mode 100644 index 0000000..dbb6983 --- /dev/null +++ b/ch2/HelloWorldProject/target/META-INF/application.xml @@ -0,0 +1,10 @@ + + + helloworldejb + + helloworldejb.jar + + + helloworldclient.jar + + diff --git a/ch2/HelloWorldProject/target/helloworldclient.jar b/ch2/HelloWorldProject/target/helloworldclient.jar new file mode 100644 index 0000000..686c734 Binary files /dev/null and b/ch2/HelloWorldProject/target/helloworldclient.jar differ diff --git a/ch2/HelloWorldProject/target/helloworldejb.jar b/ch2/HelloWorldProject/target/helloworldejb.jar new file mode 100644 index 0000000..07e6877 Binary files /dev/null and b/ch2/HelloWorldProject/target/helloworldejb.jar differ diff --git a/ch4/DeploymentDescriptors/AppClient/dist/appclient-order.jar b/ch4/DeploymentDescriptors/AppClient/dist/appclient-order.jar new file mode 100644 index 0000000..29e5f63 Binary files /dev/null and b/ch4/DeploymentDescriptors/AppClient/dist/appclient-order.jar differ diff --git a/ch4/DeploymentDescriptors/AppClient/src/ejbjpa/client/OrderSessionClient.java b/ch4/DeploymentDescriptors/AppClient/src/ejbjpa/client/OrderSessionClient.java new file mode 100644 index 0000000..ae87b88 --- /dev/null +++ b/ch4/DeploymentDescriptors/AppClient/src/ejbjpa/client/OrderSessionClient.java @@ -0,0 +1,15 @@ +package ejbjpa.client; +import javax.ejb.EJB; +import ejbjpa.ejb.OrderSession; +public class OrderSessionClient { + @EJB + private static OrderSession orderSession; + public static void main (String[] args) + { + Integer pono = Integer.parseInt(args[0]);//= 7; + Integer cust_id = Integer.parseInt(args[1]); // = 1; + Integer units = Integer.parseInt(args[2]); // = 1; + String book_id = args[3]; // = "1430209631"; + orderSession.placeOrder(pono, cust_id, units, book_id); + } +} \ No newline at end of file diff --git a/ch4/DeploymentDescriptors/AppClient/target/META-INF/MANIFEST.MF b/ch4/DeploymentDescriptors/AppClient/target/META-INF/MANIFEST.MF new file mode 100644 index 0000000..245bfc1 --- /dev/null +++ b/ch4/DeploymentDescriptors/AppClient/target/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: ejbjpa.client.OrderSessionClient + diff --git a/ch4/DeploymentDescriptors/AppClient/target/ejbjpa/client/OrderSessionClient.class b/ch4/DeploymentDescriptors/AppClient/target/ejbjpa/client/OrderSessionClient.class new file mode 100644 index 0000000..568a70b Binary files /dev/null and b/ch4/DeploymentDescriptors/AppClient/target/ejbjpa/client/OrderSessionClient.class differ diff --git a/ch4/DeploymentDescriptors/PlaceOrder/dist/order.jar b/ch4/DeploymentDescriptors/PlaceOrder/dist/order.jar new file mode 100644 index 0000000..c78047a Binary files /dev/null and b/ch4/DeploymentDescriptors/PlaceOrder/dist/order.jar differ diff --git a/ch4/DeploymentDescriptors/PlaceOrder/src/ejbjpa/ejb/OrderSession.java b/ch4/DeploymentDescriptors/PlaceOrder/src/ejbjpa/ejb/OrderSession.java new file mode 100644 index 0000000..ff2c5c6 --- /dev/null +++ b/ch4/DeploymentDescriptors/PlaceOrder/src/ejbjpa/ejb/OrderSession.java @@ -0,0 +1,9 @@ +package ejbjpa.ejb; +//import javax.ejb.Remote; +//@Remote +public interface OrderSession { + public void placeOrder(Integer pono, + Integer cust_id, + Integer units, + String book_id); +} \ No newline at end of file diff --git a/ch4/DeploymentDescriptors/PlaceOrder/src/ejbjpa/ejb/OrderSessionBean.java b/ch4/DeploymentDescriptors/PlaceOrder/src/ejbjpa/ejb/OrderSessionBean.java new file mode 100644 index 0000000..a4e79e3 --- /dev/null +++ b/ch4/DeploymentDescriptors/PlaceOrder/src/ejbjpa/ejb/OrderSessionBean.java @@ -0,0 +1,33 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +//import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +//import javax.persistence.PersistenceUnit; +import ejbjpa.entities.*; + +//@Stateless +public class OrderSessionBean implements OrderSession { +// @PersistenceUnit(unitName = "order-pu") + private EntityManagerFactory emf; + public void placeOrder(Integer pono, + Integer cust_id, + Integer units, + String book_id) + { + try { + EntityManager em = emf.createEntityManager(); + Book book = (Book) em.find(Book.class, book_id); + Order order = new Order(); + order.setPono(pono); + order.setCust_id(cust_id); + order.setUnits(units); + book.setQuantity(book.getQuantity()-units); + order.setBook(book); + em.persist(order); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/ch4/DeploymentDescriptors/PlaceOrder/src/ejbjpa/entities/Book.java b/ch4/DeploymentDescriptors/PlaceOrder/src/ejbjpa/entities/Book.java new file mode 100644 index 0000000..9dcb6a9 --- /dev/null +++ b/ch4/DeploymentDescriptors/PlaceOrder/src/ejbjpa/entities/Book.java @@ -0,0 +1,67 @@ +package ejbjpa.entities; +import java.util.List; +import javax.persistence.CascadeType; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.OneToMany; +@Entity +@Table(name = "BOOKS") +public class Book implements Serializable { + @Id + @Column(name = "ISBN") + private String isbn; + @Column(name = "TITLE", nullable = false) + private String title; + @Column(name = "AUTHOR", nullable = false) + private String author; + @Column(name = "PRICE", nullable = false) + private Double price; + @Column(name = "QUANTITY", nullable = false) + private Integer quantity; + + @OneToMany(mappedBy="book", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + + public Book() { + } + public String getIsbn() { + return this.isbn; + } + public void setIsbn(String isbn) { + this.isbn = isbn; + } + public String getTitle() { + return this.title; + } + public void setTitle(String title) { + this.title = title; + } + public String getAuthor() { + return this.author; + } + public void setAuthor(String author) { + this.author = author; + } + + public Double getPrice() { + return this.price; + } + public void setPrice(Double price) { + this.price = price; + } + public Integer getQuantity() { + return this.quantity; + } + public void setQuantity(Integer quantity) { + this.quantity = quantity; + } +} diff --git a/ch4/DeploymentDescriptors/PlaceOrder/src/ejbjpa/entities/Order.java b/ch4/DeploymentDescriptors/PlaceOrder/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..9a3eace --- /dev/null +++ b/ch4/DeploymentDescriptors/PlaceOrder/src/ejbjpa/entities/Order.java @@ -0,0 +1,53 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; + +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + @Id + @Column(name = "PONO") + private Integer pono; + @Column(name = "CUST_ID", nullable = false) + private Integer cust_id; + @Column(name = "UNITS", nullable = false) + private Integer units; + @ManyToOne + @JoinColumn( + name="BOOK_ID", + referencedColumnName="ISBN") + private Book book; + + public Order() { + } + public Integer getPono() { + return this.pono; + } + public void setPono(Integer pono) { + this.pono = pono; + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public Integer getUnits() { + return this.units; + } + public void setUnits(Integer units) { + this.units = units; + } + public Book getBook() { + return this.book; + } + public void setBook(Book book) { + this.book = book; + } + +} diff --git a/ch4/DeploymentDescriptors/PlaceOrder/target/META-INF/ejb-jar.xml b/ch4/DeploymentDescriptors/PlaceOrder/target/META-INF/ejb-jar.xml new file mode 100644 index 0000000..7c98a9a --- /dev/null +++ b/ch4/DeploymentDescriptors/PlaceOrder/target/META-INF/ejb-jar.xml @@ -0,0 +1,24 @@ + + + + + OrderSessionBean + OrderSessionBean + ejbjpa.ejb.OrderSession + ejbjpa.ejb.OrderSessionBean + Stateless + Container + + ejbjpa.ejb.OrderSessionBean/emf + order-pu + + ejbjpa.ejb.OrderSessionBean + emf + + + + + + + + \ No newline at end of file diff --git a/ch4/DeploymentDescriptors/PlaceOrder/target/META-INF/persistence.xml b/ch4/DeploymentDescriptors/PlaceOrder/target/META-INF/persistence.xml new file mode 100644 index 0000000..a9c2a51 --- /dev/null +++ b/ch4/DeploymentDescriptors/PlaceOrder/target/META-INF/persistence.xml @@ -0,0 +1,12 @@ + + + + jdbc/MySQL +order.jar +ejbjpa.entities.Order +ejbjpa.entities.Book + + + diff --git a/ch4/DeploymentDescriptors/PlaceOrder/target/ejbjpa/ejb/OrderSession.class b/ch4/DeploymentDescriptors/PlaceOrder/target/ejbjpa/ejb/OrderSession.class new file mode 100644 index 0000000..8227348 Binary files /dev/null and b/ch4/DeploymentDescriptors/PlaceOrder/target/ejbjpa/ejb/OrderSession.class differ diff --git a/ch4/DeploymentDescriptors/PlaceOrder/target/ejbjpa/ejb/OrderSessionBean.class b/ch4/DeploymentDescriptors/PlaceOrder/target/ejbjpa/ejb/OrderSessionBean.class new file mode 100644 index 0000000..a76f48b Binary files /dev/null and b/ch4/DeploymentDescriptors/PlaceOrder/target/ejbjpa/ejb/OrderSessionBean.class differ diff --git a/ch4/DeploymentDescriptors/PlaceOrder/target/ejbjpa/entities/Book.class b/ch4/DeploymentDescriptors/PlaceOrder/target/ejbjpa/entities/Book.class new file mode 100644 index 0000000..9ef41e7 Binary files /dev/null and b/ch4/DeploymentDescriptors/PlaceOrder/target/ejbjpa/entities/Book.class differ diff --git a/ch4/DeploymentDescriptors/PlaceOrder/target/ejbjpa/entities/Order.class b/ch4/DeploymentDescriptors/PlaceOrder/target/ejbjpa/entities/Order.class new file mode 100644 index 0000000..c7d5b40 Binary files /dev/null and b/ch4/DeploymentDescriptors/PlaceOrder/target/ejbjpa/entities/Order.class differ diff --git a/ch4/DeploymentDescriptors/dist/deployment-descriptors.jar b/ch4/DeploymentDescriptors/dist/deployment-descriptors.jar new file mode 100644 index 0000000..5ba299c Binary files /dev/null and b/ch4/DeploymentDescriptors/dist/deployment-descriptors.jar differ diff --git a/ch4/DeploymentDescriptors/target/META-INF/application.xml b/ch4/DeploymentDescriptors/target/META-INF/application.xml new file mode 100644 index 0000000..c86078d --- /dev/null +++ b/ch4/DeploymentDescriptors/target/META-INF/application.xml @@ -0,0 +1,10 @@ + + + deployment-descriptors + + order.jar + + + appclient-order.jar + + \ No newline at end of file diff --git a/ch4/DeploymentDescriptors/target/appclient-order.jar b/ch4/DeploymentDescriptors/target/appclient-order.jar new file mode 100644 index 0000000..29e5f63 Binary files /dev/null and b/ch4/DeploymentDescriptors/target/appclient-order.jar differ diff --git a/ch4/DeploymentDescriptors/target/order.jar b/ch4/DeploymentDescriptors/target/order.jar new file mode 100644 index 0000000..c78047a Binary files /dev/null and b/ch4/DeploymentDescriptors/target/order.jar differ diff --git a/ch4/JPAPlanningBidirectional/AppClient/dist/appclient-bidirectional.jar b/ch4/JPAPlanningBidirectional/AppClient/dist/appclient-bidirectional.jar new file mode 100644 index 0000000..0939e0d Binary files /dev/null and b/ch4/JPAPlanningBidirectional/AppClient/dist/appclient-bidirectional.jar differ diff --git a/ch4/JPAPlanningBidirectional/AppClient/src/ejbjpa/client/CustomerSessionClient.java b/ch4/JPAPlanningBidirectional/AppClient/src/ejbjpa/client/CustomerSessionClient.java new file mode 100644 index 0000000..e0af385 --- /dev/null +++ b/ch4/JPAPlanningBidirectional/AppClient/src/ejbjpa/client/CustomerSessionClient.java @@ -0,0 +1,11 @@ +package ejbjpa.client; +import javax.ejb.EJB; +import ejbjpa.ejb.CustomerSession; +public class CustomerSessionClient { + @EJB + private static CustomerSession customerSession; + public static void main (String[] args) + { + System.out.println("Details of the customer whose billing address id=1 is: "+customerSession.getCustomerDetails(1)); + } +} \ No newline at end of file diff --git a/ch4/JPAPlanningBidirectional/AppClient/target/META-INF/MANIFEST.MF b/ch4/JPAPlanningBidirectional/AppClient/target/META-INF/MANIFEST.MF new file mode 100644 index 0000000..e455a43 --- /dev/null +++ b/ch4/JPAPlanningBidirectional/AppClient/target/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: ejbjpa.client.CustomerSessionClient + diff --git a/ch4/JPAPlanningBidirectional/AppClient/target/ejbjpa/client/CustomerSessionClient.class b/ch4/JPAPlanningBidirectional/AppClient/target/ejbjpa/client/CustomerSessionClient.class new file mode 100644 index 0000000..59048bd Binary files /dev/null and b/ch4/JPAPlanningBidirectional/AppClient/target/ejbjpa/client/CustomerSessionClient.class differ diff --git a/ch4/JPAPlanningBidirectional/JPAPlanning/dist/jpaplanning-bidirectional.jar b/ch4/JPAPlanningBidirectional/JPAPlanning/dist/jpaplanning-bidirectional.jar new file mode 100644 index 0000000..c776006 Binary files /dev/null and b/ch4/JPAPlanningBidirectional/JPAPlanning/dist/jpaplanning-bidirectional.jar differ diff --git a/ch4/JPAPlanningBidirectional/JPAPlanning/src/ejbjpa/ejb/CustomerSession.java b/ch4/JPAPlanningBidirectional/JPAPlanning/src/ejbjpa/ejb/CustomerSession.java new file mode 100644 index 0000000..e26769e --- /dev/null +++ b/ch4/JPAPlanningBidirectional/JPAPlanning/src/ejbjpa/ejb/CustomerSession.java @@ -0,0 +1,6 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +@Remote +public interface CustomerSession { + public String getCustomerDetails(Integer cust_address_id); +} diff --git a/ch4/JPAPlanningBidirectional/JPAPlanning/src/ejbjpa/ejb/CustomerSessionBean.java b/ch4/JPAPlanningBidirectional/JPAPlanning/src/ejbjpa/ejb/CustomerSessionBean.java new file mode 100644 index 0000000..d72c65b --- /dev/null +++ b/ch4/JPAPlanningBidirectional/JPAPlanning/src/ejbjpa/ejb/CustomerSessionBean.java @@ -0,0 +1,28 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; +import ejbjpa.entities.*; + +@Stateless +public class CustomerSessionBean implements CustomerSession { + @PersistenceUnit(unitName = "ejbjpa-relation-pu") + private EntityManagerFactory emf; + public String getCustomerDetails(Integer cust_address_id) { + String cust_details; + try { + EntityManager em = emf.createEntityManager(); + Address addr = em.find(Address.class, cust_address_id); + cust_details = addr.getCustomer().getCompany_name()+", "+ + addr.getCustomer().getPhone()+", "+ + "address id is "+" "+ + addr.getCustomer().getAddress().getCust_address_id(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return cust_details; + } +} \ No newline at end of file diff --git a/ch4/JPAPlanningBidirectional/JPAPlanning/src/ejbjpa/entities/Address.java b/ch4/JPAPlanningBidirectional/JPAPlanning/src/ejbjpa/entities/Address.java new file mode 100644 index 0000000..e0b0c1a --- /dev/null +++ b/ch4/JPAPlanningBidirectional/JPAPlanning/src/ejbjpa/entities/Address.java @@ -0,0 +1,67 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.OneToOne; + +@Entity +@Table(name = "BILLING_ADDRESSES") +public class Address implements Serializable { + @Id + @Column(name = "CUST_ADDRESS_ID") + private Integer cust_address_id; + @Column(name = "STREET", nullable = false) + private String street; + @Column(name = "CITY", nullable = false) + private String city; + @Column(name = "STATE", nullable = false) + private String state; + @Column(name = "ZIPCODE", nullable = false) + private String zipcode; + @OneToOne(mappedBy="address") + + private Customer customer; + + public Address() { + } + public Customer getCustomer () { + return this.customer; + } + public void setCustomer(Customer customer) { + this.customer= customer; + } + + public Integer getCust_address_id() { + return this.cust_address_id; + } + public void setCust_address_id(Integer cust_address_id) { + this.cust_address_id = cust_address_id; + } + public String getStreet() { + return this.street; + } + public void setStreet(String street) { + this.street = street; + } + public String getCity() { + return this.city; + } + public void setCity(String city) { + this.city = city; + } + public String getState() { + return this.state; + } + public void setState(String state) { + this.state = state; + } + public String getZipcode() { + return this.zipcode; + } + public void setZipcode(String zipcode) { + this.zipcode = zipcode; + } + +} diff --git a/ch4/JPAPlanningBidirectional/JPAPlanning/src/ejbjpa/entities/Customer.java b/ch4/JPAPlanningBidirectional/JPAPlanning/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..7bf89c1 --- /dev/null +++ b/ch4/JPAPlanningBidirectional/JPAPlanning/src/ejbjpa/entities/Customer.java @@ -0,0 +1,50 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.OneToOne; +import javax.persistence.PrimaryKeyJoinColumn; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "COMPANY_NAME", nullable = false) + private String company_name; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToOne + @PrimaryKeyJoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ADDRESS_ID") + private Address address; + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCompany_name() { + return this.company_name; + } + public void setCompany_name(String company_name) { + this.company_name = company_name; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } + public Address getAddress() { + return this.address; + } + public void setAddress(Address address) { + this.address = address; + } +} diff --git a/ch4/JPAPlanningBidirectional/JPAPlanning/target/META-INF/persistence.xml b/ch4/JPAPlanningBidirectional/JPAPlanning/target/META-INF/persistence.xml new file mode 100644 index 0000000..b107ced --- /dev/null +++ b/ch4/JPAPlanningBidirectional/JPAPlanning/target/META-INF/persistence.xml @@ -0,0 +1,12 @@ + + + + jdbc/MySQL +jpaplanning-bidirectional.jar +ejbjpa.entities.Customer +ejbjpa.entities.Address + + + diff --git a/ch4/JPAPlanningBidirectional/JPAPlanning/target/ejbjpa/ejb/CustomerSession.class b/ch4/JPAPlanningBidirectional/JPAPlanning/target/ejbjpa/ejb/CustomerSession.class new file mode 100644 index 0000000..1262df7 Binary files /dev/null and b/ch4/JPAPlanningBidirectional/JPAPlanning/target/ejbjpa/ejb/CustomerSession.class differ diff --git a/ch4/JPAPlanningBidirectional/JPAPlanning/target/ejbjpa/ejb/CustomerSessionBean.class b/ch4/JPAPlanningBidirectional/JPAPlanning/target/ejbjpa/ejb/CustomerSessionBean.class new file mode 100644 index 0000000..81e3e0f Binary files /dev/null and b/ch4/JPAPlanningBidirectional/JPAPlanning/target/ejbjpa/ejb/CustomerSessionBean.class differ diff --git a/ch4/JPAPlanningBidirectional/JPAPlanning/target/ejbjpa/entities/Address.class b/ch4/JPAPlanningBidirectional/JPAPlanning/target/ejbjpa/entities/Address.class new file mode 100644 index 0000000..dbdeb63 Binary files /dev/null and b/ch4/JPAPlanningBidirectional/JPAPlanning/target/ejbjpa/entities/Address.class differ diff --git a/ch4/JPAPlanningBidirectional/JPAPlanning/target/ejbjpa/entities/Customer.class b/ch4/JPAPlanningBidirectional/JPAPlanning/target/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..be30355 Binary files /dev/null and b/ch4/JPAPlanningBidirectional/JPAPlanning/target/ejbjpa/entities/Customer.class differ diff --git a/ch4/JPAPlanningBidirectional/dist/_bidirectionalapp.jar b/ch4/JPAPlanningBidirectional/dist/_bidirectionalapp.jar new file mode 100644 index 0000000..e922865 Binary files /dev/null and b/ch4/JPAPlanningBidirectional/dist/_bidirectionalapp.jar differ diff --git a/ch4/JPAPlanningBidirectional/dist/bidirectional.jar b/ch4/JPAPlanningBidirectional/dist/bidirectional.jar new file mode 100644 index 0000000..d98afb2 Binary files /dev/null and b/ch4/JPAPlanningBidirectional/dist/bidirectional.jar differ diff --git a/ch4/JPAPlanningBidirectional/target/META-INF/application.xml b/ch4/JPAPlanningBidirectional/target/META-INF/application.xml new file mode 100644 index 0000000..19a7fd9 --- /dev/null +++ b/ch4/JPAPlanningBidirectional/target/META-INF/application.xml @@ -0,0 +1,10 @@ + + + ejbjpa-relation + + jpaplanning-bidirectional.jar + + + appclient-bidirectional.jar + + diff --git a/ch4/JPAPlanningBidirectional/target/appclient-bidirectional.jar b/ch4/JPAPlanningBidirectional/target/appclient-bidirectional.jar new file mode 100644 index 0000000..0939e0d Binary files /dev/null and b/ch4/JPAPlanningBidirectional/target/appclient-bidirectional.jar differ diff --git a/ch4/JPAPlanningBidirectional/target/jpaplanning-bidirectional.jar b/ch4/JPAPlanningBidirectional/target/jpaplanning-bidirectional.jar new file mode 100644 index 0000000..c776006 Binary files /dev/null and b/ch4/JPAPlanningBidirectional/target/jpaplanning-bidirectional.jar differ diff --git a/ch4/JPAPlanningTwoTables/AppClient/dist/appclient-view.jar b/ch4/JPAPlanningTwoTables/AppClient/dist/appclient-view.jar new file mode 100644 index 0000000..16ddc2c Binary files /dev/null and b/ch4/JPAPlanningTwoTables/AppClient/dist/appclient-view.jar differ diff --git a/ch4/JPAPlanningTwoTables/AppClient/src/ejbjpa/client/CustomerSessionClient.java b/ch4/JPAPlanningTwoTables/AppClient/src/ejbjpa/client/CustomerSessionClient.java new file mode 100644 index 0000000..7f9b6a2 --- /dev/null +++ b/ch4/JPAPlanningTwoTables/AppClient/src/ejbjpa/client/CustomerSessionClient.java @@ -0,0 +1,11 @@ +package ejbjpa.client; +import javax.ejb.EJB; +import ejbjpa.ejb.CustomerSession; +public class CustomerSessionClient { + @EJB + private static CustomerSession customerSession; + public static void main (String[] args) + { + System.out.println("Details of the customer whose id=1 is: "+customerSession.getCustomerDetails(1)); + } +} \ No newline at end of file diff --git a/ch4/JPAPlanningTwoTables/AppClient/target/META-INF/MANIFEST.MF b/ch4/JPAPlanningTwoTables/AppClient/target/META-INF/MANIFEST.MF new file mode 100644 index 0000000..e455a43 --- /dev/null +++ b/ch4/JPAPlanningTwoTables/AppClient/target/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: ejbjpa.client.CustomerSessionClient + diff --git a/ch4/JPAPlanningTwoTables/AppClient/target/ejbjpa/client/CustomerSessionClient.class b/ch4/JPAPlanningTwoTables/AppClient/target/ejbjpa/client/CustomerSessionClient.class new file mode 100644 index 0000000..1b12188 Binary files /dev/null and b/ch4/JPAPlanningTwoTables/AppClient/target/ejbjpa/client/CustomerSessionClient.class differ diff --git a/ch4/JPAPlanningTwoTables/JPAPlanning/dist/jpaplanning-twotables.jar b/ch4/JPAPlanningTwoTables/JPAPlanning/dist/jpaplanning-twotables.jar new file mode 100644 index 0000000..e2a6c37 Binary files /dev/null and b/ch4/JPAPlanningTwoTables/JPAPlanning/dist/jpaplanning-twotables.jar differ diff --git a/ch4/JPAPlanningTwoTables/JPAPlanning/dist/twotables.jar b/ch4/JPAPlanningTwoTables/JPAPlanning/dist/twotables.jar new file mode 100644 index 0000000..e6d9ab2 Binary files /dev/null and b/ch4/JPAPlanningTwoTables/JPAPlanning/dist/twotables.jar differ diff --git a/ch4/JPAPlanningTwoTables/JPAPlanning/src/ejbjpa/ejb/CustomerSession.java b/ch4/JPAPlanningTwoTables/JPAPlanning/src/ejbjpa/ejb/CustomerSession.java new file mode 100644 index 0000000..c9c3985 --- /dev/null +++ b/ch4/JPAPlanningTwoTables/JPAPlanning/src/ejbjpa/ejb/CustomerSession.java @@ -0,0 +1,6 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +@Remote +public interface CustomerSession { + public String getCustomerDetails(Integer cust_id); +} diff --git a/ch4/JPAPlanningTwoTables/JPAPlanning/src/ejbjpa/ejb/CustomerSessionBean.java b/ch4/JPAPlanningTwoTables/JPAPlanning/src/ejbjpa/ejb/CustomerSessionBean.java new file mode 100644 index 0000000..f1ee52d --- /dev/null +++ b/ch4/JPAPlanningTwoTables/JPAPlanning/src/ejbjpa/ejb/CustomerSessionBean.java @@ -0,0 +1,31 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; +import ejbjpa.entities.*; + +@Stateless +public class CustomerSessionBean implements CustomerSession { + @PersistenceUnit(unitName = "jpaplanning-twotables-pu") + private EntityManagerFactory emf; + public String getCustomerDetails(Integer cust_id) { + String cust_details; + try { + EntityManager em = emf.createEntityManager(); + Customer cust = em.find(Customer.class, cust_id); + cust_details = cust.getCompany_name()+", "+ + cust.getPhone()+", "+ + "address is: "+" "+ + cust.getStreet()+", "+ + cust.getCity()+", "+ + cust.getState()+", "+ + cust.getZipcode(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return cust_details; + } +} \ No newline at end of file diff --git a/ch4/JPAPlanningTwoTables/JPAPlanning/src/ejbjpa/entities/Customer.java b/ch4/JPAPlanningTwoTables/JPAPlanning/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..32aa1fd --- /dev/null +++ b/ch4/JPAPlanningTwoTables/JPAPlanning/src/ejbjpa/entities/Customer.java @@ -0,0 +1,77 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.SecondaryTable; +import javax.persistence.PrimaryKeyJoinColumn; +@Entity +@Table(name = "CUSTOMERS") +@SecondaryTable(name="BILLING_ADDRESSES", + pkJoinColumns=@PrimaryKeyJoinColumn( + name="CUST_ADDRESS_ID", + referencedColumnName="CUST_ID")) +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "COMPANY_NAME", nullable = false) + private String company_name; + @Column(name = "PHONE", nullable = false) + private String phone; + @Column(name = "STREET", table="BILLING_ADDRESSES", nullable = false) + private String street; + @Column(name = "CITY", table="BILLING_ADDRESSES", nullable = false) + private String city; + @Column(name = "STATE", table="BILLING_ADDRESSES", nullable = false) + private String state; + @Column(name = "ZIPCODE", table="BILLING_ADDRESSES", nullable = false) + private String zipcode; + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCompany_name() { + return this.company_name; + } + public void setCompany_name(String company_name) { + this.company_name = company_name; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } + + public String getStreet() { + return this.street; + } + public void setStreet(String street) { + this.street = street; + } + public String getCity() { + return this.city; + } + public void setCity(String city) { + this.city = city; + } + public String getState() { + return this.state; + } + public void setState(String state) { + this.state = state; + } + public String getZipcode() { + return this.zipcode; + } + public void setZipcode(String zipcode) { + this.zipcode = zipcode; + } + +} diff --git a/ch4/JPAPlanningTwoTables/JPAPlanning/target/META-INF/persistence.xml b/ch4/JPAPlanningTwoTables/JPAPlanning/target/META-INF/persistence.xml new file mode 100644 index 0000000..7a71a4f --- /dev/null +++ b/ch4/JPAPlanningTwoTables/JPAPlanning/target/META-INF/persistence.xml @@ -0,0 +1,11 @@ + + + + jdbc/MySQL +jpaplanning-twotables.jar +ejbjpa.entities.Customer + + + diff --git a/ch4/JPAPlanningTwoTables/JPAPlanning/target/ejbjpa/ejb/CustomerSession.class b/ch4/JPAPlanningTwoTables/JPAPlanning/target/ejbjpa/ejb/CustomerSession.class new file mode 100644 index 0000000..1262df7 Binary files /dev/null and b/ch4/JPAPlanningTwoTables/JPAPlanning/target/ejbjpa/ejb/CustomerSession.class differ diff --git a/ch4/JPAPlanningTwoTables/JPAPlanning/target/ejbjpa/ejb/CustomerSessionBean.class b/ch4/JPAPlanningTwoTables/JPAPlanning/target/ejbjpa/ejb/CustomerSessionBean.class new file mode 100644 index 0000000..fde87d5 Binary files /dev/null and b/ch4/JPAPlanningTwoTables/JPAPlanning/target/ejbjpa/ejb/CustomerSessionBean.class differ diff --git a/ch4/JPAPlanningTwoTables/JPAPlanning/target/ejbjpa/entities/Customer.class b/ch4/JPAPlanningTwoTables/JPAPlanning/target/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..fd7babb Binary files /dev/null and b/ch4/JPAPlanningTwoTables/JPAPlanning/target/ejbjpa/entities/Customer.class differ diff --git a/ch4/JPAPlanningTwoTables/dist/twotables.jar b/ch4/JPAPlanningTwoTables/dist/twotables.jar new file mode 100644 index 0000000..8c2fd35 Binary files /dev/null and b/ch4/JPAPlanningTwoTables/dist/twotables.jar differ diff --git a/ch4/JPAPlanningTwoTables/dist/view.jar b/ch4/JPAPlanningTwoTables/dist/view.jar new file mode 100644 index 0000000..8031184 Binary files /dev/null and b/ch4/JPAPlanningTwoTables/dist/view.jar differ diff --git a/ch4/JPAPlanningTwoTables/target/META-INF/application.xml b/ch4/JPAPlanningTwoTables/target/META-INF/application.xml new file mode 100644 index 0000000..e144837 --- /dev/null +++ b/ch4/JPAPlanningTwoTables/target/META-INF/application.xml @@ -0,0 +1,10 @@ + + + jpaplanning-view + + jpaplanning-twotables.jar + + + appclient-view.jar + + diff --git a/ch4/JPAPlanningTwoTables/target/appclient-view.jar b/ch4/JPAPlanningTwoTables/target/appclient-view.jar new file mode 100644 index 0000000..16ddc2c Binary files /dev/null and b/ch4/JPAPlanningTwoTables/target/appclient-view.jar differ diff --git a/ch4/JPAPlanningTwoTables/target/jpaplanning-twotables.jar b/ch4/JPAPlanningTwoTables/target/jpaplanning-twotables.jar new file mode 100644 index 0000000..e2a6c37 Binary files /dev/null and b/ch4/JPAPlanningTwoTables/target/jpaplanning-twotables.jar differ diff --git a/ch4/JPAPlanningUnidirectional/AppClient/dist/appclient-unidirectional.jar b/ch4/JPAPlanningUnidirectional/AppClient/dist/appclient-unidirectional.jar new file mode 100644 index 0000000..2609e2d Binary files /dev/null and b/ch4/JPAPlanningUnidirectional/AppClient/dist/appclient-unidirectional.jar differ diff --git a/ch4/JPAPlanningUnidirectional/AppClient/src/ejbjpa/client/CustomerSessionClient.java b/ch4/JPAPlanningUnidirectional/AppClient/src/ejbjpa/client/CustomerSessionClient.java new file mode 100644 index 0000000..215ce31 --- /dev/null +++ b/ch4/JPAPlanningUnidirectional/AppClient/src/ejbjpa/client/CustomerSessionClient.java @@ -0,0 +1,11 @@ +package ejbjpa.client; +import javax.ejb.EJB; +import ejbjpa.ejb.CustomerSession; +public class CustomerSessionClient { + @EJB + private static CustomerSession customerSession; + public static void main (String[] args) + { + System.out.println("Billing address of the customer whose id=1 is: "+customerSession.getCustomerAddress(1)); + } +} \ No newline at end of file diff --git a/ch4/JPAPlanningUnidirectional/AppClient/target/META-INF/MANIFEST.MF b/ch4/JPAPlanningUnidirectional/AppClient/target/META-INF/MANIFEST.MF new file mode 100644 index 0000000..e455a43 --- /dev/null +++ b/ch4/JPAPlanningUnidirectional/AppClient/target/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: ejbjpa.client.CustomerSessionClient + diff --git a/ch4/JPAPlanningUnidirectional/AppClient/target/ejbjpa/client/CustomerSessionClient.class b/ch4/JPAPlanningUnidirectional/AppClient/target/ejbjpa/client/CustomerSessionClient.class new file mode 100644 index 0000000..2abcf90 Binary files /dev/null and b/ch4/JPAPlanningUnidirectional/AppClient/target/ejbjpa/client/CustomerSessionClient.class differ diff --git a/ch4/JPAPlanningUnidirectional/JPAPlanning/dist/jpaplanning-unidirectional.jar b/ch4/JPAPlanningUnidirectional/JPAPlanning/dist/jpaplanning-unidirectional.jar new file mode 100644 index 0000000..dbc442f Binary files /dev/null and b/ch4/JPAPlanningUnidirectional/JPAPlanning/dist/jpaplanning-unidirectional.jar differ diff --git a/ch4/JPAPlanningUnidirectional/JPAPlanning/src/ejbjpa/ejb/CustomerSession.java b/ch4/JPAPlanningUnidirectional/JPAPlanning/src/ejbjpa/ejb/CustomerSession.java new file mode 100644 index 0000000..6d999b7 --- /dev/null +++ b/ch4/JPAPlanningUnidirectional/JPAPlanning/src/ejbjpa/ejb/CustomerSession.java @@ -0,0 +1,6 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +@Remote +public interface CustomerSession { + public String getCustomerAddress(Integer cust_id); +} diff --git a/ch4/JPAPlanningUnidirectional/JPAPlanning/src/ejbjpa/ejb/CustomerSessionBean.java b/ch4/JPAPlanningUnidirectional/JPAPlanning/src/ejbjpa/ejb/CustomerSessionBean.java new file mode 100644 index 0000000..fedfc20 --- /dev/null +++ b/ch4/JPAPlanningUnidirectional/JPAPlanning/src/ejbjpa/ejb/CustomerSessionBean.java @@ -0,0 +1,28 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; +import ejbjpa.entities.*; + +@Stateless +public class CustomerSessionBean implements CustomerSession { + @PersistenceUnit(unitName = "ejbjpa-relation-pu") + private EntityManagerFactory emf; + public String getCustomerAddress(Integer cust_id) { + String cust_address; + try { + EntityManager em = emf.createEntityManager(); + Customer cust = em.find(Customer.class, cust_id); + cust_address = cust.getAddress().getStreet()+", "+ + cust.getAddress().getCity()+", "+ + cust.getAddress().getState()+", "+ + cust.getAddress().getZipcode(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return cust_address; + } +} \ No newline at end of file diff --git a/ch4/JPAPlanningUnidirectional/JPAPlanning/src/ejbjpa/entities/Address.java b/ch4/JPAPlanningUnidirectional/JPAPlanning/src/ejbjpa/entities/Address.java new file mode 100644 index 0000000..00bfd2c --- /dev/null +++ b/ch4/JPAPlanningUnidirectional/JPAPlanning/src/ejbjpa/entities/Address.java @@ -0,0 +1,55 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +@Entity +@Table(name = "BILLING_ADDRESSES") +public class Address implements Serializable { + @Id + @Column(name = "CUST_ADDRESS_ID") + private Integer cust_address_id; + @Column(name = "STREET", nullable = false) + private String street; + @Column(name = "CITY", nullable = false) + private String city; + @Column(name = "STATE", nullable = false) + private String state; + @Column(name = "ZIPCODE", nullable = false) + private String zipcode; + + public Address() { + } + public Integer getCust_address_id() { + return this.cust_address_id; + } + public void setCust_address_id(Integer cust_address_id) { + this.cust_address_id = cust_address_id; + } + public String getStreet() { + return this.street; + } + public void setStreet(String street) { + this.street = street; + } + public String getCity() { + return this.city; + } + public void setCity(String city) { + this.city = city; + } + public String getState() { + return this.state; + } + public void setState(String state) { + this.state = state; + } + public String getZipcode() { + return this.zipcode; + } + public void setZipcode(String zipcode) { + this.zipcode = zipcode; + } + +} diff --git a/ch4/JPAPlanningUnidirectional/JPAPlanning/src/ejbjpa/entities/Customer.java b/ch4/JPAPlanningUnidirectional/JPAPlanning/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..7bf89c1 --- /dev/null +++ b/ch4/JPAPlanningUnidirectional/JPAPlanning/src/ejbjpa/entities/Customer.java @@ -0,0 +1,50 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.OneToOne; +import javax.persistence.PrimaryKeyJoinColumn; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "COMPANY_NAME", nullable = false) + private String company_name; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToOne + @PrimaryKeyJoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ADDRESS_ID") + private Address address; + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCompany_name() { + return this.company_name; + } + public void setCompany_name(String company_name) { + this.company_name = company_name; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } + public Address getAddress() { + return this.address; + } + public void setAddress(Address address) { + this.address = address; + } +} diff --git a/ch4/JPAPlanningUnidirectional/JPAPlanning/target/META-INF/persistence.xml b/ch4/JPAPlanningUnidirectional/JPAPlanning/target/META-INF/persistence.xml new file mode 100644 index 0000000..c226712 --- /dev/null +++ b/ch4/JPAPlanningUnidirectional/JPAPlanning/target/META-INF/persistence.xml @@ -0,0 +1,12 @@ + + + + jdbc/MySQL +jpaplanning-unidirectional.jar +ejbjpa.entities.Customer +ejbjpa.entities.Address + + + diff --git a/ch4/JPAPlanningUnidirectional/JPAPlanning/target/ejbjpa/ejb/CustomerSession.class b/ch4/JPAPlanningUnidirectional/JPAPlanning/target/ejbjpa/ejb/CustomerSession.class new file mode 100644 index 0000000..3834ec7 Binary files /dev/null and b/ch4/JPAPlanningUnidirectional/JPAPlanning/target/ejbjpa/ejb/CustomerSession.class differ diff --git a/ch4/JPAPlanningUnidirectional/JPAPlanning/target/ejbjpa/ejb/CustomerSessionBean.class b/ch4/JPAPlanningUnidirectional/JPAPlanning/target/ejbjpa/ejb/CustomerSessionBean.class new file mode 100644 index 0000000..cc07f0b Binary files /dev/null and b/ch4/JPAPlanningUnidirectional/JPAPlanning/target/ejbjpa/ejb/CustomerSessionBean.class differ diff --git a/ch4/JPAPlanningUnidirectional/JPAPlanning/target/ejbjpa/entities/Address.class b/ch4/JPAPlanningUnidirectional/JPAPlanning/target/ejbjpa/entities/Address.class new file mode 100644 index 0000000..feb0405 Binary files /dev/null and b/ch4/JPAPlanningUnidirectional/JPAPlanning/target/ejbjpa/entities/Address.class differ diff --git a/ch4/JPAPlanningUnidirectional/JPAPlanning/target/ejbjpa/entities/Customer.class b/ch4/JPAPlanningUnidirectional/JPAPlanning/target/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..be30355 Binary files /dev/null and b/ch4/JPAPlanningUnidirectional/JPAPlanning/target/ejbjpa/entities/Customer.class differ diff --git a/ch4/JPAPlanningUnidirectional/dist/unidirectionalapp.jar b/ch4/JPAPlanningUnidirectional/dist/unidirectionalapp.jar new file mode 100644 index 0000000..01d9235 Binary files /dev/null and b/ch4/JPAPlanningUnidirectional/dist/unidirectionalapp.jar differ diff --git a/ch4/JPAPlanningUnidirectional/target/META-INF/application.xml b/ch4/JPAPlanningUnidirectional/target/META-INF/application.xml new file mode 100644 index 0000000..8793416 --- /dev/null +++ b/ch4/JPAPlanningUnidirectional/target/META-INF/application.xml @@ -0,0 +1,10 @@ + + + ejbjpa-relation + + jpaplanning-unidirectional.jar + + + appclient-unidirectional.jar + + diff --git a/ch4/JPAPlanningUnidirectional/target/appclient-unidirectional.jar b/ch4/JPAPlanningUnidirectional/target/appclient-unidirectional.jar new file mode 100644 index 0000000..2609e2d Binary files /dev/null and b/ch4/JPAPlanningUnidirectional/target/appclient-unidirectional.jar differ diff --git a/ch4/JPAPlanningUnidirectional/target/jpaplanning-unidirectional.jar b/ch4/JPAPlanningUnidirectional/target/jpaplanning-unidirectional.jar new file mode 100644 index 0000000..dbc442f Binary files /dev/null and b/ch4/JPAPlanningUnidirectional/target/jpaplanning-unidirectional.jar differ diff --git a/ch4/JPAPlanningView/AppClient/dist/appclient-view.jar b/ch4/JPAPlanningView/AppClient/dist/appclient-view.jar new file mode 100644 index 0000000..16ddc2c Binary files /dev/null and b/ch4/JPAPlanningView/AppClient/dist/appclient-view.jar differ diff --git a/ch4/JPAPlanningView/AppClient/src/ejbjpa/client/CustomerSessionClient.java b/ch4/JPAPlanningView/AppClient/src/ejbjpa/client/CustomerSessionClient.java new file mode 100644 index 0000000..7f9b6a2 --- /dev/null +++ b/ch4/JPAPlanningView/AppClient/src/ejbjpa/client/CustomerSessionClient.java @@ -0,0 +1,11 @@ +package ejbjpa.client; +import javax.ejb.EJB; +import ejbjpa.ejb.CustomerSession; +public class CustomerSessionClient { + @EJB + private static CustomerSession customerSession; + public static void main (String[] args) + { + System.out.println("Details of the customer whose id=1 is: "+customerSession.getCustomerDetails(1)); + } +} \ No newline at end of file diff --git a/ch4/JPAPlanningView/AppClient/target/META-INF/MANIFEST.MF b/ch4/JPAPlanningView/AppClient/target/META-INF/MANIFEST.MF new file mode 100644 index 0000000..e455a43 --- /dev/null +++ b/ch4/JPAPlanningView/AppClient/target/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: ejbjpa.client.CustomerSessionClient + diff --git a/ch4/JPAPlanningView/AppClient/target/ejbjpa/client/CustomerSessionClient.class b/ch4/JPAPlanningView/AppClient/target/ejbjpa/client/CustomerSessionClient.class new file mode 100644 index 0000000..1b12188 Binary files /dev/null and b/ch4/JPAPlanningView/AppClient/target/ejbjpa/client/CustomerSessionClient.class differ diff --git a/ch4/JPAPlanningView/JPAPlanning/dist/jpaplanning-view.jar b/ch4/JPAPlanningView/JPAPlanning/dist/jpaplanning-view.jar new file mode 100644 index 0000000..84ec048 Binary files /dev/null and b/ch4/JPAPlanningView/JPAPlanning/dist/jpaplanning-view.jar differ diff --git a/ch4/JPAPlanningView/JPAPlanning/src/ejbjpa/ejb/CustomerSession.java b/ch4/JPAPlanningView/JPAPlanning/src/ejbjpa/ejb/CustomerSession.java new file mode 100644 index 0000000..c9c3985 --- /dev/null +++ b/ch4/JPAPlanningView/JPAPlanning/src/ejbjpa/ejb/CustomerSession.java @@ -0,0 +1,6 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +@Remote +public interface CustomerSession { + public String getCustomerDetails(Integer cust_id); +} diff --git a/ch4/JPAPlanningView/JPAPlanning/src/ejbjpa/ejb/CustomerSessionBean.java b/ch4/JPAPlanningView/JPAPlanning/src/ejbjpa/ejb/CustomerSessionBean.java new file mode 100644 index 0000000..8508603 --- /dev/null +++ b/ch4/JPAPlanningView/JPAPlanning/src/ejbjpa/ejb/CustomerSessionBean.java @@ -0,0 +1,31 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; +import ejbjpa.entities.*; + +@Stateless +public class CustomerSessionBean implements CustomerSession { + @PersistenceUnit(unitName = "jpaplanning-view-pu") + private EntityManagerFactory emf; + public String getCustomerDetails(Integer cust_id) { + String cust_details; + try { + EntityManager em = emf.createEntityManager(); + Customer cust = em.find(Customer.class, cust_id); + cust_details = cust.getCompany_name()+", "+ + cust.getPhone()+", "+ + "address is: "+" "+ + cust.getStreet()+", "+ + cust.getCity()+", "+ + cust.getState()+", "+ + cust.getZipcode(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return cust_details; + } +} \ No newline at end of file diff --git a/ch4/JPAPlanningView/JPAPlanning/src/ejbjpa/entities/Customer.java b/ch4/JPAPlanningView/JPAPlanning/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..a6b0ec9 --- /dev/null +++ b/ch4/JPAPlanningView/JPAPlanning/src/ejbjpa/entities/Customer.java @@ -0,0 +1,72 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +@Entity +@Table(name = "CUSTOMERS_V") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "COMPANY_NAME", nullable = false) + private String company_name; + @Column(name = "PHONE", nullable = false) + private String phone; + @Column(name = "STREET", nullable = false) + private String street; + @Column(name = "CITY", nullable = false) + private String city; + @Column(name = "STATE", nullable = false) + private String state; + @Column(name = "ZIPCODE", nullable = false) + private String zipcode; + + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCompany_name() { + return this.company_name; + } + public void setCompany_name(String company_name) { + this.company_name = company_name; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } + + public String getStreet() { + return this.street; + } + public void setStreet(String street) { + this.street = street; + } + public String getCity() { + return this.city; + } + public void setCity(String city) { + this.city = city; + } + public String getState() { + return this.state; + } + public void setState(String state) { + this.state = state; + } + public String getZipcode() { + return this.zipcode; + } + public void setZipcode(String zipcode) { + this.zipcode = zipcode; + } + +} diff --git a/ch4/JPAPlanningView/JPAPlanning/target/META-INF/persistence.xml b/ch4/JPAPlanningView/JPAPlanning/target/META-INF/persistence.xml new file mode 100644 index 0000000..8de4ba8 --- /dev/null +++ b/ch4/JPAPlanningView/JPAPlanning/target/META-INF/persistence.xml @@ -0,0 +1,11 @@ + + + + jdbc/MySQL +jpaplanning-view.jar +ejbjpa.entities.Customer + + + diff --git a/ch4/JPAPlanningView/JPAPlanning/target/ejbjpa/ejb/CustomerSession.class b/ch4/JPAPlanningView/JPAPlanning/target/ejbjpa/ejb/CustomerSession.class new file mode 100644 index 0000000..1262df7 Binary files /dev/null and b/ch4/JPAPlanningView/JPAPlanning/target/ejbjpa/ejb/CustomerSession.class differ diff --git a/ch4/JPAPlanningView/JPAPlanning/target/ejbjpa/ejb/CustomerSessionBean.class b/ch4/JPAPlanningView/JPAPlanning/target/ejbjpa/ejb/CustomerSessionBean.class new file mode 100644 index 0000000..f79c232 Binary files /dev/null and b/ch4/JPAPlanningView/JPAPlanning/target/ejbjpa/ejb/CustomerSessionBean.class differ diff --git a/ch4/JPAPlanningView/JPAPlanning/target/ejbjpa/entities/Customer.class b/ch4/JPAPlanningView/JPAPlanning/target/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..da05364 Binary files /dev/null and b/ch4/JPAPlanningView/JPAPlanning/target/ejbjpa/entities/Customer.class differ diff --git a/ch4/JPAPlanningView/dist/view.jar b/ch4/JPAPlanningView/dist/view.jar new file mode 100644 index 0000000..8031184 Binary files /dev/null and b/ch4/JPAPlanningView/dist/view.jar differ diff --git a/ch4/JPAPlanningView/target/META-INF/application.xml b/ch4/JPAPlanningView/target/META-INF/application.xml new file mode 100644 index 0000000..bc65d8d --- /dev/null +++ b/ch4/JPAPlanningView/target/META-INF/application.xml @@ -0,0 +1,10 @@ + + + jpaplanning-view + + jpaplanning-view.jar + + + appclient-view.jar + + diff --git a/ch4/JPAPlanningView/target/appclient-view.jar b/ch4/JPAPlanningView/target/appclient-view.jar new file mode 100644 index 0000000..16ddc2c Binary files /dev/null and b/ch4/JPAPlanningView/target/appclient-view.jar differ diff --git a/ch4/JPAPlanningView/target/jpaplanning-view.jar b/ch4/JPAPlanningView/target/jpaplanning-view.jar new file mode 100644 index 0000000..84ec048 Binary files /dev/null and b/ch4/JPAPlanningView/target/jpaplanning-view.jar differ diff --git a/ch4/JPAPlanningViewUpdate/AppClient/dist/appclient-view.jar b/ch4/JPAPlanningViewUpdate/AppClient/dist/appclient-view.jar new file mode 100644 index 0000000..16ddc2c Binary files /dev/null and b/ch4/JPAPlanningViewUpdate/AppClient/dist/appclient-view.jar differ diff --git a/ch4/JPAPlanningViewUpdate/AppClient/src/ejbjpa/client/CustomerSessionClient.java b/ch4/JPAPlanningViewUpdate/AppClient/src/ejbjpa/client/CustomerSessionClient.java new file mode 100644 index 0000000..7f9b6a2 --- /dev/null +++ b/ch4/JPAPlanningViewUpdate/AppClient/src/ejbjpa/client/CustomerSessionClient.java @@ -0,0 +1,11 @@ +package ejbjpa.client; +import javax.ejb.EJB; +import ejbjpa.ejb.CustomerSession; +public class CustomerSessionClient { + @EJB + private static CustomerSession customerSession; + public static void main (String[] args) + { + System.out.println("Details of the customer whose id=1 is: "+customerSession.getCustomerDetails(1)); + } +} \ No newline at end of file diff --git a/ch4/JPAPlanningViewUpdate/AppClient/target/META-INF/MANIFEST.MF b/ch4/JPAPlanningViewUpdate/AppClient/target/META-INF/MANIFEST.MF new file mode 100644 index 0000000..e455a43 --- /dev/null +++ b/ch4/JPAPlanningViewUpdate/AppClient/target/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: ejbjpa.client.CustomerSessionClient + diff --git a/ch4/JPAPlanningViewUpdate/AppClient/target/ejbjpa/client/CustomerSessionClient.class b/ch4/JPAPlanningViewUpdate/AppClient/target/ejbjpa/client/CustomerSessionClient.class new file mode 100644 index 0000000..1b12188 Binary files /dev/null and b/ch4/JPAPlanningViewUpdate/AppClient/target/ejbjpa/client/CustomerSessionClient.class differ diff --git a/ch4/JPAPlanningViewUpdate/JPAPlanning/dist/jpaplanning-view-update.jar b/ch4/JPAPlanningViewUpdate/JPAPlanning/dist/jpaplanning-view-update.jar new file mode 100644 index 0000000..081febd Binary files /dev/null and b/ch4/JPAPlanningViewUpdate/JPAPlanning/dist/jpaplanning-view-update.jar differ diff --git a/ch4/JPAPlanningViewUpdate/JPAPlanning/src/ejbjpa/ejb/CustomerSession.java b/ch4/JPAPlanningViewUpdate/JPAPlanning/src/ejbjpa/ejb/CustomerSession.java new file mode 100644 index 0000000..c9c3985 --- /dev/null +++ b/ch4/JPAPlanningViewUpdate/JPAPlanning/src/ejbjpa/ejb/CustomerSession.java @@ -0,0 +1,6 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +@Remote +public interface CustomerSession { + public String getCustomerDetails(Integer cust_id); +} diff --git a/ch4/JPAPlanningViewUpdate/JPAPlanning/src/ejbjpa/ejb/CustomerSessionBean.java b/ch4/JPAPlanningViewUpdate/JPAPlanning/src/ejbjpa/ejb/CustomerSessionBean.java new file mode 100644 index 0000000..c6e75b0 --- /dev/null +++ b/ch4/JPAPlanningViewUpdate/JPAPlanning/src/ejbjpa/ejb/CustomerSessionBean.java @@ -0,0 +1,35 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; +import ejbjpa.entities.*; + +@Stateless +public class CustomerSessionBean implements CustomerSession { + @PersistenceUnit(unitName = "jpaplanning-view-update-pu") + private EntityManagerFactory emf; + public String getCustomerDetails(Integer cust_id) { + String cust_details; + try { + EntityManager em = emf.createEntityManager(); + Customer cust = em.find(Customer.class, cust_id); + cust.setPhone("(650)777-5669"); + em.flush(); + cust.setZipcode("94401"); + em.flush(); + cust_details = cust.getCompany_name()+", "+ + cust.getPhone()+", "+ + "address is: "+" "+ + cust.getStreet()+", "+ + cust.getCity()+", "+ + cust.getState()+", "+ + cust.getZipcode(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return cust_details; + } +} \ No newline at end of file diff --git a/ch4/JPAPlanningViewUpdate/JPAPlanning/src/ejbjpa/entities/Customer.java b/ch4/JPAPlanningViewUpdate/JPAPlanning/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..a6b0ec9 --- /dev/null +++ b/ch4/JPAPlanningViewUpdate/JPAPlanning/src/ejbjpa/entities/Customer.java @@ -0,0 +1,72 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +@Entity +@Table(name = "CUSTOMERS_V") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "COMPANY_NAME", nullable = false) + private String company_name; + @Column(name = "PHONE", nullable = false) + private String phone; + @Column(name = "STREET", nullable = false) + private String street; + @Column(name = "CITY", nullable = false) + private String city; + @Column(name = "STATE", nullable = false) + private String state; + @Column(name = "ZIPCODE", nullable = false) + private String zipcode; + + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCompany_name() { + return this.company_name; + } + public void setCompany_name(String company_name) { + this.company_name = company_name; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } + + public String getStreet() { + return this.street; + } + public void setStreet(String street) { + this.street = street; + } + public String getCity() { + return this.city; + } + public void setCity(String city) { + this.city = city; + } + public String getState() { + return this.state; + } + public void setState(String state) { + this.state = state; + } + public String getZipcode() { + return this.zipcode; + } + public void setZipcode(String zipcode) { + this.zipcode = zipcode; + } + +} diff --git a/ch4/JPAPlanningViewUpdate/JPAPlanning/target/META-INF/persistence.xml b/ch4/JPAPlanningViewUpdate/JPAPlanning/target/META-INF/persistence.xml new file mode 100644 index 0000000..ec567eb --- /dev/null +++ b/ch4/JPAPlanningViewUpdate/JPAPlanning/target/META-INF/persistence.xml @@ -0,0 +1,11 @@ + + + + jdbc/MySQL +jpaplanning-view-update.jar +ejbjpa.entities.Customer + + + diff --git a/ch4/JPAPlanningViewUpdate/JPAPlanning/target/ejbjpa/ejb/CustomerSession.class b/ch4/JPAPlanningViewUpdate/JPAPlanning/target/ejbjpa/ejb/CustomerSession.class new file mode 100644 index 0000000..1262df7 Binary files /dev/null and b/ch4/JPAPlanningViewUpdate/JPAPlanning/target/ejbjpa/ejb/CustomerSession.class differ diff --git a/ch4/JPAPlanningViewUpdate/JPAPlanning/target/ejbjpa/ejb/CustomerSessionBean.class b/ch4/JPAPlanningViewUpdate/JPAPlanning/target/ejbjpa/ejb/CustomerSessionBean.class new file mode 100644 index 0000000..868a305 Binary files /dev/null and b/ch4/JPAPlanningViewUpdate/JPAPlanning/target/ejbjpa/ejb/CustomerSessionBean.class differ diff --git a/ch4/JPAPlanningViewUpdate/JPAPlanning/target/ejbjpa/entities/Customer.class b/ch4/JPAPlanningViewUpdate/JPAPlanning/target/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..da05364 Binary files /dev/null and b/ch4/JPAPlanningViewUpdate/JPAPlanning/target/ejbjpa/entities/Customer.class differ diff --git a/ch4/JPAPlanningViewUpdate/dist/view-update.jar b/ch4/JPAPlanningViewUpdate/dist/view-update.jar new file mode 100644 index 0000000..4506038 Binary files /dev/null and b/ch4/JPAPlanningViewUpdate/dist/view-update.jar differ diff --git a/ch4/JPAPlanningViewUpdate/dist/view.jar b/ch4/JPAPlanningViewUpdate/dist/view.jar new file mode 100644 index 0000000..6d71b44 Binary files /dev/null and b/ch4/JPAPlanningViewUpdate/dist/view.jar differ diff --git a/ch4/JPAPlanningViewUpdate/target/META-INF/application.xml b/ch4/JPAPlanningViewUpdate/target/META-INF/application.xml new file mode 100644 index 0000000..47ad081 --- /dev/null +++ b/ch4/JPAPlanningViewUpdate/target/META-INF/application.xml @@ -0,0 +1,10 @@ + + + jpaplanning-view-update + + jpaplanning-view-update.jar + + + appclient-view.jar + + diff --git a/ch4/JPAPlanningViewUpdate/target/appclient-view.jar b/ch4/JPAPlanningViewUpdate/target/appclient-view.jar new file mode 100644 index 0000000..16ddc2c Binary files /dev/null and b/ch4/JPAPlanningViewUpdate/target/appclient-view.jar differ diff --git a/ch4/JPAPlanningViewUpdate/target/jpaplanning-view-update.jar b/ch4/JPAPlanningViewUpdate/target/jpaplanning-view-update.jar new file mode 100644 index 0000000..081febd Binary files /dev/null and b/ch4/JPAPlanningViewUpdate/target/jpaplanning-view-update.jar differ diff --git a/ch4/TransactionsPlanning/AppClient/dist/appclient-order.jar b/ch4/TransactionsPlanning/AppClient/dist/appclient-order.jar new file mode 100644 index 0000000..59863eb Binary files /dev/null and b/ch4/TransactionsPlanning/AppClient/dist/appclient-order.jar differ diff --git a/ch4/TransactionsPlanning/AppClient/src/ejbjpa/client/OrderSessionClient.java b/ch4/TransactionsPlanning/AppClient/src/ejbjpa/client/OrderSessionClient.java new file mode 100644 index 0000000..69aff36 --- /dev/null +++ b/ch4/TransactionsPlanning/AppClient/src/ejbjpa/client/OrderSessionClient.java @@ -0,0 +1,15 @@ +package ejbjpa.client; +import javax.ejb.EJB; +import ejbjpa.ejb.OrderSession; +public class OrderSessionClient { + @EJB + private static OrderSession orderSession; + public static void main (String[] args) + { + Integer pono = Integer.parseInt(args[0]); //= 7; + Integer cust_id = Integer.parseInt(args[1]); // = 1; + Integer units = Integer.parseInt(args[2]); // = 1; + String book_id = args[3]; // = "1430209631"; + orderSession.placeOrder(pono, cust_id, units, book_id); + } +} \ No newline at end of file diff --git a/ch4/TransactionsPlanning/AppClient/target/META-INF/MANIFEST.MF b/ch4/TransactionsPlanning/AppClient/target/META-INF/MANIFEST.MF new file mode 100644 index 0000000..245bfc1 --- /dev/null +++ b/ch4/TransactionsPlanning/AppClient/target/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: ejbjpa.client.OrderSessionClient + diff --git a/ch4/TransactionsPlanning/AppClient/target/ejbjpa/client/OrderSessionClient.class b/ch4/TransactionsPlanning/AppClient/target/ejbjpa/client/OrderSessionClient.class new file mode 100644 index 0000000..568a70b Binary files /dev/null and b/ch4/TransactionsPlanning/AppClient/target/ejbjpa/client/OrderSessionClient.class differ diff --git a/ch4/TransactionsPlanning/PlaceOrder/dist/order.jar b/ch4/TransactionsPlanning/PlaceOrder/dist/order.jar new file mode 100644 index 0000000..3e12692 Binary files /dev/null and b/ch4/TransactionsPlanning/PlaceOrder/dist/order.jar differ diff --git a/ch4/TransactionsPlanning/PlaceOrder/src/ejbjpa/ejb/OrderSession.java b/ch4/TransactionsPlanning/PlaceOrder/src/ejbjpa/ejb/OrderSession.java new file mode 100644 index 0000000..8578fe3 --- /dev/null +++ b/ch4/TransactionsPlanning/PlaceOrder/src/ejbjpa/ejb/OrderSession.java @@ -0,0 +1,9 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +@Remote +public interface OrderSession { + public void placeOrder(Integer pono, + Integer cust_id, + Integer units, + String book_id); +} diff --git a/ch4/TransactionsPlanning/PlaceOrder/src/ejbjpa/ejb/OrderSessionBean.java b/ch4/TransactionsPlanning/PlaceOrder/src/ejbjpa/ejb/OrderSessionBean.java new file mode 100644 index 0000000..23569cf --- /dev/null +++ b/ch4/TransactionsPlanning/PlaceOrder/src/ejbjpa/ejb/OrderSessionBean.java @@ -0,0 +1,33 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; +import ejbjpa.entities.*; + +@Stateless +public class OrderSessionBean implements OrderSession { + @PersistenceUnit(unitName = "order-pu") + private EntityManagerFactory emf; + public void placeOrder(Integer pono, + Integer cust_id, + Integer units, + String book_id) + { + try { + EntityManager em = emf.createEntityManager(); + Book book = (Book) em.find(Book.class, book_id); + Order order = new Order(); + order.setPono(pono); + order.setCust_id(cust_id); + order.setUnits(units); + book.setQuantity(book.getQuantity()-units); + order.setBook(book); + em.persist(order); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/ch4/TransactionsPlanning/PlaceOrder/src/ejbjpa/entities/Book.java b/ch4/TransactionsPlanning/PlaceOrder/src/ejbjpa/entities/Book.java new file mode 100644 index 0000000..ce545d6 --- /dev/null +++ b/ch4/TransactionsPlanning/PlaceOrder/src/ejbjpa/entities/Book.java @@ -0,0 +1,68 @@ +package ejbjpa.entities; +import java.util.List; +import javax.persistence.CascadeType; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.OneToMany; +@Entity +@Table(name = "BOOKS") +public class Book implements Serializable { + + @Id + @Column(name = "ISBN") + private String isbn; + @Column(name = "TITLE", nullable = false) + private String title; + @Column(name = "AUTHOR", nullable = false) + private String author; + @Column(name = "PRICE", nullable = false) + private Double price; + @Column(name = "QUANTITY", nullable = false) + private Integer quantity; + + @OneToMany(mappedBy="book", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + + public Book() { + } + public String getIsbn() { + return this.isbn; + } + public void setIsbn(String isbn) { + this.isbn = isbn; + } + public String getTitle() { + return this.title; + } + public void setTitle(String title) { + this.title = title; + } + public String getAuthor() { + return this.author; + } + public void setAuthor(String author) { + this.author = author; + } + + public Double getPrice() { + return this.price; + } + public void setPrice(Double price) { + this.price = price; + } + public Integer getQuantity() { + return this.quantity; + } + public void setQuantity(Integer quantity) { + this.quantity = quantity; + } +} diff --git a/ch4/TransactionsPlanning/PlaceOrder/src/ejbjpa/entities/Order.java b/ch4/TransactionsPlanning/PlaceOrder/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..dfd6c01 --- /dev/null +++ b/ch4/TransactionsPlanning/PlaceOrder/src/ejbjpa/entities/Order.java @@ -0,0 +1,54 @@ +package ejbjpa.entities; + +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; + +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + @Id + @Column(name = "PONO") + private Integer pono; + @Column(name = "CUST_ID", nullable = false) + private Integer cust_id; + @Column(name = "UNITS", nullable = false) + private Integer units; + @ManyToOne + @JoinColumn( + name="BOOK_ID", + referencedColumnName="ISBN") + private Book book; + + public Order() { + } + public Integer getPono() { + return this.pono; + } + public void setPono(Integer pono) { + this.pono = pono; + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public Integer getUnits() { + return this.units; + } + public void setUnits(Integer units) { + this.units = units; + } + public Book getBook() { + return this.book; + } + public void setBook(Book book) { + this.book = book; + } + +} diff --git a/ch4/TransactionsPlanning/PlaceOrder/target/META-INF/persistence.xml b/ch4/TransactionsPlanning/PlaceOrder/target/META-INF/persistence.xml new file mode 100644 index 0000000..e4ca601 --- /dev/null +++ b/ch4/TransactionsPlanning/PlaceOrder/target/META-INF/persistence.xml @@ -0,0 +1,13 @@ + + + + jdbc/MySQL +order.jar +ejbjpa.entities.Order +ejbjpa.entities.Book + + + + diff --git a/ch4/TransactionsPlanning/PlaceOrder/target/ejbjpa/ejb/OrderSession.class b/ch4/TransactionsPlanning/PlaceOrder/target/ejbjpa/ejb/OrderSession.class new file mode 100644 index 0000000..b19ddac Binary files /dev/null and b/ch4/TransactionsPlanning/PlaceOrder/target/ejbjpa/ejb/OrderSession.class differ diff --git a/ch4/TransactionsPlanning/PlaceOrder/target/ejbjpa/ejb/OrderSessionBean.class b/ch4/TransactionsPlanning/PlaceOrder/target/ejbjpa/ejb/OrderSessionBean.class new file mode 100644 index 0000000..c73ae4e Binary files /dev/null and b/ch4/TransactionsPlanning/PlaceOrder/target/ejbjpa/ejb/OrderSessionBean.class differ diff --git a/ch4/TransactionsPlanning/PlaceOrder/target/ejbjpa/entities/Book.class b/ch4/TransactionsPlanning/PlaceOrder/target/ejbjpa/entities/Book.class new file mode 100644 index 0000000..72fea7e Binary files /dev/null and b/ch4/TransactionsPlanning/PlaceOrder/target/ejbjpa/entities/Book.class differ diff --git a/ch4/TransactionsPlanning/PlaceOrder/target/ejbjpa/entities/Order.class b/ch4/TransactionsPlanning/PlaceOrder/target/ejbjpa/entities/Order.class new file mode 100644 index 0000000..3907d1c Binary files /dev/null and b/ch4/TransactionsPlanning/PlaceOrder/target/ejbjpa/entities/Order.class differ diff --git a/ch4/TransactionsPlanning/dist/order.jar b/ch4/TransactionsPlanning/dist/order.jar new file mode 100644 index 0000000..dcb298a Binary files /dev/null and b/ch4/TransactionsPlanning/dist/order.jar differ diff --git a/ch4/TransactionsPlanning/dist/transactions-planning.jar b/ch4/TransactionsPlanning/dist/transactions-planning.jar new file mode 100644 index 0000000..e879385 Binary files /dev/null and b/ch4/TransactionsPlanning/dist/transactions-planning.jar differ diff --git a/ch4/TransactionsPlanning/dist/view-update.jar b/ch4/TransactionsPlanning/dist/view-update.jar new file mode 100644 index 0000000..4506038 Binary files /dev/null and b/ch4/TransactionsPlanning/dist/view-update.jar differ diff --git a/ch4/TransactionsPlanning/dist/view.jar b/ch4/TransactionsPlanning/dist/view.jar new file mode 100644 index 0000000..6d71b44 Binary files /dev/null and b/ch4/TransactionsPlanning/dist/view.jar differ diff --git a/ch4/TransactionsPlanning/target/META-INF/application.xml b/ch4/TransactionsPlanning/target/META-INF/application.xml new file mode 100644 index 0000000..c3280d3 --- /dev/null +++ b/ch4/TransactionsPlanning/target/META-INF/application.xml @@ -0,0 +1,10 @@ + + + transactions-planning + + order.jar + + + appclient-order.jar + + diff --git a/ch4/TransactionsPlanning/target/appclient-order.jar b/ch4/TransactionsPlanning/target/appclient-order.jar new file mode 100644 index 0000000..59863eb Binary files /dev/null and b/ch4/TransactionsPlanning/target/appclient-order.jar differ diff --git a/ch4/TransactionsPlanning/target/order.jar b/ch4/TransactionsPlanning/target/order.jar new file mode 100644 index 0000000..3e12692 Binary files /dev/null and b/ch4/TransactionsPlanning/target/order.jar differ diff --git a/ch4/TwoEjbs/AppClient/dist/appclient-client.jar b/ch4/TwoEjbs/AppClient/dist/appclient-client.jar new file mode 100644 index 0000000..8e32379 Binary files /dev/null and b/ch4/TwoEjbs/AppClient/dist/appclient-client.jar differ diff --git a/ch4/TwoEjbs/AppClient/src/ejbjpa/client/OrderSessionClient.java b/ch4/TwoEjbs/AppClient/src/ejbjpa/client/OrderSessionClient.java new file mode 100644 index 0000000..5f5de79 --- /dev/null +++ b/ch4/TwoEjbs/AppClient/src/ejbjpa/client/OrderSessionClient.java @@ -0,0 +1,19 @@ +package ejbjpa.client; +import javax.ejb.EJB; +import ejbjpa.ejb.OrderSession; +import ejbjpa.ejb.CustSession; +public class OrderSessionClient { + @EJB + private static CustSession custSession; + @EJB + private static OrderSession orderSession; + public static void main (String[] args) + { + Integer pono = Integer.parseInt(args[0]);//= 7; + String company_name = args[1]; // = 1; + Integer units = Integer.parseInt(args[2]); // = 1; + String book_id = args[3]; // = "1430209631"; + Integer cust_id = custSession.getCustId(company_name); + orderSession.placeOrder(pono, cust_id, units, book_id); + } +} \ No newline at end of file diff --git a/ch4/TwoEjbs/AppClient/target/META-INF/MANIFEST.MF b/ch4/TwoEjbs/AppClient/target/META-INF/MANIFEST.MF new file mode 100644 index 0000000..245bfc1 --- /dev/null +++ b/ch4/TwoEjbs/AppClient/target/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: ejbjpa.client.OrderSessionClient + diff --git a/ch4/TwoEjbs/AppClient/target/ejbjpa/client/OrderSessionClient.class b/ch4/TwoEjbs/AppClient/target/ejbjpa/client/OrderSessionClient.class new file mode 100644 index 0000000..4128302 Binary files /dev/null and b/ch4/TwoEjbs/AppClient/target/ejbjpa/client/OrderSessionClient.class differ diff --git a/ch4/TwoEjbs/GetCust/dist/cust.jar b/ch4/TwoEjbs/GetCust/dist/cust.jar new file mode 100644 index 0000000..29866aa Binary files /dev/null and b/ch4/TwoEjbs/GetCust/dist/cust.jar differ diff --git a/ch4/TwoEjbs/GetCust/src/ejbjpa/ejb/CustSession.java b/ch4/TwoEjbs/GetCust/src/ejbjpa/ejb/CustSession.java new file mode 100644 index 0000000..dfe651e --- /dev/null +++ b/ch4/TwoEjbs/GetCust/src/ejbjpa/ejb/CustSession.java @@ -0,0 +1,6 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +@Remote +public interface CustSession { + public Integer getCustId(String company_name); +} \ No newline at end of file diff --git a/ch4/TwoEjbs/GetCust/src/ejbjpa/ejb/CustSessionBean.java b/ch4/TwoEjbs/GetCust/src/ejbjpa/ejb/CustSessionBean.java new file mode 100644 index 0000000..6ea5501 --- /dev/null +++ b/ch4/TwoEjbs/GetCust/src/ejbjpa/ejb/CustSessionBean.java @@ -0,0 +1,28 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; +import ejbjpa.entities.*; + +@Stateless +public class CustSessionBean implements CustSession { + @PersistenceUnit(unitName = "cust-pu") + private EntityManagerFactory emf; + public Integer getCustId(String company_name) + { + Integer cust_id; + try { + EntityManager em = emf.createEntityManager(); + Customer customer = (Customer) em.createQuery("SELECT c FROM Customer c WHERE c.company_name LIKE :company_name") + .setParameter("company_name", company_name) +.getSingleResult(); + cust_id = customer.getCust_id(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return cust_id; + } +} \ No newline at end of file diff --git a/ch4/TwoEjbs/GetCust/src/ejbjpa/entities/Customer.java b/ch4/TwoEjbs/GetCust/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..af6f9ec --- /dev/null +++ b/ch4/TwoEjbs/GetCust/src/ejbjpa/entities/Customer.java @@ -0,0 +1,36 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "COMPANY_NAME", nullable = false) + private String company_name; + @Column(name = "PHONE", nullable = false) + private String phone; + + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCompany_name() { + return this.company_name; + } + public void setCompany_name(String company_name) { + this.company_name = company_name; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/ch4/TwoEjbs/GetCust/target/META-INF/persistence.xml b/ch4/TwoEjbs/GetCust/target/META-INF/persistence.xml new file mode 100644 index 0000000..d292c24 --- /dev/null +++ b/ch4/TwoEjbs/GetCust/target/META-INF/persistence.xml @@ -0,0 +1,11 @@ + + + + jdbc/MySQL +cust.jar +ejbjpa.entities.Customer + + + diff --git a/ch4/TwoEjbs/GetCust/target/ejbjpa/ejb/CustSession.class b/ch4/TwoEjbs/GetCust/target/ejbjpa/ejb/CustSession.class new file mode 100644 index 0000000..e5886ec Binary files /dev/null and b/ch4/TwoEjbs/GetCust/target/ejbjpa/ejb/CustSession.class differ diff --git a/ch4/TwoEjbs/GetCust/target/ejbjpa/ejb/CustSessionBean.class b/ch4/TwoEjbs/GetCust/target/ejbjpa/ejb/CustSessionBean.class new file mode 100644 index 0000000..acc4151 Binary files /dev/null and b/ch4/TwoEjbs/GetCust/target/ejbjpa/ejb/CustSessionBean.class differ diff --git a/ch4/TwoEjbs/GetCust/target/ejbjpa/entities/Customer.class b/ch4/TwoEjbs/GetCust/target/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..0ff0b83 Binary files /dev/null and b/ch4/TwoEjbs/GetCust/target/ejbjpa/entities/Customer.class differ diff --git a/ch4/TwoEjbs/PlaceOrder/dist/order.jar b/ch4/TwoEjbs/PlaceOrder/dist/order.jar new file mode 100644 index 0000000..c0d8a5c Binary files /dev/null and b/ch4/TwoEjbs/PlaceOrder/dist/order.jar differ diff --git a/ch4/TwoEjbs/PlaceOrder/src/ejbjpa/ejb/OrderSession.java b/ch4/TwoEjbs/PlaceOrder/src/ejbjpa/ejb/OrderSession.java new file mode 100644 index 0000000..ff2c5c6 --- /dev/null +++ b/ch4/TwoEjbs/PlaceOrder/src/ejbjpa/ejb/OrderSession.java @@ -0,0 +1,9 @@ +package ejbjpa.ejb; +//import javax.ejb.Remote; +//@Remote +public interface OrderSession { + public void placeOrder(Integer pono, + Integer cust_id, + Integer units, + String book_id); +} \ No newline at end of file diff --git a/ch4/TwoEjbs/PlaceOrder/src/ejbjpa/ejb/OrderSessionBean.java b/ch4/TwoEjbs/PlaceOrder/src/ejbjpa/ejb/OrderSessionBean.java new file mode 100644 index 0000000..a4e79e3 --- /dev/null +++ b/ch4/TwoEjbs/PlaceOrder/src/ejbjpa/ejb/OrderSessionBean.java @@ -0,0 +1,33 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +//import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +//import javax.persistence.PersistenceUnit; +import ejbjpa.entities.*; + +//@Stateless +public class OrderSessionBean implements OrderSession { +// @PersistenceUnit(unitName = "order-pu") + private EntityManagerFactory emf; + public void placeOrder(Integer pono, + Integer cust_id, + Integer units, + String book_id) + { + try { + EntityManager em = emf.createEntityManager(); + Book book = (Book) em.find(Book.class, book_id); + Order order = new Order(); + order.setPono(pono); + order.setCust_id(cust_id); + order.setUnits(units); + book.setQuantity(book.getQuantity()-units); + order.setBook(book); + em.persist(order); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/ch4/TwoEjbs/PlaceOrder/src/ejbjpa/entities/Book.java b/ch4/TwoEjbs/PlaceOrder/src/ejbjpa/entities/Book.java new file mode 100644 index 0000000..9dcb6a9 --- /dev/null +++ b/ch4/TwoEjbs/PlaceOrder/src/ejbjpa/entities/Book.java @@ -0,0 +1,67 @@ +package ejbjpa.entities; +import java.util.List; +import javax.persistence.CascadeType; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.OneToMany; +@Entity +@Table(name = "BOOKS") +public class Book implements Serializable { + @Id + @Column(name = "ISBN") + private String isbn; + @Column(name = "TITLE", nullable = false) + private String title; + @Column(name = "AUTHOR", nullable = false) + private String author; + @Column(name = "PRICE", nullable = false) + private Double price; + @Column(name = "QUANTITY", nullable = false) + private Integer quantity; + + @OneToMany(mappedBy="book", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + + public Book() { + } + public String getIsbn() { + return this.isbn; + } + public void setIsbn(String isbn) { + this.isbn = isbn; + } + public String getTitle() { + return this.title; + } + public void setTitle(String title) { + this.title = title; + } + public String getAuthor() { + return this.author; + } + public void setAuthor(String author) { + this.author = author; + } + + public Double getPrice() { + return this.price; + } + public void setPrice(Double price) { + this.price = price; + } + public Integer getQuantity() { + return this.quantity; + } + public void setQuantity(Integer quantity) { + this.quantity = quantity; + } +} diff --git a/ch4/TwoEjbs/PlaceOrder/src/ejbjpa/entities/Order.java b/ch4/TwoEjbs/PlaceOrder/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..9a3eace --- /dev/null +++ b/ch4/TwoEjbs/PlaceOrder/src/ejbjpa/entities/Order.java @@ -0,0 +1,53 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; + +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + @Id + @Column(name = "PONO") + private Integer pono; + @Column(name = "CUST_ID", nullable = false) + private Integer cust_id; + @Column(name = "UNITS", nullable = false) + private Integer units; + @ManyToOne + @JoinColumn( + name="BOOK_ID", + referencedColumnName="ISBN") + private Book book; + + public Order() { + } + public Integer getPono() { + return this.pono; + } + public void setPono(Integer pono) { + this.pono = pono; + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public Integer getUnits() { + return this.units; + } + public void setUnits(Integer units) { + this.units = units; + } + public Book getBook() { + return this.book; + } + public void setBook(Book book) { + this.book = book; + } + +} diff --git a/ch4/TwoEjbs/PlaceOrder/target/META-INF/ejb-jar.xml b/ch4/TwoEjbs/PlaceOrder/target/META-INF/ejb-jar.xml new file mode 100644 index 0000000..7c98a9a --- /dev/null +++ b/ch4/TwoEjbs/PlaceOrder/target/META-INF/ejb-jar.xml @@ -0,0 +1,24 @@ + + + + + OrderSessionBean + OrderSessionBean + ejbjpa.ejb.OrderSession + ejbjpa.ejb.OrderSessionBean + Stateless + Container + + ejbjpa.ejb.OrderSessionBean/emf + order-pu + + ejbjpa.ejb.OrderSessionBean + emf + + + + + + + + \ No newline at end of file diff --git a/ch4/TwoEjbs/PlaceOrder/target/META-INF/persistence.xml b/ch4/TwoEjbs/PlaceOrder/target/META-INF/persistence.xml new file mode 100644 index 0000000..a9c2a51 --- /dev/null +++ b/ch4/TwoEjbs/PlaceOrder/target/META-INF/persistence.xml @@ -0,0 +1,12 @@ + + + + jdbc/MySQL +order.jar +ejbjpa.entities.Order +ejbjpa.entities.Book + + + diff --git a/ch4/TwoEjbs/PlaceOrder/target/ejbjpa/ejb/OrderSession.class b/ch4/TwoEjbs/PlaceOrder/target/ejbjpa/ejb/OrderSession.class new file mode 100644 index 0000000..8227348 Binary files /dev/null and b/ch4/TwoEjbs/PlaceOrder/target/ejbjpa/ejb/OrderSession.class differ diff --git a/ch4/TwoEjbs/PlaceOrder/target/ejbjpa/ejb/OrderSessionBean.class b/ch4/TwoEjbs/PlaceOrder/target/ejbjpa/ejb/OrderSessionBean.class new file mode 100644 index 0000000..a76f48b Binary files /dev/null and b/ch4/TwoEjbs/PlaceOrder/target/ejbjpa/ejb/OrderSessionBean.class differ diff --git a/ch4/TwoEjbs/PlaceOrder/target/ejbjpa/entities/Book.class b/ch4/TwoEjbs/PlaceOrder/target/ejbjpa/entities/Book.class new file mode 100644 index 0000000..9ef41e7 Binary files /dev/null and b/ch4/TwoEjbs/PlaceOrder/target/ejbjpa/entities/Book.class differ diff --git a/ch4/TwoEjbs/PlaceOrder/target/ejbjpa/entities/Order.class b/ch4/TwoEjbs/PlaceOrder/target/ejbjpa/entities/Order.class new file mode 100644 index 0000000..c7d5b40 Binary files /dev/null and b/ch4/TwoEjbs/PlaceOrder/target/ejbjpa/entities/Order.class differ diff --git a/ch4/TwoEjbs/dist/deployment-descriptors.jar b/ch4/TwoEjbs/dist/deployment-descriptors.jar new file mode 100644 index 0000000..5ba299c Binary files /dev/null and b/ch4/TwoEjbs/dist/deployment-descriptors.jar differ diff --git a/ch4/TwoEjbs/dist/deployment-descs.jar b/ch4/TwoEjbs/dist/deployment-descs.jar new file mode 100644 index 0000000..ed1135f Binary files /dev/null and b/ch4/TwoEjbs/dist/deployment-descs.jar differ diff --git a/ch4/TwoEjbs/dist/two-ejbs.jar b/ch4/TwoEjbs/dist/two-ejbs.jar new file mode 100644 index 0000000..9d10abd Binary files /dev/null and b/ch4/TwoEjbs/dist/two-ejbs.jar differ diff --git a/ch4/TwoEjbs/target/META-INF/application.xml b/ch4/TwoEjbs/target/META-INF/application.xml new file mode 100644 index 0000000..2fe9a88 --- /dev/null +++ b/ch4/TwoEjbs/target/META-INF/application.xml @@ -0,0 +1,13 @@ + + + two-ejbs + + order.jar + + + cust.jar + + + appclient-client.jar + + \ No newline at end of file diff --git a/ch4/TwoEjbs/target/appclient-client.jar b/ch4/TwoEjbs/target/appclient-client.jar new file mode 100644 index 0000000..8e32379 Binary files /dev/null and b/ch4/TwoEjbs/target/appclient-client.jar differ diff --git a/ch4/TwoEjbs/target/cust.jar b/ch4/TwoEjbs/target/cust.jar new file mode 100644 index 0000000..29866aa Binary files /dev/null and b/ch4/TwoEjbs/target/cust.jar differ diff --git a/ch4/TwoEjbs/target/order.jar b/ch4/TwoEjbs/target/order.jar new file mode 100644 index 0000000..c0d8a5c Binary files /dev/null and b/ch4/TwoEjbs/target/order.jar differ diff --git a/ch5/GettingQuantity/AppClient/dist/appclient-book.jar b/ch5/GettingQuantity/AppClient/dist/appclient-book.jar new file mode 100644 index 0000000..f50a835 Binary files /dev/null and b/ch5/GettingQuantity/AppClient/dist/appclient-book.jar differ diff --git a/ch5/GettingQuantity/AppClient/src/ejbjpa/client/BookSessionClient.java b/ch5/GettingQuantity/AppClient/src/ejbjpa/client/BookSessionClient.java new file mode 100644 index 0000000..f88e5b8 --- /dev/null +++ b/ch5/GettingQuantity/AppClient/src/ejbjpa/client/BookSessionClient.java @@ -0,0 +1,12 @@ +package ejbjpa.client; +import javax.ejb.EJB; +import ejbjpa.ejb.BookSession; +public class BookSessionClient { + @EJB + private static BookSession bookSession; + public static void main (String[] args) + { + String book_id = args[0]; // = "1430209631"; + System.out.println(bookSession.gettingQuantity(book_id)); + } +} \ No newline at end of file diff --git a/ch5/GettingQuantity/AppClient/target/META-INF/MANIFEST.MF b/ch5/GettingQuantity/AppClient/target/META-INF/MANIFEST.MF new file mode 100644 index 0000000..ea84eb4 --- /dev/null +++ b/ch5/GettingQuantity/AppClient/target/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: ejbjpa.client.BookSessionClient + diff --git a/ch5/GettingQuantity/AppClient/target/ejbjpa/client/BookSessionClient.class b/ch5/GettingQuantity/AppClient/target/ejbjpa/client/BookSessionClient.class new file mode 100644 index 0000000..a5510ae Binary files /dev/null and b/ch5/GettingQuantity/AppClient/target/ejbjpa/client/BookSessionClient.class differ diff --git a/ch5/GettingQuantity/GetQuantity/dist/book.jar b/ch5/GettingQuantity/GetQuantity/dist/book.jar new file mode 100644 index 0000000..4b66855 Binary files /dev/null and b/ch5/GettingQuantity/GetQuantity/dist/book.jar differ diff --git a/ch5/GettingQuantity/GetQuantity/src/ejbjpa/ejb/BookSession.java b/ch5/GettingQuantity/GetQuantity/src/ejbjpa/ejb/BookSession.java new file mode 100644 index 0000000..64e6b51 --- /dev/null +++ b/ch5/GettingQuantity/GetQuantity/src/ejbjpa/ejb/BookSession.java @@ -0,0 +1,6 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +@Remote +public interface BookSession { + public Integer gettingQuantity(String book_id); +} diff --git a/ch5/GettingQuantity/GetQuantity/src/ejbjpa/ejb/BookSessionBean.java b/ch5/GettingQuantity/GetQuantity/src/ejbjpa/ejb/BookSessionBean.java new file mode 100644 index 0000000..fa8e7fd --- /dev/null +++ b/ch5/GettingQuantity/GetQuantity/src/ejbjpa/ejb/BookSessionBean.java @@ -0,0 +1,31 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.annotation.Resource; +import javax.sql.DataSource; +import java.sql.*; + +@Stateless +public class BookSessionBean implements BookSession { + @Resource(name="jdbc/MySQL") + private DataSource dataSource; + public Integer gettingQuantity(String book_id) + { + Integer qnty; + try { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("SELECT quantity FROM books WHERE isbn= ?"); + stmt.setString(1, book_id); + ResultSet rslt = stmt.executeQuery(); + rslt.next(); + qnty = rslt.getInt("quantity"); + rslt.close(); + stmt.close(); + conn.close(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return qnty; + } +} \ No newline at end of file diff --git a/ch5/GettingQuantity/GetQuantity/target/ejbjpa/ejb/BookSession.class b/ch5/GettingQuantity/GetQuantity/target/ejbjpa/ejb/BookSession.class new file mode 100644 index 0000000..70dc6f3 Binary files /dev/null and b/ch5/GettingQuantity/GetQuantity/target/ejbjpa/ejb/BookSession.class differ diff --git a/ch5/GettingQuantity/GetQuantity/target/ejbjpa/ejb/BookSessionBean.class b/ch5/GettingQuantity/GetQuantity/target/ejbjpa/ejb/BookSessionBean.class new file mode 100644 index 0000000..6126c74 Binary files /dev/null and b/ch5/GettingQuantity/GetQuantity/target/ejbjpa/ejb/BookSessionBean.class differ diff --git a/ch5/GettingQuantity/dist/getting-quantity.jar b/ch5/GettingQuantity/dist/getting-quantity.jar new file mode 100644 index 0000000..d1ae5f3 Binary files /dev/null and b/ch5/GettingQuantity/dist/getting-quantity.jar differ diff --git a/ch5/GettingQuantity/target/META-INF/application.xml b/ch5/GettingQuantity/target/META-INF/application.xml new file mode 100644 index 0000000..6818457 --- /dev/null +++ b/ch5/GettingQuantity/target/META-INF/application.xml @@ -0,0 +1,10 @@ + + + getting-quantity + + book.jar + + + appclient-book.jar + + diff --git a/ch5/GettingQuantity/target/appclient-book.jar b/ch5/GettingQuantity/target/appclient-book.jar new file mode 100644 index 0000000..f50a835 Binary files /dev/null and b/ch5/GettingQuantity/target/appclient-book.jar differ diff --git a/ch5/GettingQuantity/target/book.jar b/ch5/GettingQuantity/target/book.jar new file mode 100644 index 0000000..4b66855 Binary files /dev/null and b/ch5/GettingQuantity/target/book.jar differ diff --git a/ch5/ProcessingToData/AppClient/dist/appclient-order.jar b/ch5/ProcessingToData/AppClient/dist/appclient-order.jar new file mode 100644 index 0000000..59863eb Binary files /dev/null and b/ch5/ProcessingToData/AppClient/dist/appclient-order.jar differ diff --git a/ch5/ProcessingToData/AppClient/src/ejbjpa/client/OrderSessionClient.java b/ch5/ProcessingToData/AppClient/src/ejbjpa/client/OrderSessionClient.java new file mode 100644 index 0000000..69aff36 --- /dev/null +++ b/ch5/ProcessingToData/AppClient/src/ejbjpa/client/OrderSessionClient.java @@ -0,0 +1,15 @@ +package ejbjpa.client; +import javax.ejb.EJB; +import ejbjpa.ejb.OrderSession; +public class OrderSessionClient { + @EJB + private static OrderSession orderSession; + public static void main (String[] args) + { + Integer pono = Integer.parseInt(args[0]); //= 7; + Integer cust_id = Integer.parseInt(args[1]); // = 1; + Integer units = Integer.parseInt(args[2]); // = 1; + String book_id = args[3]; // = "1430209631"; + orderSession.placeOrder(pono, cust_id, units, book_id); + } +} \ No newline at end of file diff --git a/ch5/ProcessingToData/AppClient/target/META-INF/MANIFEST.MF b/ch5/ProcessingToData/AppClient/target/META-INF/MANIFEST.MF new file mode 100644 index 0000000..245bfc1 --- /dev/null +++ b/ch5/ProcessingToData/AppClient/target/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: ejbjpa.client.OrderSessionClient + diff --git a/ch5/ProcessingToData/AppClient/target/ejbjpa/client/OrderSessionClient.class b/ch5/ProcessingToData/AppClient/target/ejbjpa/client/OrderSessionClient.class new file mode 100644 index 0000000..568a70b Binary files /dev/null and b/ch5/ProcessingToData/AppClient/target/ejbjpa/client/OrderSessionClient.class differ diff --git a/ch5/ProcessingToData/PlaceOrder/dist/order.jar b/ch5/ProcessingToData/PlaceOrder/dist/order.jar new file mode 100644 index 0000000..6915a4c Binary files /dev/null and b/ch5/ProcessingToData/PlaceOrder/dist/order.jar differ diff --git a/ch5/ProcessingToData/PlaceOrder/src/ejbjpa/ejb/OrderSession.java b/ch5/ProcessingToData/PlaceOrder/src/ejbjpa/ejb/OrderSession.java new file mode 100644 index 0000000..8578fe3 --- /dev/null +++ b/ch5/ProcessingToData/PlaceOrder/src/ejbjpa/ejb/OrderSession.java @@ -0,0 +1,9 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +@Remote +public interface OrderSession { + public void placeOrder(Integer pono, + Integer cust_id, + Integer units, + String book_id); +} diff --git a/ch5/ProcessingToData/PlaceOrder/src/ejbjpa/ejb/OrderSessionBean.java b/ch5/ProcessingToData/PlaceOrder/src/ejbjpa/ejb/OrderSessionBean.java new file mode 100644 index 0000000..0450718 --- /dev/null +++ b/ch5/ProcessingToData/PlaceOrder/src/ejbjpa/ejb/OrderSessionBean.java @@ -0,0 +1,34 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; +import ejbjpa.entities.*; + +@Stateless +public class OrderSessionBean implements OrderSession { + @PersistenceUnit(unitName = "order-pu") + private EntityManagerFactory emf; + public void placeOrder(Integer pono, + Integer cust_id, + Integer units, + String book_id) + { + try { + EntityManager em = emf.createEntityManager(); + // Book book = (Book) em.find(Book.class, book_id); + Order order = new Order(); + order.setPono(pono); + order.setCust_id(cust_id); + order.setBook_id(book_id); + order.setUnits(units); + // book.setQuantity(book.getQuantity()-units); + //order.setBook(book); + em.persist(order); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/ch5/ProcessingToData/PlaceOrder/src/ejbjpa/entities/Order.java b/ch5/ProcessingToData/PlaceOrder/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..f18c7a4 --- /dev/null +++ b/ch5/ProcessingToData/PlaceOrder/src/ejbjpa/entities/Order.java @@ -0,0 +1,50 @@ +package ejbjpa.entities; + +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; + +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + @Id + @Column(name = "PONO") + private Integer pono; + @Column(name = "CUST_ID", nullable = false) + private Integer cust_id; + @Column(name = "BOOK_ID", nullable = false) + private String book_id; + @Column(name = "UNITS", nullable = false) + private Integer units; + public Order() { + } + public Integer getPono() { + return this.pono; + } + public void setPono(Integer pono) { + this.pono = pono; + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getBook_id() { + return this.book_id; + } + public void setBook_id(String book_id) { + this.book_id = book_id; + } + public Integer getUnits() { + return this.units; + } + public void setUnits(Integer units) { + this.units = units; + } + +} diff --git a/ch5/ProcessingToData/PlaceOrder/target/META-INF/persistence.xml b/ch5/ProcessingToData/PlaceOrder/target/META-INF/persistence.xml new file mode 100644 index 0000000..12080c9 --- /dev/null +++ b/ch5/ProcessingToData/PlaceOrder/target/META-INF/persistence.xml @@ -0,0 +1,12 @@ + + + + jdbc/MySQL +order.jar +ejbjpa.entities.Order + + + + diff --git a/ch5/ProcessingToData/PlaceOrder/target/ejbjpa/ejb/OrderSession.class b/ch5/ProcessingToData/PlaceOrder/target/ejbjpa/ejb/OrderSession.class new file mode 100644 index 0000000..b19ddac Binary files /dev/null and b/ch5/ProcessingToData/PlaceOrder/target/ejbjpa/ejb/OrderSession.class differ diff --git a/ch5/ProcessingToData/PlaceOrder/target/ejbjpa/ejb/OrderSessionBean.class b/ch5/ProcessingToData/PlaceOrder/target/ejbjpa/ejb/OrderSessionBean.class new file mode 100644 index 0000000..e8af08d Binary files /dev/null and b/ch5/ProcessingToData/PlaceOrder/target/ejbjpa/ejb/OrderSessionBean.class differ diff --git a/ch5/ProcessingToData/PlaceOrder/target/ejbjpa/entities/Order.class b/ch5/ProcessingToData/PlaceOrder/target/ejbjpa/entities/Order.class new file mode 100644 index 0000000..bffce88 Binary files /dev/null and b/ch5/ProcessingToData/PlaceOrder/target/ejbjpa/entities/Order.class differ diff --git a/ch5/ProcessingToData/dist/processing-todata.jar b/ch5/ProcessingToData/dist/processing-todata.jar new file mode 100644 index 0000000..23f5e99 Binary files /dev/null and b/ch5/ProcessingToData/dist/processing-todata.jar differ diff --git a/ch5/ProcessingToData/target/META-INF/application.xml b/ch5/ProcessingToData/target/META-INF/application.xml new file mode 100644 index 0000000..c3280d3 --- /dev/null +++ b/ch5/ProcessingToData/target/META-INF/application.xml @@ -0,0 +1,10 @@ + + + transactions-planning + + order.jar + + + appclient-order.jar + + diff --git a/ch5/ProcessingToData/target/appclient-order.jar b/ch5/ProcessingToData/target/appclient-order.jar new file mode 100644 index 0000000..59863eb Binary files /dev/null and b/ch5/ProcessingToData/target/appclient-order.jar differ diff --git a/ch5/ProcessingToData/target/order.jar b/ch5/ProcessingToData/target/order.jar new file mode 100644 index 0000000..6915a4c Binary files /dev/null and b/ch5/ProcessingToData/target/order.jar differ diff --git a/ch5/QuantityViaFunction/AppClient/dist/appclient-book.jar b/ch5/QuantityViaFunction/AppClient/dist/appclient-book.jar new file mode 100644 index 0000000..f50a835 Binary files /dev/null and b/ch5/QuantityViaFunction/AppClient/dist/appclient-book.jar differ diff --git a/ch5/QuantityViaFunction/AppClient/src/ejbjpa/client/BookSessionClient.java b/ch5/QuantityViaFunction/AppClient/src/ejbjpa/client/BookSessionClient.java new file mode 100644 index 0000000..f88e5b8 --- /dev/null +++ b/ch5/QuantityViaFunction/AppClient/src/ejbjpa/client/BookSessionClient.java @@ -0,0 +1,12 @@ +package ejbjpa.client; +import javax.ejb.EJB; +import ejbjpa.ejb.BookSession; +public class BookSessionClient { + @EJB + private static BookSession bookSession; + public static void main (String[] args) + { + String book_id = args[0]; // = "1430209631"; + System.out.println(bookSession.gettingQuantity(book_id)); + } +} \ No newline at end of file diff --git a/ch5/QuantityViaFunction/AppClient/target/META-INF/MANIFEST.MF b/ch5/QuantityViaFunction/AppClient/target/META-INF/MANIFEST.MF new file mode 100644 index 0000000..ea84eb4 --- /dev/null +++ b/ch5/QuantityViaFunction/AppClient/target/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: ejbjpa.client.BookSessionClient + diff --git a/ch5/QuantityViaFunction/AppClient/target/ejbjpa/client/BookSessionClient.class b/ch5/QuantityViaFunction/AppClient/target/ejbjpa/client/BookSessionClient.class new file mode 100644 index 0000000..a5510ae Binary files /dev/null and b/ch5/QuantityViaFunction/AppClient/target/ejbjpa/client/BookSessionClient.class differ diff --git a/ch5/QuantityViaFunction/GetQuantity/dist/book.jar b/ch5/QuantityViaFunction/GetQuantity/dist/book.jar new file mode 100644 index 0000000..a71cee1 Binary files /dev/null and b/ch5/QuantityViaFunction/GetQuantity/dist/book.jar differ diff --git a/ch5/QuantityViaFunction/GetQuantity/src/ejbjpa/ejb/BookSession.java b/ch5/QuantityViaFunction/GetQuantity/src/ejbjpa/ejb/BookSession.java new file mode 100644 index 0000000..64e6b51 --- /dev/null +++ b/ch5/QuantityViaFunction/GetQuantity/src/ejbjpa/ejb/BookSession.java @@ -0,0 +1,6 @@ +package ejbjpa.ejb; +import javax.ejb.Remote; +@Remote +public interface BookSession { + public Integer gettingQuantity(String book_id); +} diff --git a/ch5/QuantityViaFunction/GetQuantity/src/ejbjpa/ejb/BookSessionBean.java b/ch5/QuantityViaFunction/GetQuantity/src/ejbjpa/ejb/BookSessionBean.java new file mode 100644 index 0000000..904281e --- /dev/null +++ b/ch5/QuantityViaFunction/GetQuantity/src/ejbjpa/ejb/BookSessionBean.java @@ -0,0 +1,31 @@ +package ejbjpa.ejb; +import java.io.Serializable; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.annotation.Resource; +import javax.sql.DataSource; +import java.sql.*; + +@Stateless +public class BookSessionBean implements BookSession { + @Resource(name="jdbc/MySQL") + private DataSource dataSource; + public Integer gettingQuantity(String book_id) + { + Integer qnty; + try { + Connection conn = dataSource.getConnection(); + PreparedStatement stmt = conn.prepareStatement("SELECT copiesInStock(?) as quantity FROM DUAL"); + stmt.setString(1, book_id); + ResultSet rslt = stmt.executeQuery(); + rslt.next(); + qnty = rslt.getInt("quantity"); + rslt.close(); + stmt.close(); + conn.close(); + } catch (Exception e) { + throw new EJBException(e.getMessage()); + } + return qnty; + } +} \ No newline at end of file diff --git a/ch5/QuantityViaFunction/GetQuantity/target/ejbjpa/ejb/BookSession.class b/ch5/QuantityViaFunction/GetQuantity/target/ejbjpa/ejb/BookSession.class new file mode 100644 index 0000000..70dc6f3 Binary files /dev/null and b/ch5/QuantityViaFunction/GetQuantity/target/ejbjpa/ejb/BookSession.class differ diff --git a/ch5/QuantityViaFunction/GetQuantity/target/ejbjpa/ejb/BookSessionBean.class b/ch5/QuantityViaFunction/GetQuantity/target/ejbjpa/ejb/BookSessionBean.class new file mode 100644 index 0000000..64ee40e Binary files /dev/null and b/ch5/QuantityViaFunction/GetQuantity/target/ejbjpa/ejb/BookSessionBean.class differ diff --git a/ch5/QuantityViaFunction/dist/quantity-function.jar b/ch5/QuantityViaFunction/dist/quantity-function.jar new file mode 100644 index 0000000..a8d0cf0 Binary files /dev/null and b/ch5/QuantityViaFunction/dist/quantity-function.jar differ diff --git a/ch5/QuantityViaFunction/target/META-INF/application.xml b/ch5/QuantityViaFunction/target/META-INF/application.xml new file mode 100644 index 0000000..6818457 --- /dev/null +++ b/ch5/QuantityViaFunction/target/META-INF/application.xml @@ -0,0 +1,10 @@ + + + getting-quantity + + book.jar + + + appclient-book.jar + + diff --git a/ch5/QuantityViaFunction/target/appclient-book.jar b/ch5/QuantityViaFunction/target/appclient-book.jar new file mode 100644 index 0000000..f50a835 Binary files /dev/null and b/ch5/QuantityViaFunction/target/appclient-book.jar differ diff --git a/ch5/QuantityViaFunction/target/book.jar b/ch5/QuantityViaFunction/target/book.jar new file mode 100644 index 0000000..a71cee1 Binary files /dev/null and b/ch5/QuantityViaFunction/target/book.jar differ diff --git a/ch6/deliveryestimatemysql.sql b/ch6/deliveryestimatemysql.sql new file mode 100644 index 0000000..e536c88 --- /dev/null +++ b/ch6/deliveryestimatemysql.sql @@ -0,0 +1,23 @@ +USE dbsample; + +delimiter // +CREATE FUNCTION deliveryEstimate(location_id INT) RETURNS VARCHAR(20) +BEGIN + DECLARE estimate VARCHAR(20); + DECLARE location VARCHAR(100); + SELECT area INTO location FROM locations WHERE loc_id = location_id; + CASE + WHEN location = 'US' THEN + SET estimate = '2 business days'; + WHEN location = 'Canada' THEN + SET estimate = '2-4 business days'; +WHEN location = 'Europe' THEN + SET estimate = '4-6 business days'; +ELSE + SET estimate = '8-12 business days'; + END CASE; + RETURN estimate; +END; +// +delimiter ; + diff --git a/ch6/deliveryestimateoracle.sql b/ch6/deliveryestimateoracle.sql new file mode 100644 index 0000000..61b7290 --- /dev/null +++ b/ch6/deliveryestimateoracle.sql @@ -0,0 +1,19 @@ +CREATE OR REPLACE FUNCTION deliveryEstimate(location_id NUMBER) RETURN VARCHAR2 AS + estimate VARCHAR2(20); + location VARCHAR2(100); +BEGIN + SELECT area INTO location FROM locations WHERE loc_id = location_id; + estimate := + CASE + WHEN location = 'US' THEN + '2 business days' + WHEN location = 'Canada' THEN + '2-4 business days' + WHEN location = 'Europe' THEN + '4-6 business days' + ELSE + '8-12 business days' + END; + RETURN estimate; +END; +/ diff --git a/ch6/shippingdatemysql.sql b/ch6/shippingdatemysql.sql new file mode 100644 index 0000000..500ca28 --- /dev/null +++ b/ch6/shippingdatemysql.sql @@ -0,0 +1,19 @@ +USE dbsample; + +delimiter // +CREATE FUNCTION shippingDate() RETURNS DATE +BEGIN + DECLARE dayno INT; + DECLARE shpdate DATE; + SELECT WEEKDAY(NOW()) INTO dayno; + IF dayno < 5 THEN + SELECT CURDATE() INTO shpdate; + ELSEIF dayno = 5 THEN + SELECT DATE_ADD(CURDATE(), INTERVAL 2 DAY) INTO shpdate; + ELSEIF dayno = 6 THEN + SELECT DATE_ADD(CURDATE(), INTERVAL 1 DAY) INTO shpdate; + END IF; + RETURN shpdate; +END; +// +delimiter ; diff --git a/ch6/shippingdateoracle.sql b/ch6/shippingdateoracle.sql new file mode 100644 index 0000000..a083fdb --- /dev/null +++ b/ch6/shippingdateoracle.sql @@ -0,0 +1,13 @@ +CREATE OR REPLACE FUNCTION shippingDate RETURN DATE IS + dayno NUMBER; + shpdate DATE; + BEGIN + SELECT TO_NUMBER(TO_CHAR(SYSDATE, 'D')) INTO dayno FROM DUAL; + IF (dayno>1) AND (dayno<7) THEN + SELECT SYSDATE INTO shpdate FROM DUAL; + ELSE + SELECT NEXT_DAY(SYSDATE, 'MONDAY') INTO shpdate FROM DUAL; + END IF; + RETURN shpdate; +END; +/ \ No newline at end of file diff --git a/ch6/tablesmysql.sql b/ch6/tablesmysql.sql new file mode 100644 index 0000000..8af1a9e --- /dev/null +++ b/ch6/tablesmysql.sql @@ -0,0 +1,72 @@ +USE dbsample; + +DROP TABLE IF EXISTS details; +DROP TABLE IF EXISTS shoppingCarts; +DROP TABLE IF EXISTS orders; +DROP TABLE IF EXISTS customers; +DROP TABLE IF EXISTS locations; +DROP TABLE IF EXISTS employees; +DROP TABLE IF EXISTS books; + +CREATE TABLE locations ( + loc_id INTEGER PRIMARY KEY, + area VARCHAR(100) +) +ENGINE = InnoDB; + +CREATE TABLE customers ( + cust_id INTEGER PRIMARY KEY, + cust_name VARCHAR(100), + loc_id INTEGER, + email VARCHAR(100), + phone VARCHAR(20), + FOREIGN KEY(loc_id) REFERENCES locations(loc_id) +) +ENGINE = InnoDB; + +CREATE TABLE employees( + empno INTEGER PRIMARY KEY, + firstname VARCHAR(30), + lastname VARCHAR(30) +) +ENGINE = InnoDB; + +CREATE TABLE books( + isbn VARCHAR(20) PRIMARY KEY, + title VARCHAR(150), + author VARCHAR(150), + quantity INTEGER, + price NUMERIC(8,2) +) +ENGINE = InnoDB; + +CREATE TABLE orders( + pono INTEGER PRIMARY KEY, + cust_id INTEGER, + empno INTEGER, + shipping_date DATE, + delivery_estimate VARCHAR(20), + FOREIGN KEY(cust_id) REFERENCES customers(cust_id), + FOREIGN KEY(empno) REFERENCES employees(empno) +) +ENGINE = InnoDB; + +CREATE TABLE details( + ordno INTEGER, + book_id VARCHAR(20), + units INTEGER, + unit_price NUMERIC(8,2), + PRIMARY KEY(ordno, book_id), + FOREIGN KEY(ordno) REFERENCES orders(pono), + FOREIGN KEY(book_id) REFERENCES books(isbn) +) ENGINE = InnoDB; + +CREATE TABLE shoppingCarts( + cart_id INTEGER, + book_id VARCHAR(20), + units INTEGER, + unit_price NUMERIC(8,2), + PRIMARY KEY(cart_id, book_id), + FOREIGN KEY(book_id) REFERENCES books(isbn) +) ENGINE = InnoDB; + diff --git a/ch6/tablesoracle.sql b/ch6/tablesoracle.sql new file mode 100644 index 0000000..adfad5f --- /dev/null +++ b/ch6/tablesoracle.sql @@ -0,0 +1,64 @@ +DROP TABLE details; +DROP TABLE shoppingCarts; +DROP TABLE orders; +DROP TABLE customers; +DROP TABLE locations; +DROP TABLE employees; +DROP TABLE books; + +CREATE TABLE locations ( + loc_id NUMBER PRIMARY KEY, + area VARCHAR2(100) +); + +CREATE TABLE customers ( + cust_id NUMBER PRIMARY KEY, + cust_name VARCHAR2(100), + loc_id NUMBER, + email VARCHAR2(100), + phone VARCHAR2(20), + FOREIGN KEY(loc_id) REFERENCES locations(loc_id) +); + +CREATE TABLE employees( + empno NUMBER PRIMARY KEY, + firstname VARCHAR2(30), + lastname VARCHAR2(30) +); + +CREATE TABLE books( + isbn VARCHAR2(20) PRIMARY KEY, + title VARCHAR2(150), + author VARCHAR2(150), + quantity NUMBER, + price NUMBER(8,2) +); + +CREATE TABLE orders( + pono NUMBER PRIMARY KEY, + cust_id NUMBER, + empno NUMBER, + shipping_date DATE, + delivery_estimate VARCHAR2(20), + FOREIGN KEY(cust_id) REFERENCES customers(cust_id), + FOREIGN KEY(empno) REFERENCES employees(empno) +); + +CREATE TABLE details( + ordno NUMBER, + book_id VARCHAR2(20), + units NUMBER, + unit_price NUMBER(8,2), + PRIMARY KEY(ordno, book_id), + FOREIGN KEY(ordno) REFERENCES orders(pono), + FOREIGN KEY(book_id) REFERENCES books(isbn) +); + +CREATE TABLE shoppingCarts( + cart_id NUMBER, + book_id VARCHAR2(20), + units NUMBER, + unit_price NUMBER(8,2), + PRIMARY KEY(cart_id, book_id), + FOREIGN KEY(book_id) REFERENCES books(isbn) +); diff --git a/ch6/tablespopulatingmysql.sql b/ch6/tablespopulatingmysql.sql new file mode 100644 index 0000000..8a2026f --- /dev/null +++ b/ch6/tablespopulatingmysql.sql @@ -0,0 +1,19 @@ +USE dbsample; + +START TRANSACTION; + +INSERT INTO locations VALUES(1, 'US'); +INSERT INTO locations VALUES(2, 'Canada'); +INSERT INTO locations VALUES(3, 'Europe'); +INSERT INTO locations VALUES(4, 'Other'); + +INSERT INTO customers VALUES(1, 'John Poplavski', 1, 'joshp@mail.com', '(650)777-5665'); +INSERT INTO customers VALUES(2, 'Paul Medica', 3, 'paulmed@mail.com', '(029)2124-5540'); + +INSERT INTO employees VALUES(1, 'Serg', 'Oganovich'); +INSERT INTO employees VALUES(2, 'Maya', 'Silver'); + +INSERT INTO books VALUES('1430209631', 'Beginning GlassFish TopLink: From Novice to Professional', 'Yuli Vasiliev', 10, 44.99); +INSERT INTO books VALUES('1590595300', 'Expert Oracle Databas Architecture: 9i and 10g Programming Techniques and Solutions', 'Thomas Kyte', 10, 49.99); + +COMMIT; diff --git a/ch6/tablespopulatingoracle.sql b/ch6/tablespopulatingoracle.sql new file mode 100644 index 0000000..d9bc063 --- /dev/null +++ b/ch6/tablespopulatingoracle.sql @@ -0,0 +1,15 @@ +INSERT INTO locations VALUES(1, 'US'); +INSERT INTO locations VALUES(2, 'Canada'); +INSERT INTO locations VALUES(3, 'Europe'); +INSERT INTO locations VALUES(4, 'Other'); + +INSERT INTO customers VALUES(1, 'John Poplavski', 1, 'joshp@mail.com', '(650)777-5665'); +INSERT INTO customers VALUES(2, 'Paul Medica', 3, 'paulmed@mail.com', '(029)2124-5540'); + +INSERT INTO employees VALUES(1, 'Serg', 'Oganovich'); +INSERT INTO employees VALUES(2, 'Maya', 'Silver'); + +INSERT INTO books VALUES('1430209631', 'Beginning GlassFish TopLink: From Novice to Professional', 'Yuli Vasiliev', 10, 44.99); +INSERT INTO books VALUES('1590595300', 'Expert Oracle Databas Architecture: 9i and 10g Programming Techniques and Solutions', 'Thomas Kyte', 10, 49.99); + +COMMIT; diff --git a/ch7/TestDataSource/WEB-INF/sun-web.xml b/ch7/TestDataSource/WEB-INF/sun-web.xml new file mode 100644 index 0000000..0b3eb44 --- /dev/null +++ b/ch7/TestDataSource/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /testPage + \ No newline at end of file diff --git a/ch7/TestDataSource/WEB-INF/web.xml b/ch7/TestDataSource/WEB-INF/web.xml new file mode 100644 index 0000000..fc8ffba --- /dev/null +++ b/ch7/TestDataSource/WEB-INF/web.xml @@ -0,0 +1,17 @@ + + + + + testPage.jsp + + + + jdbc/mysqlpool + javax.sql.DataSource + Container + + \ No newline at end of file diff --git a/ch7/TestDataSource/testPage.jsp b/ch7/TestDataSource/testPage.jsp new file mode 100644 index 0000000..037865e --- /dev/null +++ b/ch7/TestDataSource/testPage.jsp @@ -0,0 +1,23 @@ +<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> + + + select isbn, title, quantity, price from dbsample.books + + + + JDBC Connection Test + + +

Available books:

+ + + Isbn: ${book.isbn}
+ Title: ${book.title}
+ Price: $${book.price}
+ In stock: ${book.quantity}
+
+
+
+ + diff --git a/ch7/TestDataSource/testdb.war b/ch7/TestDataSource/testdb.war new file mode 100644 index 0000000..af83cdf Binary files /dev/null and b/ch7/TestDataSource/testdb.war differ diff --git a/ch8/entitiestest/src/ejbjpa/entities/Customer.java b/ch8/entitiestest/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..aa187c0 --- /dev/null +++ b/ch8/entitiestest/src/ejbjpa/entities/Customer.java @@ -0,0 +1,64 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "CUST_NAME", nullable = false) + private String cust_name; + @Column(name = "LOC_ID") + private Integer loc_id; + @Column(name = "EMAIL", nullable = false) + private String email; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToMany(mappedBy="customer", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCust_name() { + return this.cust_name; + } + public void setCust_name(String cust_name) { + this.cust_name = cust_name; + } + public Integer getLoc_id() { + return this.loc_id; + } + public void setLoc_id(Integer loc_id) { + this.loc_id = loc_id; + } + public String getEmail() { + return this.email; + } + public void setEmail(String email) { + this.email = email; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/ch8/entitiestest/src/ejbjpa/entities/Employee.java b/ch8/entitiestest/src/ejbjpa/entities/Employee.java new file mode 100644 index 0000000..9751c22 --- /dev/null +++ b/ch8/entitiestest/src/ejbjpa/entities/Employee.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "EMPLOYEES") +public class Employee implements Serializable { + @Id + @Column(name = "EMPNO") + private Integer empno; + @Column(name = "firstname", nullable = false) + private String firstname; + @Column(name = "lastname", nullable = false) + private String lastname; + @OneToMany(mappedBy="employee", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Employee() { + } + public Integer getEmpno() { + return this.empno; + } + public void setEmpno(Integer empno) { + this.empno = empno; + } + public String getFirstname() { + return this.firstname; + } + public void setFirstname(String firstname) { + this.firstname = firstname; + } + public String getLastname() { + return this.lastname; + } + public void setLastname(String lastname) { + this.lastname = lastname; + } +} diff --git a/ch8/entitiestest/src/ejbjpa/entities/Order.java b/ch8/entitiestest/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..c550cad --- /dev/null +++ b/ch8/entitiestest/src/ejbjpa/entities/Order.java @@ -0,0 +1,65 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import static javax.persistence.TemporalType.DATE; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import java.util.Date; +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + @Id + @Column(name = "PONO") + private Integer pono; + @Column(name = "SHIPPING_DATE", nullable = false) + @Temporal(DATE) + private Date shipping_date; + @Column(name = "DELIVERY_ESTIMATE", nullable = false) + private String delivery_estimate; + @ManyToOne + @JoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ID") + private Customer customer; + @ManyToOne + @JoinColumn( + name="EMPNO", + referencedColumnName="EMPNO") + private Employee employee; + public Order() { + } + public Customer getCustomer() { + return this.customer; + } + public void setCustomer(Customer customer) { + this.customer = customer; + } + public Employee getEmployee() { + return this.employee; + } + public void setEmployee (Employee employee) { + this.employee = employee; + } + public Integer getPono () { + return this.pono; + } + public void setPono (Integer pono) { + this.pono = pono; + } + public Date getShipping_date () { + return this.shipping_date; + } + public void setShipping_date (Date shipping_date) { + this.shipping_date = shipping_date; + } + public String getDelivery_estimate () { + return this.delivery_estimate; + } + public void setDelivery_estimate (String delivery_estimate) { + this.delivery_estimate = delivery_estimate; + } +} diff --git a/ch8/entitiestest/src/ejbjpa/entities/ShoppingCart.java b/ch8/entitiestest/src/ejbjpa/entities/ShoppingCart.java new file mode 100644 index 0000000..7d169f2 --- /dev/null +++ b/ch8/entitiestest/src/ejbjpa/entities/ShoppingCart.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.IdClass; +import javax.persistence.Table; +@Entity +@Table(name = "SHOPPINGCARTS") +@IdClass(value = ejbjpa.entities.ShoppingCartKey.class) +public class ShoppingCart implements Serializable { + @Id + @Column(name = "CART_ID") + private Integer cart_id; + @Id + @Column(name = "BOOK_ID") + private String book_id; + @Column(name = "UNITS", nullable = false) + private Integer units; + @Column(name = "UNIT_PRICE", nullable = false) + private Double unit_price; + public ShoppingCart() { + } + public Integer getCart_id() { + return this.cart_id; + } + public void setCart_id(Integer cart_id) { + this.cart_id = cart_id; + } + public String getBook_id() { + return this.book_id; + } + public void setBook_id(String book_id) { + this.book_id = book_id; + } + public Integer getUnits () { + return this.units; + } + public void setUnits(Integer units) { + this.units = units; + } + public Double getUnit_price() { + return this.unit_price; + } + public void setUnit_price(Double unit_price) { + this.unit_price = unit_price; + } +} diff --git a/ch8/entitiestest/src/ejbjpa/entities/ShoppingCartKey.java b/ch8/entitiestest/src/ejbjpa/entities/ShoppingCartKey.java new file mode 100644 index 0000000..5c29dbe --- /dev/null +++ b/ch8/entitiestest/src/ejbjpa/entities/ShoppingCartKey.java @@ -0,0 +1,37 @@ +package ejbjpa.entities; +import java.io.Serializable; + +public final class ShoppingCartKey implements Serializable { + public Integer cart_id; + public String book_id; + public ShoppingCartKey() {} + public ShoppingCartKey(Integer cart_id, String book_id) { + this.cart_id = cart_id; + this.book_id = book_id; + } + +public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof ShoppingCartKey)) { + return false; + } + ShoppingCartKey other = (ShoppingCartKey) obj; + if (cart_id != null && other.cart_id!= null && this.cart_id.equals(other.cart_id)) { + return (book_id != null && other.book_id!= null && this.book_id.equals(other.book_id)); + } + return false; +} + +public int hashCode() { + if (cart_id!=null && book_id!=null) { + return (cart_id.hashCode() ^ book_id.hashCode()); + } + return 0; +} + +} \ No newline at end of file diff --git a/ch8/entitiestest/src/ejbjpa/servlets/JpaTestServlet.java b/ch8/entitiestest/src/ejbjpa/servlets/JpaTestServlet.java new file mode 100644 index 0000000..0616a2b --- /dev/null +++ b/ch8/entitiestest/src/ejbjpa/servlets/JpaTestServlet.java @@ -0,0 +1,86 @@ +package ejbjpa.servlets; +import java.io.*; +import java.util.*; +import java.sql.*; +import javax.servlet.*; +import javax.servlet.http.*; +import javax.transaction.*; +import javax.annotation.Resource; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; +import ejbjpa.entities.*; +public class JpaTestServlet extends HttpServlet { + @PersistenceUnit + private EntityManagerFactory emf; + @Resource + private UserTransaction userTransaction; + public void doGet( + HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + response.setBufferSize(8192); + PrintWriter out = response.getWriter(); + EntityManager em= emf.createEntityManager(); + int status; + //creating ShoppingCart entity instances + ShoppingCart cart1 = new ShoppingCart(); + cart1.setCart_id(2); + cart1.setBook_id("1590595300"); + cart1.setUnits(1); + cart1.setUnit_price(49.99); + ShoppingCart cart2 = new ShoppingCart(); + cart2.setCart_id(2); + cart2.setBook_id("1430209631"); + cart2.setUnits(1); + cart2.setUnit_price(44.99); + //Creating the order entity instance that will convert the above ShoppingCarts into the order's details + Customer cust1 = (Customer) em.find(Customer.class, 2); + Employee emp1 = (Employee) em.find(Employee.class, 1); + Order order1 = new Order(); + order1.setPono(10); + order1.setCustomer(cust1); + order1.setEmployee(emp1); + //Performing transaction + try{ + userTransaction.begin(); + out.println("Transaction began!"+"
"); + em.persist(cart1); + em.persist(cart2); + em.flush(); + em.refresh(cart1); + out.println("cart1 has been refreshed from database!"+ "
"); + out.println("Price of the book in cart1 is: $" + cart1.getUnit_price() + "
"); + em.refresh(cart2); + out.println("cart2 has been refreshed from database!"+ "
"); + out.println("Price of the book in cart2 is: $" + cart2.getUnit_price() + "
"); + em.persist(order1); + em.flush(); + out.println("Order shipping date is: " + order1.getShipping_date() + "
"); + em.refresh(order1); + out.println("Order instance has been refreshed from database!" + "
"); + out.println("Order shipping date is: " + order1.getShipping_date() + "
"); + try{ + em.refresh(cart1); + out.println("Price of the book in cart1 is: $" + cart1.getUnit_price() + "
"); + em.refresh(cart2); + out.println("Price of the book in cart2 is: $" + cart2.getUnit_price() + "
"); + } + catch ( Exception ex){ + out.println("Failed to refresh ShoppingCart instances from database!" + "
"); + status = userTransaction.getStatus(); + if (status==Status.STATUS_MARKED_ROLLBACK){ + out.println("Transaction has been marked for roll back due to exception!"); + } + } + status = userTransaction.getStatus(); + if (status==Status.STATUS_ACTIVE){ + userTransaction.rollback(); + out.println("Transaction has been rolled back!"); + } + } + catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/ch8/entitiestest/target/WEB-INF/classes/META-INF/persistence.xml b/ch8/entitiestest/target/WEB-INF/classes/META-INF/persistence.xml new file mode 100644 index 0000000..43e7142 --- /dev/null +++ b/ch8/entitiestest/target/WEB-INF/classes/META-INF/persistence.xml @@ -0,0 +1,6 @@ + + + + jdbc/mysqlpool + + diff --git a/ch8/entitiestest/target/WEB-INF/classes/ejbjpa/entities/Customer.class b/ch8/entitiestest/target/WEB-INF/classes/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..855839f Binary files /dev/null and b/ch8/entitiestest/target/WEB-INF/classes/ejbjpa/entities/Customer.class differ diff --git a/ch8/entitiestest/target/WEB-INF/classes/ejbjpa/entities/Employee.class b/ch8/entitiestest/target/WEB-INF/classes/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..380c0cf Binary files /dev/null and b/ch8/entitiestest/target/WEB-INF/classes/ejbjpa/entities/Employee.class differ diff --git a/ch8/entitiestest/target/WEB-INF/classes/ejbjpa/entities/Order.class b/ch8/entitiestest/target/WEB-INF/classes/ejbjpa/entities/Order.class new file mode 100644 index 0000000..7030ba9 Binary files /dev/null and b/ch8/entitiestest/target/WEB-INF/classes/ejbjpa/entities/Order.class differ diff --git a/ch8/entitiestest/target/WEB-INF/classes/ejbjpa/entities/ShoppingCart.class b/ch8/entitiestest/target/WEB-INF/classes/ejbjpa/entities/ShoppingCart.class new file mode 100644 index 0000000..f74950a Binary files /dev/null and b/ch8/entitiestest/target/WEB-INF/classes/ejbjpa/entities/ShoppingCart.class differ diff --git a/ch8/entitiestest/target/WEB-INF/classes/ejbjpa/entities/ShoppingCartKey.class b/ch8/entitiestest/target/WEB-INF/classes/ejbjpa/entities/ShoppingCartKey.class new file mode 100644 index 0000000..c13e03f Binary files /dev/null and b/ch8/entitiestest/target/WEB-INF/classes/ejbjpa/entities/ShoppingCartKey.class differ diff --git a/ch8/entitiestest/target/WEB-INF/classes/ejbjpa/servlets/JpaTestServlet.class b/ch8/entitiestest/target/WEB-INF/classes/ejbjpa/servlets/JpaTestServlet.class new file mode 100644 index 0000000..39b2649 Binary files /dev/null and b/ch8/entitiestest/target/WEB-INF/classes/ejbjpa/servlets/JpaTestServlet.class differ diff --git a/ch8/entitiestest/target/WEB-INF/sun-web.xml b/ch8/entitiestest/target/WEB-INF/sun-web.xml new file mode 100644 index 0000000..3a11a36 --- /dev/null +++ b/ch8/entitiestest/target/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /jpatest + diff --git a/ch8/entitiestest/target/WEB-INF/web.xml b/ch8/entitiestest/target/WEB-INF/web.xml new file mode 100644 index 0000000..77114b3 --- /dev/null +++ b/ch8/entitiestest/target/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + + + JpaTestServlet + ejbjpa.servlets.JpaTestServlet + + + JpaTestServlet + /jpatestservlet + + + diff --git a/ch8/entitiestest/target/jpatestservlet.war b/ch8/entitiestest/target/jpatestservlet.war new file mode 100644 index 0000000..64af658 Binary files /dev/null and b/ch8/entitiestest/target/jpatestservlet.war differ diff --git a/ch8/sampleapp/src/ejbjpa/entities/Customer.java b/ch8/sampleapp/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..aa187c0 --- /dev/null +++ b/ch8/sampleapp/src/ejbjpa/entities/Customer.java @@ -0,0 +1,64 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "CUST_NAME", nullable = false) + private String cust_name; + @Column(name = "LOC_ID") + private Integer loc_id; + @Column(name = "EMAIL", nullable = false) + private String email; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToMany(mappedBy="customer", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCust_name() { + return this.cust_name; + } + public void setCust_name(String cust_name) { + this.cust_name = cust_name; + } + public Integer getLoc_id() { + return this.loc_id; + } + public void setLoc_id(Integer loc_id) { + this.loc_id = loc_id; + } + public String getEmail() { + return this.email; + } + public void setEmail(String email) { + this.email = email; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/ch8/sampleapp/src/ejbjpa/entities/Employee.java b/ch8/sampleapp/src/ejbjpa/entities/Employee.java new file mode 100644 index 0000000..9751c22 --- /dev/null +++ b/ch8/sampleapp/src/ejbjpa/entities/Employee.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "EMPLOYEES") +public class Employee implements Serializable { + @Id + @Column(name = "EMPNO") + private Integer empno; + @Column(name = "firstname", nullable = false) + private String firstname; + @Column(name = "lastname", nullable = false) + private String lastname; + @OneToMany(mappedBy="employee", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Employee() { + } + public Integer getEmpno() { + return this.empno; + } + public void setEmpno(Integer empno) { + this.empno = empno; + } + public String getFirstname() { + return this.firstname; + } + public void setFirstname(String firstname) { + this.firstname = firstname; + } + public String getLastname() { + return this.lastname; + } + public void setLastname(String lastname) { + this.lastname = lastname; + } +} diff --git a/ch8/sampleapp/src/ejbjpa/entities/Order.java b/ch8/sampleapp/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..c550cad --- /dev/null +++ b/ch8/sampleapp/src/ejbjpa/entities/Order.java @@ -0,0 +1,65 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import static javax.persistence.TemporalType.DATE; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import java.util.Date; +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + @Id + @Column(name = "PONO") + private Integer pono; + @Column(name = "SHIPPING_DATE", nullable = false) + @Temporal(DATE) + private Date shipping_date; + @Column(name = "DELIVERY_ESTIMATE", nullable = false) + private String delivery_estimate; + @ManyToOne + @JoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ID") + private Customer customer; + @ManyToOne + @JoinColumn( + name="EMPNO", + referencedColumnName="EMPNO") + private Employee employee; + public Order() { + } + public Customer getCustomer() { + return this.customer; + } + public void setCustomer(Customer customer) { + this.customer = customer; + } + public Employee getEmployee() { + return this.employee; + } + public void setEmployee (Employee employee) { + this.employee = employee; + } + public Integer getPono () { + return this.pono; + } + public void setPono (Integer pono) { + this.pono = pono; + } + public Date getShipping_date () { + return this.shipping_date; + } + public void setShipping_date (Date shipping_date) { + this.shipping_date = shipping_date; + } + public String getDelivery_estimate () { + return this.delivery_estimate; + } + public void setDelivery_estimate (String delivery_estimate) { + this.delivery_estimate = delivery_estimate; + } +} diff --git a/ch8/sampleapp/src/ejbjpa/entities/ShoppingCart.java b/ch8/sampleapp/src/ejbjpa/entities/ShoppingCart.java new file mode 100644 index 0000000..7d169f2 --- /dev/null +++ b/ch8/sampleapp/src/ejbjpa/entities/ShoppingCart.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.IdClass; +import javax.persistence.Table; +@Entity +@Table(name = "SHOPPINGCARTS") +@IdClass(value = ejbjpa.entities.ShoppingCartKey.class) +public class ShoppingCart implements Serializable { + @Id + @Column(name = "CART_ID") + private Integer cart_id; + @Id + @Column(name = "BOOK_ID") + private String book_id; + @Column(name = "UNITS", nullable = false) + private Integer units; + @Column(name = "UNIT_PRICE", nullable = false) + private Double unit_price; + public ShoppingCart() { + } + public Integer getCart_id() { + return this.cart_id; + } + public void setCart_id(Integer cart_id) { + this.cart_id = cart_id; + } + public String getBook_id() { + return this.book_id; + } + public void setBook_id(String book_id) { + this.book_id = book_id; + } + public Integer getUnits () { + return this.units; + } + public void setUnits(Integer units) { + this.units = units; + } + public Double getUnit_price() { + return this.unit_price; + } + public void setUnit_price(Double unit_price) { + this.unit_price = unit_price; + } +} diff --git a/ch8/sampleapp/src/ejbjpa/entities/ShoppingCartKey.java b/ch8/sampleapp/src/ejbjpa/entities/ShoppingCartKey.java new file mode 100644 index 0000000..5c29dbe --- /dev/null +++ b/ch8/sampleapp/src/ejbjpa/entities/ShoppingCartKey.java @@ -0,0 +1,37 @@ +package ejbjpa.entities; +import java.io.Serializable; + +public final class ShoppingCartKey implements Serializable { + public Integer cart_id; + public String book_id; + public ShoppingCartKey() {} + public ShoppingCartKey(Integer cart_id, String book_id) { + this.cart_id = cart_id; + this.book_id = book_id; + } + +public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof ShoppingCartKey)) { + return false; + } + ShoppingCartKey other = (ShoppingCartKey) obj; + if (cart_id != null && other.cart_id!= null && this.cart_id.equals(other.cart_id)) { + return (book_id != null && other.book_id!= null && this.book_id.equals(other.book_id)); + } + return false; +} + +public int hashCode() { + if (cart_id!=null && book_id!=null) { + return (cart_id.hashCode() ^ book_id.hashCode()); + } + return 0; +} + +} \ No newline at end of file diff --git a/ch8/sampleapp/target/ejbjpa/entities/Customer.class b/ch8/sampleapp/target/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..855839f Binary files /dev/null and b/ch8/sampleapp/target/ejbjpa/entities/Customer.class differ diff --git a/ch8/sampleapp/target/ejbjpa/entities/Employee.class b/ch8/sampleapp/target/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..380c0cf Binary files /dev/null and b/ch8/sampleapp/target/ejbjpa/entities/Employee.class differ diff --git a/ch8/sampleapp/target/ejbjpa/entities/Order.class b/ch8/sampleapp/target/ejbjpa/entities/Order.class new file mode 100644 index 0000000..7030ba9 Binary files /dev/null and b/ch8/sampleapp/target/ejbjpa/entities/Order.class differ diff --git a/ch8/sampleapp/target/ejbjpa/entities/ShoppingCart.class b/ch8/sampleapp/target/ejbjpa/entities/ShoppingCart.class new file mode 100644 index 0000000..f74950a Binary files /dev/null and b/ch8/sampleapp/target/ejbjpa/entities/ShoppingCart.class differ diff --git a/ch8/sampleapp/target/ejbjpa/entities/ShoppingCartKey.class b/ch8/sampleapp/target/ejbjpa/entities/ShoppingCartKey.class new file mode 100644 index 0000000..c13e03f Binary files /dev/null and b/ch8/sampleapp/target/ejbjpa/entities/ShoppingCartKey.class differ diff --git a/ch9/cascadingtest/src/ejbjpa/entities/Customer.java b/ch9/cascadingtest/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..aa187c0 --- /dev/null +++ b/ch9/cascadingtest/src/ejbjpa/entities/Customer.java @@ -0,0 +1,64 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "CUST_NAME", nullable = false) + private String cust_name; + @Column(name = "LOC_ID") + private Integer loc_id; + @Column(name = "EMAIL", nullable = false) + private String email; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToMany(mappedBy="customer", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCust_name() { + return this.cust_name; + } + public void setCust_name(String cust_name) { + this.cust_name = cust_name; + } + public Integer getLoc_id() { + return this.loc_id; + } + public void setLoc_id(Integer loc_id) { + this.loc_id = loc_id; + } + public String getEmail() { + return this.email; + } + public void setEmail(String email) { + this.email = email; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/ch9/cascadingtest/src/ejbjpa/entities/Employee.java b/ch9/cascadingtest/src/ejbjpa/entities/Employee.java new file mode 100644 index 0000000..9751c22 --- /dev/null +++ b/ch9/cascadingtest/src/ejbjpa/entities/Employee.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "EMPLOYEES") +public class Employee implements Serializable { + @Id + @Column(name = "EMPNO") + private Integer empno; + @Column(name = "firstname", nullable = false) + private String firstname; + @Column(name = "lastname", nullable = false) + private String lastname; + @OneToMany(mappedBy="employee", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Employee() { + } + public Integer getEmpno() { + return this.empno; + } + public void setEmpno(Integer empno) { + this.empno = empno; + } + public String getFirstname() { + return this.firstname; + } + public void setFirstname(String firstname) { + this.firstname = firstname; + } + public String getLastname() { + return this.lastname; + } + public void setLastname(String lastname) { + this.lastname = lastname; + } +} diff --git a/ch9/cascadingtest/src/ejbjpa/entities/Order.java b/ch9/cascadingtest/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..c550cad --- /dev/null +++ b/ch9/cascadingtest/src/ejbjpa/entities/Order.java @@ -0,0 +1,65 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import static javax.persistence.TemporalType.DATE; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import java.util.Date; +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + @Id + @Column(name = "PONO") + private Integer pono; + @Column(name = "SHIPPING_DATE", nullable = false) + @Temporal(DATE) + private Date shipping_date; + @Column(name = "DELIVERY_ESTIMATE", nullable = false) + private String delivery_estimate; + @ManyToOne + @JoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ID") + private Customer customer; + @ManyToOne + @JoinColumn( + name="EMPNO", + referencedColumnName="EMPNO") + private Employee employee; + public Order() { + } + public Customer getCustomer() { + return this.customer; + } + public void setCustomer(Customer customer) { + this.customer = customer; + } + public Employee getEmployee() { + return this.employee; + } + public void setEmployee (Employee employee) { + this.employee = employee; + } + public Integer getPono () { + return this.pono; + } + public void setPono (Integer pono) { + this.pono = pono; + } + public Date getShipping_date () { + return this.shipping_date; + } + public void setShipping_date (Date shipping_date) { + this.shipping_date = shipping_date; + } + public String getDelivery_estimate () { + return this.delivery_estimate; + } + public void setDelivery_estimate (String delivery_estimate) { + this.delivery_estimate = delivery_estimate; + } +} diff --git a/ch9/cascadingtest/src/ejbjpa/servlets/CascadingTestServlet.java b/ch9/cascadingtest/src/ejbjpa/servlets/CascadingTestServlet.java new file mode 100644 index 0000000..8d81666 --- /dev/null +++ b/ch9/cascadingtest/src/ejbjpa/servlets/CascadingTestServlet.java @@ -0,0 +1,57 @@ +package ejbjpa.servlets; +import java.io.*; +import java.util.*; +import java.sql.*; +import javax.servlet.*; +import javax.servlet.http.*; +import javax.transaction.*; +import javax.annotation.Resource; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; +import ejbjpa.entities.*; +public class CascadingTestServlet extends HttpServlet { + @PersistenceUnit + private EntityManagerFactory emf; + @Resource + private UserTransaction userTransaction; + public void doGet( + HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + response.setBufferSize(8192); + PrintWriter out = response.getWriter(); + EntityManager em= emf.createEntityManager(); + Customer cust = (Customer) em.find(Customer.class, 2); + Employee emp = (Employee) em.find(Employee.class, 2); + List orders = new ArrayList(); + Order order1 = new Order(); + order1.setPono(10); + order1.setCustomer(cust); + order1.setEmployee(emp); + Order order2 = new Order(); + order2.setPono(11); + order2.setCustomer(cust); + order2.setEmployee(emp); + orders.add(order1); + orders.add(order2); + cust.setOrders(orders); + //Performing transaction + try{ + userTransaction.begin(); + out.println("Transaction began!"+"
"); + // em.persist(cust); + em.flush(); + em.refresh(cust); + orders = cust.getOrders(); + for (int i = 0; i < orders.size(); i++) { + out.println("order "+ orders.get(i).getPono()+ " placed by: " + orders.get(i).getCustomer().getCust_name() + "
"); + } + userTransaction.rollback(); + out.println("Transaction has been rolled back!"); + } + catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/ch9/cascadingtest/target/WEB-INF/classes/META-INF/persistence.xml b/ch9/cascadingtest/target/WEB-INF/classes/META-INF/persistence.xml new file mode 100644 index 0000000..601d2ff --- /dev/null +++ b/ch9/cascadingtest/target/WEB-INF/classes/META-INF/persistence.xml @@ -0,0 +1,6 @@ + + + + jdbc/mysqlpool + + diff --git a/ch9/cascadingtest/target/WEB-INF/classes/ejbjpa/entities/Customer.class b/ch9/cascadingtest/target/WEB-INF/classes/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..855839f Binary files /dev/null and b/ch9/cascadingtest/target/WEB-INF/classes/ejbjpa/entities/Customer.class differ diff --git a/ch9/cascadingtest/target/WEB-INF/classes/ejbjpa/entities/Employee.class b/ch9/cascadingtest/target/WEB-INF/classes/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..380c0cf Binary files /dev/null and b/ch9/cascadingtest/target/WEB-INF/classes/ejbjpa/entities/Employee.class differ diff --git a/ch9/cascadingtest/target/WEB-INF/classes/ejbjpa/entities/Order.class b/ch9/cascadingtest/target/WEB-INF/classes/ejbjpa/entities/Order.class new file mode 100644 index 0000000..7030ba9 Binary files /dev/null and b/ch9/cascadingtest/target/WEB-INF/classes/ejbjpa/entities/Order.class differ diff --git a/ch9/cascadingtest/target/WEB-INF/classes/ejbjpa/servlets/CascadingTestServlet.class b/ch9/cascadingtest/target/WEB-INF/classes/ejbjpa/servlets/CascadingTestServlet.class new file mode 100644 index 0000000..28f8f80 Binary files /dev/null and b/ch9/cascadingtest/target/WEB-INF/classes/ejbjpa/servlets/CascadingTestServlet.class differ diff --git a/ch9/cascadingtest/target/WEB-INF/sun-web.xml b/ch9/cascadingtest/target/WEB-INF/sun-web.xml new file mode 100644 index 0000000..a1d4356 --- /dev/null +++ b/ch9/cascadingtest/target/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /cascadingtest + diff --git a/ch9/cascadingtest/target/WEB-INF/web.xml b/ch9/cascadingtest/target/WEB-INF/web.xml new file mode 100644 index 0000000..e380a37 --- /dev/null +++ b/ch9/cascadingtest/target/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + + + CascadingTestServlet + ejbjpa.servlets.CascadingTestServlet + + + CascadingTestServlet + /cascadingtestservlet + + + diff --git a/ch9/cascadingtest/target/cascadingtestservlet.war b/ch9/cascadingtest/target/cascadingtestservlet.war new file mode 100644 index 0000000..8c2d024 Binary files /dev/null and b/ch9/cascadingtest/target/cascadingtestservlet.war differ diff --git a/ch9/compositekey/src/ejbjpa/entities/Book.java b/ch9/compositekey/src/ejbjpa/entities/Book.java new file mode 100644 index 0000000..f8b30f7 --- /dev/null +++ b/ch9/compositekey/src/ejbjpa/entities/Book.java @@ -0,0 +1,67 @@ +package ejbjpa.entities; +import java.util.List; +import javax.persistence.CascadeType; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.OneToMany; +@Entity +@Table(name = "BOOKS") +public class Book implements Serializable { + @Id + @Column(name = "ISBN") + private String isbn; + @Column(name = "TITLE", nullable = false) + private String title; + @Column(name = "AUTHOR", nullable = false) + private String author; + @Column(name = "PRICE", nullable = false) + private Double price; + @Column(name = "QUANTITY", nullable = false) + private Integer quantity; + + @OneToMany(mappedBy="book", cascade = CascadeType.ALL) + private List shoppingCarts; + public List getShoppingCarts(){ + return shoppingCarts; + } + public void setShoppingCarts(List shoppingCarts) { + this.shoppingCarts = shoppingCarts; + } + + public Book() { + } + public String getIsbn() { + return this.isbn; + } + public void setIsbn(String isbn) { + this.isbn = isbn; + } + public String getTitle() { + return this.title; + } + public void setTitle(String title) { + this.title = title; + } + public String getAuthor() { + return this.author; + } + public void setAuthor(String author) { + this.author = author; + } + + public Double getPrice() { + return this.price; + } + public void setPrice(Double price) { + this.price = price; + } + public Integer getQuantity() { + return this.quantity; + } + public void setQuantity(Integer quantity) { + this.quantity = quantity; + } +} diff --git a/ch9/compositekey/src/ejbjpa/entities/ShoppingCart.java b/ch9/compositekey/src/ejbjpa/entities/ShoppingCart.java new file mode 100644 index 0000000..a3da937 --- /dev/null +++ b/ch9/compositekey/src/ejbjpa/entities/ShoppingCart.java @@ -0,0 +1,56 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.IdClass; +import javax.persistence.Table; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; + +@Entity +@Table(name = "SHOPPINGCARTS") +@IdClass(value = ejbjpa.entities.ShoppingCartKey.class) +public class ShoppingCart implements Serializable { + @Id + @Column(name = "CART_ID") + private Integer cart_id; + @Id + @Column(name = "BOOK_ID", insertable=false, updatable=false) + private String book_id; + @Column(name = "UNITS", nullable = false) + private Integer units; + @Column(name = "UNIT_PRICE", nullable = false) + private Double unit_price; + @ManyToOne + @JoinColumn( + name="BOOK_ID", + referencedColumnName="ISBN") + private Book book; + public ShoppingCart() { + } + public Book getBook() { + return this.book; + } + public void setBook(Book book) { + this.book = book; + } + public Integer getCart_id() { + return this.cart_id; + } + public void setCart_id(Integer cart_id) { + this.cart_id = cart_id; + } + public Integer getUnits () { + return this.units; + } + public void setUnits(Integer units) { + this.units = units; + } + public Double getUnit_price() { + return this.unit_price; + } + public void setUnit_price(Double unit_price) { + this.unit_price = unit_price; + } +} diff --git a/ch9/compositekey/src/ejbjpa/entities/ShoppingCartKey.java b/ch9/compositekey/src/ejbjpa/entities/ShoppingCartKey.java new file mode 100644 index 0000000..5c29dbe --- /dev/null +++ b/ch9/compositekey/src/ejbjpa/entities/ShoppingCartKey.java @@ -0,0 +1,37 @@ +package ejbjpa.entities; +import java.io.Serializable; + +public final class ShoppingCartKey implements Serializable { + public Integer cart_id; + public String book_id; + public ShoppingCartKey() {} + public ShoppingCartKey(Integer cart_id, String book_id) { + this.cart_id = cart_id; + this.book_id = book_id; + } + +public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof ShoppingCartKey)) { + return false; + } + ShoppingCartKey other = (ShoppingCartKey) obj; + if (cart_id != null && other.cart_id!= null && this.cart_id.equals(other.cart_id)) { + return (book_id != null && other.book_id!= null && this.book_id.equals(other.book_id)); + } + return false; +} + +public int hashCode() { + if (cart_id!=null && book_id!=null) { + return (cart_id.hashCode() ^ book_id.hashCode()); + } + return 0; +} + +} \ No newline at end of file diff --git a/ch9/compositekey/src/ejbjpa/servlets/CompositeKeyTestServlet.java b/ch9/compositekey/src/ejbjpa/servlets/CompositeKeyTestServlet.java new file mode 100644 index 0000000..44084e7 --- /dev/null +++ b/ch9/compositekey/src/ejbjpa/servlets/CompositeKeyTestServlet.java @@ -0,0 +1,64 @@ +package ejbjpa.servlets; + +import java.io.*; +import java.util.*; +import java.sql.*; +import javax.servlet.*; +import javax.servlet.http.*; +import javax.transaction.*; +import javax.annotation.Resource; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; +import ejbjpa.entities.*; + +public class CompositeKeyTestServlet extends HttpServlet { + @PersistenceUnit + private EntityManagerFactory emf; + @Resource + private UserTransaction userTransaction; + + public void doGet( + HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + response.setBufferSize(8192); + PrintWriter out = response.getWriter(); + EntityManager em= emf.createEntityManager(); + //creating ShoppingCart entity instances + ShoppingCart cart1 = new ShoppingCart(); + cart1.setCart_id(2); + Book book1 = (Book) em.find(Book.class, "1590595300"); + cart1.setBook(book1); + cart1.setUnits(3); + cart1.setUnit_price(49.99); + out.println("Price of the book in book1 is: $" + book1.getPrice() + "
"); + + ShoppingCart cart2 = new ShoppingCart(); + cart2.setCart_id(2); + Book book2 = (Book) em.find(Book.class, "1430209631"); + cart2.setBook(book2); + cart2.setUnits(2); + cart2.setUnit_price(44.99); + out.println("Price of the book in book2 is: $" + book2.getPrice() + "
"); + + //Performing transaction + try{ + userTransaction.begin(); + out.println("Transaction began!"+"
"); + em.persist(cart1); + em.persist(cart2); + em.flush(); + out.println("cart instances have been persisted to database!"+ "
"); + ShoppingCart cart3 = (ShoppingCart) em.find(ShoppingCart.class, new ShoppingCartKey(2, "1590595300")); + out.println("Price of the book in cart3 is: $" + cart3.getBook().getPrice() + "
"); + ShoppingCart cart4 = (ShoppingCart) em.find(ShoppingCart.class, new ShoppingCartKey(2, "1430209631")); + out.println("Price of the book in cart4 is: $" + cart4.getBook().getPrice() + "
"); + userTransaction.rollback(); + out.println("Transaction has been rolled back!"); + } + catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/ch9/compositekey/target/WEB-INF/classes/META-INF/persistence.xml b/ch9/compositekey/target/WEB-INF/classes/META-INF/persistence.xml new file mode 100644 index 0000000..321b186 --- /dev/null +++ b/ch9/compositekey/target/WEB-INF/classes/META-INF/persistence.xml @@ -0,0 +1,6 @@ + + + + jdbc/mysqlpool + + diff --git a/ch9/compositekey/target/WEB-INF/classes/ejbjpa/entities/Book.class b/ch9/compositekey/target/WEB-INF/classes/ejbjpa/entities/Book.class new file mode 100644 index 0000000..c45580c Binary files /dev/null and b/ch9/compositekey/target/WEB-INF/classes/ejbjpa/entities/Book.class differ diff --git a/ch9/compositekey/target/WEB-INF/classes/ejbjpa/entities/ShoppingCart.class b/ch9/compositekey/target/WEB-INF/classes/ejbjpa/entities/ShoppingCart.class new file mode 100644 index 0000000..2b51d62 Binary files /dev/null and b/ch9/compositekey/target/WEB-INF/classes/ejbjpa/entities/ShoppingCart.class differ diff --git a/ch9/compositekey/target/WEB-INF/classes/ejbjpa/entities/ShoppingCartKey.class b/ch9/compositekey/target/WEB-INF/classes/ejbjpa/entities/ShoppingCartKey.class new file mode 100644 index 0000000..c13e03f Binary files /dev/null and b/ch9/compositekey/target/WEB-INF/classes/ejbjpa/entities/ShoppingCartKey.class differ diff --git a/ch9/compositekey/target/WEB-INF/classes/ejbjpa/servlets/CompositeKeyTestServlet.class b/ch9/compositekey/target/WEB-INF/classes/ejbjpa/servlets/CompositeKeyTestServlet.class new file mode 100644 index 0000000..edf987d Binary files /dev/null and b/ch9/compositekey/target/WEB-INF/classes/ejbjpa/servlets/CompositeKeyTestServlet.class differ diff --git a/ch9/compositekey/target/WEB-INF/sun-web.xml b/ch9/compositekey/target/WEB-INF/sun-web.xml new file mode 100644 index 0000000..a52fa7c --- /dev/null +++ b/ch9/compositekey/target/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /keytest + diff --git a/ch9/compositekey/target/WEB-INF/web.xml b/ch9/compositekey/target/WEB-INF/web.xml new file mode 100644 index 0000000..73e33b3 --- /dev/null +++ b/ch9/compositekey/target/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + + + CompositeKeyTestServlet + ejbjpa.servlets.CompositeKeyTestServlet + + + CompositeKeyTestServlet + /compositekeytestservlet + + + diff --git a/ch9/compositekey/target/compositekeytestservlet.war b/ch9/compositekey/target/compositekeytestservlet.war new file mode 100644 index 0000000..2c9c546 Binary files /dev/null and b/ch9/compositekey/target/compositekeytestservlet.war differ diff --git a/ch9/generatingkeytest/src/ejbjpa/entities/Customer.java b/ch9/generatingkeytest/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..aa187c0 --- /dev/null +++ b/ch9/generatingkeytest/src/ejbjpa/entities/Customer.java @@ -0,0 +1,64 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "CUST_NAME", nullable = false) + private String cust_name; + @Column(name = "LOC_ID") + private Integer loc_id; + @Column(name = "EMAIL", nullable = false) + private String email; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToMany(mappedBy="customer", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCust_name() { + return this.cust_name; + } + public void setCust_name(String cust_name) { + this.cust_name = cust_name; + } + public Integer getLoc_id() { + return this.loc_id; + } + public void setLoc_id(Integer loc_id) { + this.loc_id = loc_id; + } + public String getEmail() { + return this.email; + } + public void setEmail(String email) { + this.email = email; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/ch9/generatingkeytest/src/ejbjpa/entities/Employee.java b/ch9/generatingkeytest/src/ejbjpa/entities/Employee.java new file mode 100644 index 0000000..9751c22 --- /dev/null +++ b/ch9/generatingkeytest/src/ejbjpa/entities/Employee.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "EMPLOYEES") +public class Employee implements Serializable { + @Id + @Column(name = "EMPNO") + private Integer empno; + @Column(name = "firstname", nullable = false) + private String firstname; + @Column(name = "lastname", nullable = false) + private String lastname; + @OneToMany(mappedBy="employee", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Employee() { + } + public Integer getEmpno() { + return this.empno; + } + public void setEmpno(Integer empno) { + this.empno = empno; + } + public String getFirstname() { + return this.firstname; + } + public void setFirstname(String firstname) { + this.firstname = firstname; + } + public String getLastname() { + return this.lastname; + } + public void setLastname(String lastname) { + this.lastname = lastname; + } +} diff --git a/ch9/generatingkeytest/src/ejbjpa/entities/Order.java b/ch9/generatingkeytest/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..b9aa6e0 --- /dev/null +++ b/ch9/generatingkeytest/src/ejbjpa/entities/Order.java @@ -0,0 +1,76 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import static javax.persistence.TemporalType.DATE; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.TableGenerator; +import java.util.Date; +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + /* @TableGenerator(name = "ponoGen", + table = "PONO_GEN_TABLE", + pkColumnName = "GEN_NAME", + valueColumnName = "GEN_VALUE", + pkColumnValue = "PONO_GEN", + allocationSize = 1) */ + @Id + // @GeneratedValue(strategy=GenerationType.TABLE, generator = "ponoGen") + @GeneratedValue(strategy=GenerationType.IDENTITY) + @Column(name = "PONO") + private Integer pono; + @Column(name = "SHIPPING_DATE", nullable = false) + @Temporal(DATE) + private Date shipping_date; + @Column(name = "DELIVERY_ESTIMATE", nullable = false) + private String delivery_estimate; + @ManyToOne + @JoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ID") + private Customer customer; + @ManyToOne + @JoinColumn( + name="EMPNO", + referencedColumnName="EMPNO") + private Employee employee; + public Order() { + } + public Customer getCustomer() { + return this.customer; + } + public void setCustomer(Customer customer) { + this.customer = customer; + } + public Employee getEmployee() { + return this.employee; + } + public void setEmployee (Employee employee) { + this.employee = employee; + } + public Integer getPono () { + return this.pono; + } + public void setPono (Integer pono) { + this.pono = pono; + } + public Date getShipping_date () { + return this.shipping_date; + } + public void setShipping_date (Date shipping_date) { + this.shipping_date = shipping_date; + } + public String getDelivery_estimate () { + return this.delivery_estimate; + } + public void setDelivery_estimate (String delivery_estimate) { + this.delivery_estimate = delivery_estimate; + } +} diff --git a/ch9/generatingkeytest/src/ejbjpa/servlets/GeneratingKeyTestServlet.java b/ch9/generatingkeytest/src/ejbjpa/servlets/GeneratingKeyTestServlet.java new file mode 100644 index 0000000..a539442 --- /dev/null +++ b/ch9/generatingkeytest/src/ejbjpa/servlets/GeneratingKeyTestServlet.java @@ -0,0 +1,53 @@ +package ejbjpa.servlets; +import java.io.*; +import java.util.*; +import java.sql.*; +import javax.servlet.*; +import javax.servlet.http.*; +import javax.transaction.*; +import javax.annotation.Resource; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; +import ejbjpa.entities.*; +public class GeneratingKeyTestServlet extends HttpServlet { + @PersistenceUnit + private EntityManagerFactory emf; + @Resource + private UserTransaction userTransaction; + public void doGet( + HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + response.setBufferSize(8192); + PrintWriter out = response.getWriter(); + EntityManager em= emf.createEntityManager(); + Customer cust = (Customer) em.find(Customer.class, 2); + Employee emp = (Employee) em.find(Employee.class, 2); + Order order1 = new Order(); + // order1.setPono(16); + order1.setCustomer(cust); + order1.setEmployee(emp); + Order order2 = new Order(); + // order2.setPono(17); + order2.setCustomer(cust); + order2.setEmployee(emp); + //Performing transaction + try{ + userTransaction.begin(); + out.println("Transaction began!"+"
"); + em.persist(order1); + em.persist(order2); + em.flush(); + em.refresh(order1); + em.refresh(order1); + out.println("order "+ order1.getPono()+ " placed by: " + order1.getCustomer().getCust_name() + "
"); + out.println("order "+ order2.getPono()+ " placed by: " + order2.getCustomer().getCust_name() + "
"); + userTransaction.rollback(); + out.println("Transaction has been rolled back!"); + } + catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/ch9/generatingkeytest/target/WEB-INF/classes/META-INF/persistence.xml b/ch9/generatingkeytest/target/WEB-INF/classes/META-INF/persistence.xml new file mode 100644 index 0000000..b176fd7 --- /dev/null +++ b/ch9/generatingkeytest/target/WEB-INF/classes/META-INF/persistence.xml @@ -0,0 +1,6 @@ + + + + jdbc/mysqlpool + + diff --git a/ch9/generatingkeytest/target/WEB-INF/classes/ejbjpa/entities/Customer.class b/ch9/generatingkeytest/target/WEB-INF/classes/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..855839f Binary files /dev/null and b/ch9/generatingkeytest/target/WEB-INF/classes/ejbjpa/entities/Customer.class differ diff --git a/ch9/generatingkeytest/target/WEB-INF/classes/ejbjpa/entities/Employee.class b/ch9/generatingkeytest/target/WEB-INF/classes/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..380c0cf Binary files /dev/null and b/ch9/generatingkeytest/target/WEB-INF/classes/ejbjpa/entities/Employee.class differ diff --git a/ch9/generatingkeytest/target/WEB-INF/classes/ejbjpa/entities/Order.class b/ch9/generatingkeytest/target/WEB-INF/classes/ejbjpa/entities/Order.class new file mode 100644 index 0000000..7c7e67c Binary files /dev/null and b/ch9/generatingkeytest/target/WEB-INF/classes/ejbjpa/entities/Order.class differ diff --git a/ch9/generatingkeytest/target/WEB-INF/classes/ejbjpa/servlets/GeneratingKeyTestServlet.class b/ch9/generatingkeytest/target/WEB-INF/classes/ejbjpa/servlets/GeneratingKeyTestServlet.class new file mode 100644 index 0000000..89ba6f9 Binary files /dev/null and b/ch9/generatingkeytest/target/WEB-INF/classes/ejbjpa/servlets/GeneratingKeyTestServlet.class differ diff --git a/ch9/generatingkeytest/target/WEB-INF/sun-web.xml b/ch9/generatingkeytest/target/WEB-INF/sun-web.xml new file mode 100644 index 0000000..d019f84 --- /dev/null +++ b/ch9/generatingkeytest/target/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /generatingkeytest + diff --git a/ch9/generatingkeytest/target/WEB-INF/web.xml b/ch9/generatingkeytest/target/WEB-INF/web.xml new file mode 100644 index 0000000..7721bfb --- /dev/null +++ b/ch9/generatingkeytest/target/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + + + GeneratingKeyTestServlet + ejbjpa.servlets.GeneratingKeyTestServlet + + + GeneratingKeyTestServlet + /generatingkeytestservlet + + + diff --git a/ch9/generatingkeytest/target/generatingkeytestservlet.war b/ch9/generatingkeytest/target/generatingkeytestservlet.war new file mode 100644 index 0000000..912456f Binary files /dev/null and b/ch9/generatingkeytest/target/generatingkeytestservlet.war differ diff --git a/ch9/generatingkeytestoracle/src/ejbjpa/entities/Customer.java b/ch9/generatingkeytestoracle/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..aa187c0 --- /dev/null +++ b/ch9/generatingkeytestoracle/src/ejbjpa/entities/Customer.java @@ -0,0 +1,64 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "CUST_NAME", nullable = false) + private String cust_name; + @Column(name = "LOC_ID") + private Integer loc_id; + @Column(name = "EMAIL", nullable = false) + private String email; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToMany(mappedBy="customer", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCust_name() { + return this.cust_name; + } + public void setCust_name(String cust_name) { + this.cust_name = cust_name; + } + public Integer getLoc_id() { + return this.loc_id; + } + public void setLoc_id(Integer loc_id) { + this.loc_id = loc_id; + } + public String getEmail() { + return this.email; + } + public void setEmail(String email) { + this.email = email; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/ch9/generatingkeytestoracle/src/ejbjpa/entities/Employee.java b/ch9/generatingkeytestoracle/src/ejbjpa/entities/Employee.java new file mode 100644 index 0000000..9751c22 --- /dev/null +++ b/ch9/generatingkeytestoracle/src/ejbjpa/entities/Employee.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "EMPLOYEES") +public class Employee implements Serializable { + @Id + @Column(name = "EMPNO") + private Integer empno; + @Column(name = "firstname", nullable = false) + private String firstname; + @Column(name = "lastname", nullable = false) + private String lastname; + @OneToMany(mappedBy="employee", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Employee() { + } + public Integer getEmpno() { + return this.empno; + } + public void setEmpno(Integer empno) { + this.empno = empno; + } + public String getFirstname() { + return this.firstname; + } + public void setFirstname(String firstname) { + this.firstname = firstname; + } + public String getLastname() { + return this.lastname; + } + public void setLastname(String lastname) { + this.lastname = lastname; + } +} diff --git a/ch9/generatingkeytestoracle/src/ejbjpa/entities/Order.java b/ch9/generatingkeytestoracle/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..74a69ef --- /dev/null +++ b/ch9/generatingkeytestoracle/src/ejbjpa/entities/Order.java @@ -0,0 +1,83 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import static javax.persistence.TemporalType.DATE; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.TableGenerator; +import javax.persistence.SequenceGenerator; + +import java.util.Date; +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + /* @TableGenerator(name = "ponoGen", + table = "PONO_GEN_TABLE", + pkColumnName = "GEN_NAME", + valueColumnName = "GEN_VALUE", + pkColumnValue = "PONO_GEN", + allocationSize = 1) */ + @SequenceGenerator(name = "ponoGen", + sequenceName = "PONO_GEN_SEQUENCE", + initialValue = 10, + allocationSize = 1) + + @Id + // @GeneratedValue(strategy=GenerationType.TABLE, generator = "ponoGen") + @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "ponoGen") + @Column(name = "PONO") + private Integer pono; + @Column(name = "SHIPPING_DATE", nullable = false) + @Temporal(DATE) + private Date shipping_date; + @Column(name = "DELIVERY_ESTIMATE", nullable = false) + private String delivery_estimate; + @ManyToOne + @JoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ID") + private Customer customer; + @ManyToOne + @JoinColumn( + name="EMPNO", + referencedColumnName="EMPNO") + private Employee employee; + public Order() { + } + public Customer getCustomer() { + return this.customer; + } + public void setCustomer(Customer customer) { + this.customer = customer; + } + public Employee getEmployee() { + return this.employee; + } + public void setEmployee (Employee employee) { + this.employee = employee; + } + public Integer getPono () { + return this.pono; + } + public void setPono (Integer pono) { + this.pono = pono; + } + public Date getShipping_date () { + return this.shipping_date; + } + public void setShipping_date (Date shipping_date) { + this.shipping_date = shipping_date; + } + public String getDelivery_estimate () { + return this.delivery_estimate; + } + public void setDelivery_estimate (String delivery_estimate) { + this.delivery_estimate = delivery_estimate; + } +} diff --git a/ch9/generatingkeytestoracle/src/ejbjpa/servlets/GeneratingKeyTestServlet.java b/ch9/generatingkeytestoracle/src/ejbjpa/servlets/GeneratingKeyTestServlet.java new file mode 100644 index 0000000..156117c --- /dev/null +++ b/ch9/generatingkeytestoracle/src/ejbjpa/servlets/GeneratingKeyTestServlet.java @@ -0,0 +1,53 @@ +package ejbjpa.servlets; +import java.io.*; +import java.util.*; +import java.sql.*; +import javax.servlet.*; +import javax.servlet.http.*; +import javax.transaction.*; +import javax.annotation.Resource; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; +import ejbjpa.entities.*; +public class GeneratingKeyTestServlet extends HttpServlet { + @PersistenceUnit + private EntityManagerFactory emf; + @Resource + private UserTransaction userTransaction; + public void doGet( + HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + response.setBufferSize(8192); + PrintWriter out = response.getWriter(); + EntityManager em= emf.createEntityManager(); + Customer cust = (Customer) em.find(Customer.class, 2); + Employee emp = (Employee) em.find(Employee.class, 2); + Order order1 = new Order(); + // order1.setPono(17); + order1.setCustomer(cust); + order1.setEmployee(emp); + Order order2 = new Order(); + // order2.setPono(18); + order2.setCustomer(cust); + order2.setEmployee(emp); + //Performing transaction + try{ + userTransaction.begin(); + out.println("Transaction began!"+"
"); + em.persist(order1); + em.persist(order2); + em.flush(); + em.refresh(order1); + em.refresh(order1); + out.println("order "+ order1.getPono()+ " placed by: " + order1.getCustomer().getCust_name() + "
"); + out.println("order "+ order2.getPono()+ " placed by: " + order2.getCustomer().getCust_name() + "
"); + userTransaction.rollback(); + out.println("Transaction has been rolled back!"); + } + catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/ch9/generatingkeytestoracle/target/WEB-INF/classes/META-INF/persistence.xml b/ch9/generatingkeytestoracle/target/WEB-INF/classes/META-INF/persistence.xml new file mode 100644 index 0000000..22e6ed4 --- /dev/null +++ b/ch9/generatingkeytestoracle/target/WEB-INF/classes/META-INF/persistence.xml @@ -0,0 +1,6 @@ + + + + jdbc/oraclepool + + diff --git a/ch9/generatingkeytestoracle/target/WEB-INF/classes/ejbjpa/entities/Customer.class b/ch9/generatingkeytestoracle/target/WEB-INF/classes/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..855839f Binary files /dev/null and b/ch9/generatingkeytestoracle/target/WEB-INF/classes/ejbjpa/entities/Customer.class differ diff --git a/ch9/generatingkeytestoracle/target/WEB-INF/classes/ejbjpa/entities/Employee.class b/ch9/generatingkeytestoracle/target/WEB-INF/classes/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..380c0cf Binary files /dev/null and b/ch9/generatingkeytestoracle/target/WEB-INF/classes/ejbjpa/entities/Employee.class differ diff --git a/ch9/generatingkeytestoracle/target/WEB-INF/classes/ejbjpa/entities/Order.class b/ch9/generatingkeytestoracle/target/WEB-INF/classes/ejbjpa/entities/Order.class new file mode 100644 index 0000000..98e3a0a Binary files /dev/null and b/ch9/generatingkeytestoracle/target/WEB-INF/classes/ejbjpa/entities/Order.class differ diff --git a/ch9/generatingkeytestoracle/target/WEB-INF/classes/ejbjpa/servlets/GeneratingKeyTestServlet.class b/ch9/generatingkeytestoracle/target/WEB-INF/classes/ejbjpa/servlets/GeneratingKeyTestServlet.class new file mode 100644 index 0000000..89ba6f9 Binary files /dev/null and b/ch9/generatingkeytestoracle/target/WEB-INF/classes/ejbjpa/servlets/GeneratingKeyTestServlet.class differ diff --git a/ch9/generatingkeytestoracle/target/WEB-INF/sun-web.xml b/ch9/generatingkeytestoracle/target/WEB-INF/sun-web.xml new file mode 100644 index 0000000..fcd21a0 --- /dev/null +++ b/ch9/generatingkeytestoracle/target/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /generatingkeytestoracle + diff --git a/ch9/generatingkeytestoracle/target/WEB-INF/web.xml b/ch9/generatingkeytestoracle/target/WEB-INF/web.xml new file mode 100644 index 0000000..7721bfb --- /dev/null +++ b/ch9/generatingkeytestoracle/target/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + + + GeneratingKeyTestServlet + ejbjpa.servlets.GeneratingKeyTestServlet + + + GeneratingKeyTestServlet + /generatingkeytestservlet + + + diff --git a/ch9/generatingkeytestoracle/target/generatingkeyservletoracle.war b/ch9/generatingkeytestoracle/target/generatingkeyservletoracle.war new file mode 100644 index 0000000..7a461b9 Binary files /dev/null and b/ch9/generatingkeytestoracle/target/generatingkeyservletoracle.war differ diff --git a/ch9/generatingkeytestoracle/target/generatingkeytestservlet.war b/ch9/generatingkeytestoracle/target/generatingkeytestservlet.war new file mode 100644 index 0000000..27c6eaa Binary files /dev/null and b/ch9/generatingkeytestoracle/target/generatingkeytestservlet.war differ diff --git a/ch9/ormxmltest/src/ejbjpa/entities/Customer.java b/ch9/ormxmltest/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..aa187c0 --- /dev/null +++ b/ch9/ormxmltest/src/ejbjpa/entities/Customer.java @@ -0,0 +1,64 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "CUST_NAME", nullable = false) + private String cust_name; + @Column(name = "LOC_ID") + private Integer loc_id; + @Column(name = "EMAIL", nullable = false) + private String email; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToMany(mappedBy="customer", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCust_name() { + return this.cust_name; + } + public void setCust_name(String cust_name) { + this.cust_name = cust_name; + } + public Integer getLoc_id() { + return this.loc_id; + } + public void setLoc_id(Integer loc_id) { + this.loc_id = loc_id; + } + public String getEmail() { + return this.email; + } + public void setEmail(String email) { + this.email = email; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/ch9/ormxmltest/src/ejbjpa/entities/Employee.java b/ch9/ormxmltest/src/ejbjpa/entities/Employee.java new file mode 100644 index 0000000..93822c9 --- /dev/null +++ b/ch9/ormxmltest/src/ejbjpa/entities/Employee.java @@ -0,0 +1,35 @@ +package ejbjpa.entities; +import java.io.Serializable; +import java.util.List; +public class Employee implements Serializable { + private Integer empno; + private String firstname; + private String lastname; + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Employee() { + } + public Integer getEmpno() { + return this.empno; + } + public void setEmpno(Integer empno) { + this.empno = empno; + } + public String getFirstname() { + return this.firstname; + } + public void setFirstname(String firstname) { + this.firstname = firstname; + } + public String getLastname() { + return this.lastname; + } + public void setLastname(String lastname) { + this.lastname = lastname; + } +} diff --git a/ch9/ormxmltest/src/ejbjpa/entities/Order.java b/ch9/ormxmltest/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..c550cad --- /dev/null +++ b/ch9/ormxmltest/src/ejbjpa/entities/Order.java @@ -0,0 +1,65 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import static javax.persistence.TemporalType.DATE; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import java.util.Date; +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + @Id + @Column(name = "PONO") + private Integer pono; + @Column(name = "SHIPPING_DATE", nullable = false) + @Temporal(DATE) + private Date shipping_date; + @Column(name = "DELIVERY_ESTIMATE", nullable = false) + private String delivery_estimate; + @ManyToOne + @JoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ID") + private Customer customer; + @ManyToOne + @JoinColumn( + name="EMPNO", + referencedColumnName="EMPNO") + private Employee employee; + public Order() { + } + public Customer getCustomer() { + return this.customer; + } + public void setCustomer(Customer customer) { + this.customer = customer; + } + public Employee getEmployee() { + return this.employee; + } + public void setEmployee (Employee employee) { + this.employee = employee; + } + public Integer getPono () { + return this.pono; + } + public void setPono (Integer pono) { + this.pono = pono; + } + public Date getShipping_date () { + return this.shipping_date; + } + public void setShipping_date (Date shipping_date) { + this.shipping_date = shipping_date; + } + public String getDelivery_estimate () { + return this.delivery_estimate; + } + public void setDelivery_estimate (String delivery_estimate) { + this.delivery_estimate = delivery_estimate; + } +} diff --git a/ch9/ormxmltest/src/ejbjpa/servlets/OrmXMLTestServlet.java b/ch9/ormxmltest/src/ejbjpa/servlets/OrmXMLTestServlet.java new file mode 100644 index 0000000..1871ba7 --- /dev/null +++ b/ch9/ormxmltest/src/ejbjpa/servlets/OrmXMLTestServlet.java @@ -0,0 +1,52 @@ +package ejbjpa.servlets; +import java.io.*; +import java.util.*; +import java.sql.*; +import javax.servlet.*; +import javax.servlet.http.*; +import javax.transaction.*; +import javax.annotation.Resource; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; +import ejbjpa.entities.*; +public class OrmXMLTestServlet extends HttpServlet { + @PersistenceUnit + private EntityManagerFactory emf; + @Resource + private UserTransaction userTransaction; + public void doGet( + HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + response.setBufferSize(8192); + PrintWriter out = response.getWriter(); + EntityManager em= emf.createEntityManager(); + Customer cust = (Customer) em.find(Customer.class, 2); + Employee emp = (Employee) em.find(Employee.class, 2); + Order order1 = new Order(); + order1.setPono(10); + order1.setCustomer(cust); + order1.setEmployee(emp); + Order order2 = new Order(); + order2.setPono(11); + order2.setCustomer(cust); + order2.setEmployee(emp); + //Performing transaction + try{ + userTransaction.begin(); + out.println("Transaction began!"+"
"); + em.persist(order1); + em.persist(order2); + em.flush(); + em.refresh(emp); + out.println("order " + emp.getOrders().get(0).getPono()+ " placed via: " + emp.getOrders().get(0).getEmployee().getLastname() + "
"); + out.println("order " + emp.getOrders().get(1).getPono()+ " placed via: " + emp.getOrders().get(1).getEmployee().getLastname() + "
"); + userTransaction.rollback(); + out.println("Transaction has been rolled back!"); + } + catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/ch9/ormxmltest/target/WEB-INF/classes/META-INF/orm.xml b/ch9/ormxmltest/target/WEB-INF/classes/META-INF/orm.xml new file mode 100644 index 0000000..c987409 --- /dev/null +++ b/ch9/ormxmltest/target/WEB-INF/classes/META-INF/orm.xml @@ -0,0 +1,27 @@ + + + ejbjpa.entities + FIELD + + + + + + + + + + + + + + + + + + + + diff --git a/ch9/ormxmltest/target/WEB-INF/classes/META-INF/persistence.xml b/ch9/ormxmltest/target/WEB-INF/classes/META-INF/persistence.xml new file mode 100644 index 0000000..1d90102 --- /dev/null +++ b/ch9/ormxmltest/target/WEB-INF/classes/META-INF/persistence.xml @@ -0,0 +1,6 @@ + + + + jdbc/mysqlpool + + diff --git a/ch9/ormxmltest/target/WEB-INF/classes/ejbjpa/entities/Customer.class b/ch9/ormxmltest/target/WEB-INF/classes/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..855839f Binary files /dev/null and b/ch9/ormxmltest/target/WEB-INF/classes/ejbjpa/entities/Customer.class differ diff --git a/ch9/ormxmltest/target/WEB-INF/classes/ejbjpa/entities/Employee.class b/ch9/ormxmltest/target/WEB-INF/classes/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..91ecf29 Binary files /dev/null and b/ch9/ormxmltest/target/WEB-INF/classes/ejbjpa/entities/Employee.class differ diff --git a/ch9/ormxmltest/target/WEB-INF/classes/ejbjpa/entities/Order.class b/ch9/ormxmltest/target/WEB-INF/classes/ejbjpa/entities/Order.class new file mode 100644 index 0000000..7030ba9 Binary files /dev/null and b/ch9/ormxmltest/target/WEB-INF/classes/ejbjpa/entities/Order.class differ diff --git a/ch9/ormxmltest/target/WEB-INF/classes/ejbjpa/servlets/OrmXMLTestServlet.class b/ch9/ormxmltest/target/WEB-INF/classes/ejbjpa/servlets/OrmXMLTestServlet.class new file mode 100644 index 0000000..6780a62 Binary files /dev/null and b/ch9/ormxmltest/target/WEB-INF/classes/ejbjpa/servlets/OrmXMLTestServlet.class differ diff --git a/ch9/ormxmltest/target/WEB-INF/sun-web.xml b/ch9/ormxmltest/target/WEB-INF/sun-web.xml new file mode 100644 index 0000000..b65fa17 --- /dev/null +++ b/ch9/ormxmltest/target/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /ormxmltest + diff --git a/ch9/ormxmltest/target/WEB-INF/web.xml b/ch9/ormxmltest/target/WEB-INF/web.xml new file mode 100644 index 0000000..540d573 --- /dev/null +++ b/ch9/ormxmltest/target/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + + + OrmXMLTestServlet + ejbjpa.servlets.OrmXMLTestServlet + + + OrmXMLTestServlet + /ormxmltestservlet + + + diff --git a/ch9/ormxmltest/target/ormxmltestservlet.war b/ch9/ormxmltest/target/ormxmltestservlet.war new file mode 100644 index 0000000..bec8de6 Binary files /dev/null and b/ch9/ormxmltest/target/ormxmltestservlet.war differ diff --git a/ch9/relationshiptest/src/ejbjpa/entities/Customer.java b/ch9/relationshiptest/src/ejbjpa/entities/Customer.java new file mode 100644 index 0000000..aa187c0 --- /dev/null +++ b/ch9/relationshiptest/src/ejbjpa/entities/Customer.java @@ -0,0 +1,64 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "CUSTOMERS") +public class Customer implements Serializable { + @Id + @Column(name = "CUST_ID") + private Integer cust_id; + @Column(name = "CUST_NAME", nullable = false) + private String cust_name; + @Column(name = "LOC_ID") + private Integer loc_id; + @Column(name = "EMAIL", nullable = false) + private String email; + @Column(name = "PHONE", nullable = false) + private String phone; + @OneToMany(mappedBy="customer", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Customer() { + } + public Integer getCust_id() { + return this.cust_id; + } + public void setCust_id(Integer cust_id) { + this.cust_id = cust_id; + } + public String getCust_name() { + return this.cust_name; + } + public void setCust_name(String cust_name) { + this.cust_name = cust_name; + } + public Integer getLoc_id() { + return this.loc_id; + } + public void setLoc_id(Integer loc_id) { + this.loc_id = loc_id; + } + public String getEmail() { + return this.email; + } + public void setEmail(String email) { + this.email = email; + } + public String getPhone() { + return this.phone; + } + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/ch9/relationshiptest/src/ejbjpa/entities/Employee.java b/ch9/relationshiptest/src/ejbjpa/entities/Employee.java new file mode 100644 index 0000000..9751c22 --- /dev/null +++ b/ch9/relationshiptest/src/ejbjpa/entities/Employee.java @@ -0,0 +1,48 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +@Entity +@Table(name = "EMPLOYEES") +public class Employee implements Serializable { + @Id + @Column(name = "EMPNO") + private Integer empno; + @Column(name = "firstname", nullable = false) + private String firstname; + @Column(name = "lastname", nullable = false) + private String lastname; + @OneToMany(mappedBy="employee", cascade = CascadeType.ALL) + private List orders; + public List getOrders(){ + return orders; + } + public void setOrders(List orders) { + this.orders = orders; + } + public Employee() { + } + public Integer getEmpno() { + return this.empno; + } + public void setEmpno(Integer empno) { + this.empno = empno; + } + public String getFirstname() { + return this.firstname; + } + public void setFirstname(String firstname) { + this.firstname = firstname; + } + public String getLastname() { + return this.lastname; + } + public void setLastname(String lastname) { + this.lastname = lastname; + } +} diff --git a/ch9/relationshiptest/src/ejbjpa/entities/Order.java b/ch9/relationshiptest/src/ejbjpa/entities/Order.java new file mode 100644 index 0000000..c550cad --- /dev/null +++ b/ch9/relationshiptest/src/ejbjpa/entities/Order.java @@ -0,0 +1,65 @@ +package ejbjpa.entities; +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.Temporal; +import static javax.persistence.TemporalType.DATE; +import javax.persistence.ManyToOne; +import javax.persistence.JoinColumn; +import java.util.Date; +@Entity +@Table(name = "ORDERS") +public class Order implements Serializable { + @Id + @Column(name = "PONO") + private Integer pono; + @Column(name = "SHIPPING_DATE", nullable = false) + @Temporal(DATE) + private Date shipping_date; + @Column(name = "DELIVERY_ESTIMATE", nullable = false) + private String delivery_estimate; + @ManyToOne + @JoinColumn( + name="CUST_ID", + referencedColumnName="CUST_ID") + private Customer customer; + @ManyToOne + @JoinColumn( + name="EMPNO", + referencedColumnName="EMPNO") + private Employee employee; + public Order() { + } + public Customer getCustomer() { + return this.customer; + } + public void setCustomer(Customer customer) { + this.customer = customer; + } + public Employee getEmployee() { + return this.employee; + } + public void setEmployee (Employee employee) { + this.employee = employee; + } + public Integer getPono () { + return this.pono; + } + public void setPono (Integer pono) { + this.pono = pono; + } + public Date getShipping_date () { + return this.shipping_date; + } + public void setShipping_date (Date shipping_date) { + this.shipping_date = shipping_date; + } + public String getDelivery_estimate () { + return this.delivery_estimate; + } + public void setDelivery_estimate (String delivery_estimate) { + this.delivery_estimate = delivery_estimate; + } +} diff --git a/ch9/relationshiptest/src/ejbjpa/servlets/RelationshipTestServlet.java b/ch9/relationshiptest/src/ejbjpa/servlets/RelationshipTestServlet.java new file mode 100644 index 0000000..e4c2846 --- /dev/null +++ b/ch9/relationshiptest/src/ejbjpa/servlets/RelationshipTestServlet.java @@ -0,0 +1,57 @@ +package ejbjpa.servlets; +import java.io.*; +import java.util.*; +import java.sql.*; +import javax.servlet.*; +import javax.servlet.http.*; +import javax.transaction.*; +import javax.annotation.Resource; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceUnit; +import java.util.List; +import ejbjpa.entities.*; +public class RelationshipTestServlet extends HttpServlet { + @PersistenceUnit + private EntityManagerFactory emf; + @Resource + private UserTransaction userTransaction; + public void doGet( + HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + response.setBufferSize(8192); + PrintWriter out = response.getWriter(); + EntityManager em= emf.createEntityManager(); + //Creating the order entity instances + Customer cust = (Customer) em.find(Customer.class, 2); + Employee emp = (Employee) em.find(Employee.class, 1); + List orders = new ArrayList(); + Order order1 = new Order(); + order1.setPono(10); + order1.setCustomer(cust); + order1.setEmployee(emp); + Order order2 = new Order(); + order2.setPono(11); + order2.setCustomer(cust); + order2.setEmployee(emp); + //Performing transaction + try{ + userTransaction.begin(); + out.println("Transaction began!"+"
"); + em.persist(order1); + em.persist(order2); + em.flush(); + em.refresh(cust); + orders = cust.getOrders(); + for (int i = 0; i < orders.size(); i++) { + out.println("order "+ orders.get(i).getPono()+ " placed by: " + orders.get(i).getCustomer().getCust_name() + "
"); + } + userTransaction.rollback(); + out.println("Transaction has been rolled back!"); + } + catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/ch9/relationshiptest/target/WEB-INF/classes/META-INF/persistence.xml b/ch9/relationshiptest/target/WEB-INF/classes/META-INF/persistence.xml new file mode 100644 index 0000000..ba39971 --- /dev/null +++ b/ch9/relationshiptest/target/WEB-INF/classes/META-INF/persistence.xml @@ -0,0 +1,6 @@ + + + + jdbc/mysqlpool + + diff --git a/ch9/relationshiptest/target/WEB-INF/classes/ejbjpa/entities/Customer.class b/ch9/relationshiptest/target/WEB-INF/classes/ejbjpa/entities/Customer.class new file mode 100644 index 0000000..855839f Binary files /dev/null and b/ch9/relationshiptest/target/WEB-INF/classes/ejbjpa/entities/Customer.class differ diff --git a/ch9/relationshiptest/target/WEB-INF/classes/ejbjpa/entities/Employee.class b/ch9/relationshiptest/target/WEB-INF/classes/ejbjpa/entities/Employee.class new file mode 100644 index 0000000..380c0cf Binary files /dev/null and b/ch9/relationshiptest/target/WEB-INF/classes/ejbjpa/entities/Employee.class differ diff --git a/ch9/relationshiptest/target/WEB-INF/classes/ejbjpa/entities/Order.class b/ch9/relationshiptest/target/WEB-INF/classes/ejbjpa/entities/Order.class new file mode 100644 index 0000000..7030ba9 Binary files /dev/null and b/ch9/relationshiptest/target/WEB-INF/classes/ejbjpa/entities/Order.class differ diff --git a/ch9/relationshiptest/target/WEB-INF/classes/ejbjpa/servlets/RelationshipTestServlet.class b/ch9/relationshiptest/target/WEB-INF/classes/ejbjpa/servlets/RelationshipTestServlet.class new file mode 100644 index 0000000..fb01b36 Binary files /dev/null and b/ch9/relationshiptest/target/WEB-INF/classes/ejbjpa/servlets/RelationshipTestServlet.class differ diff --git a/ch9/relationshiptest/target/WEB-INF/sun-web.xml b/ch9/relationshiptest/target/WEB-INF/sun-web.xml new file mode 100644 index 0000000..b9f3335 --- /dev/null +++ b/ch9/relationshiptest/target/WEB-INF/sun-web.xml @@ -0,0 +1,5 @@ + + + + /relationshiptest + diff --git a/ch9/relationshiptest/target/WEB-INF/web.xml b/ch9/relationshiptest/target/WEB-INF/web.xml new file mode 100644 index 0000000..217354b --- /dev/null +++ b/ch9/relationshiptest/target/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + + + RelationshipTestServlet + ejbjpa.servlets.RelationshipTestServlet + + + RelationshipTestServlet + /relationshiptestservlet + + + diff --git a/ch9/relationshiptest/target/relationshiptestservlet.war b/ch9/relationshiptest/target/relationshiptestservlet.war new file mode 100644 index 0000000..37745b4 Binary files /dev/null and b/ch9/relationshiptest/target/relationshiptestservlet.war differ diff --git a/contributing.md b/contributing.md new file mode 100644 index 0000000..f6005ad --- /dev/null +++ b/contributing.md @@ -0,0 +1,14 @@ +# Contributing to Apress Source Code + +Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. + +## How to Contribute + +1. Make sure you have a GitHub account. +2. Fork the repository for the relevant book. +3. Create a new branch on which to make your change, e.g. +`git checkout -b my_code_contribution` +4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. +5. Submit a pull request. + +Thank you for your contribution! \ No newline at end of file