Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SDev3Repeat;
- import javax.persistence.*;
- import java.util.Calendar;
- @Entity
- @Table(name = "CASHIER")
- @SuppressWarnings("SerializableClass")
- public class Cashier {
- @Id
- private int cashierId;
- private String name;
- private String address;
- @Temporal(TemporalType.DATE)
- private Calendar DOB; //Date of Birth of the cashier
- @OneToOne
- @JoinColumn(name = "transID")
- private Bill bill;
- public Cashier(){
- }
- public Cashier(int cashierId, String name, String address, Calendar DOB) {
- this.cashierId = cashierId;
- this.name = name;
- this.address = address;
- this.DOB = DOB;
- }
- public int getCashierId() {
- return cashierId;
- }
- public void setCashierId(int cashierId) {
- this.cashierId = cashierId;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public Calendar getDOB() {
- return DOB;
- }
- public void setDOB(Calendar DOB) {
- this.DOB = DOB;
- }
- public Bill getTransaction() {
- return bill;
- }
- public void setTransaction(Bill bill) {
- this.bill = bill;
- }
- @Override
- public String toString(){
- String d = String.format("%1$s %2$tB %2$td, %2$tY",
- " Date of Birth:", DOB);
- return "Cashier ID: "+cashierId+" | Cashier Name: "+name+" | Cashier Address: "+address+" | Cashier Date Of Birth: "+d;
- }
- }
- package SDev3Repeat;
- import javax.persistence.*;
- @Entity
- @Table(name = "COFFEE")
- @SuppressWarnings("SerializableClass")
- public class Coffee {
- @Id
- private int coffeeId;
- private String type;
- private String size;
- private double price;
- @ManyToOne()
- @JoinColumn(name = "TRANSID")
- private Bill trans;
- public Coffee(){
- }
- public Coffee(int coffeeId, String type, String size, double price){
- this.coffeeId = coffeeId;
- this.type = type;
- this.size = size;
- this.price = price;
- }
- public int getCoffeeId() {
- return coffeeId;
- }
- public void setCoffeeId(int coffeeId) {
- this.coffeeId = coffeeId;
- }
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- public String getSize() {
- return size;
- }
- public void setSize(String size) {
- this.size = size;
- }
- public double getPrice() {
- return price;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- public void setTrans(Bill trans) {
- this.trans = trans;
- }
- @Override
- public String toString(){
- return " ID of Coffee: "+coffeeId+" | Type of Coffee: " + type + " | Size of Coffee: " + size + " | Price of Coffee: " + price;
- }
- }
- import java.util.List;
- import java.util.ArrayList;
- import javax.persistence.*;
- @Entity
- @Table(name = "BILL")
- @Inheritance( strategy = InheritanceType.JOINED )
- @SuppressWarnings("SerializableClass")
- public class Bill {
- @Id
- private int transId;
- private double bill;
- private final double VAT = .09; //VAT for hot food and drinks is 9%
- @OneToMany(mappedBy = "trans", cascade = CascadeType.PERSIST)
- private List<Coffee> clist = new ArrayList<>();
- @OneToOne(mappedBy="bill")
- private Cashier cashier;
- private String tempname = cashier.getName();
- public Bill() {
- }
- public Bill(int transId, double bill,String name) {
- this.transId = transId;
- this.bill = bill;
- this.tempname = name;
- }
- public int getTransId() {
- return transId;
- }
- public void setTransId(int transId) {
- this.transId = transId;
- }
- public double getBill() {
- return bill;
- }
- public void setBill(double bill) {
- this.bill = bill;
- }
- public String getTempname() {
- return tempname;
- }
- public void setTempname(String tempname) {
- this.tempname = tempname;
- }
- public List<Coffee> getClist() {
- return clist;
- }
- public void setClist(List<Coffee> clist) {
- this.clist = clist;
- }
- public Cashier getCashier() {
- return cashier;
- }
- public void setCashier(Cashier cashier) {
- this.cashier = cashier;
- }
- @Override
- public String toString() {
- String s="";
- for(int i=0;i<clist.size();i++) {
- s+= clist.get(i);
- }
- return s;
- }
- public double calcBill(Double tempBill){
- bill = tempBill + (tempBill * VAT);
- return bill;
- }
- }
- import java.util.Scanner;
- import java.util.Calendar;
- import javax.persistence.*;
- public class TestCoffee {
- public static void main(String[] args){
- EntityManagerFactory emf = Persistence.createEntityManagerFactory("SDEVRepeatCAPU");
- EntityManager em = emf.createEntityManager();
- Calendar dob = Calendar.getInstance();
- dob.set(1992, 06, 23);
- Cashier cashier = new Cashier(1,"John Smith", "123 South Street, Dublin",dob);
- em.getTransaction().begin();
- em.persist(cashier);
- em.getTransaction().commit();
- Scanner in = new Scanner(System.in);
- Coffee c = new Coffee(1,"Espresso","Solo",2.00);
- em.getTransaction().begin();
- em.persist(c);
- em.getTransaction().commit();
- c.setCoffeeId(2);
- c.setType("Espresso");
- c.setSize("Doppio");
- c.setPrice(2.35);
- em.getTransaction().begin();
- em.persist(c);
- em.getTransaction().commit();
- c.setCoffeeId(3);
- c.setType("Americano");
- c.setSize("Large");
- c.setPrice(2.85);
- em.getTransaction().begin();
- em.persist(c);
- em.getTransaction().commit();
- c.setCoffeeId(4);
- c.setType("Americano");
- c.setSize("Medium");
- c.setPrice(2.50);
- em.getTransaction().begin();
- em.persist(c);
- em.getTransaction().commit();
- c.setCoffeeId(5);
- c.setType("Cappuccino");
- c.setSize("Large");
- c.setPrice(3.45);
- em.getTransaction().begin();
- em.persist(c);
- em.getTransaction().commit();
- c.setCoffeeId(6);
- c.setType("Cappuccino");
- c.setSize("Medium");
- c.setPrice(3.00);
- em.getTransaction().begin();
- em.persist(c);
- em.getTransaction().commit();
- c.setCoffeeId(7);
- c.setType("Flat white");
- c.setSize("Small");
- c.setPrice(3.25);
- em.getTransaction().begin();
- em.persist(c);
- em.getTransaction().commit();
- c.setCoffeeId(8);
- c.setType("Caffè Latte");
- c.setSize("Large");
- c.setPrice(3.45);
- em.getTransaction().begin();
- em.persist(c);
- em.getTransaction().commit();
- c.setCoffeeId(9);
- c.setType("Caffè Latte");
- c.setSize("Medium");
- c.setPrice(3.00);
- em.getTransaction().begin();
- em.persist(c);
- em.getTransaction().commit();
- c.setCoffeeId(10);
- c.setType("Caffè Mocha");
- c.setSize("Large");
- c.setPrice(4.00);
- em.getTransaction().begin();
- em.persist(c);
- em.getTransaction().commit();
- c.setCoffeeId(11);
- c.setType("Caffè Mocha");
- c.setSize("Medium");
- c.setPrice(3.50);
- em.getTransaction().begin();
- em.persist(c);
- em.getTransaction().commit();
- Bill b = new Bill(1,10.20,"John Smith");
- System.out.println("Would you like to Create(Enter C),Read(Enter R),Update(Enter U) or Delete(Enter D):");
- String choice = in.nextLine();
- int IdChoice;
- int transId = 1;
- int coffId = 11;
- int cashId = 1;
- String cont = "y";
- while("y".equalsIgnoreCase(cont)){
- if("C".equalsIgnoreCase(choice)){ //finished
- System.out.println("From what class would you like to Create? Transaction(Enter T), Cashier(enter Cash) or Coffee(Enter Coff):");
- choice = in.nextLine();
- if("T".equalsIgnoreCase(choice)){ //finished
- System.out.println("Please enter Total for bill - vat:");
- double tempbill = in.nextDouble();
- b.setBill(b.calcBill(tempbill));
- transId++;
- b.setTransId(transId);
- System.out.println("Please enter Cashiers ID:");
- IdChoice = in.nextInt();
- cashier = em.find(Cashier.class, IdChoice);
- b.setTempname(cashier.getName());
- em.getTransaction().begin();
- em.persist(b);
- em.getTransaction().commit();
- }else if("Cash".equalsIgnoreCase(choice)){ //finished
- System.out.println("Please enter cashiers Name:");
- cashier.setName(in.nextLine());
- System.out.println("Please enter cashiers Address:");
- cashier.setAddress(in.nextLine());
- System.out.println("Please enter Year of Birth(YYYY):");
- int YOB = in.nextInt();
- System.out.println("Please enter Month of Birth(MM):");
- int MOB = in.nextInt();
- System.out.println("Please enter Day of Birth(DD):");
- int dayOB = in.nextInt();
- dob.set(YOB,MOB,dayOB);
- cashier.setDOB(dob);
- cashId++;
- cashier.setCashierId(cashId);
- em.getTransaction().begin();
- em.persist(cashier);
- em.getTransaction().commit();
- }else if("Coff".equalsIgnoreCase(choice)){ //finished
- System.out.println("Please Enter Name of Coffee:");
- c.setType(in.nextLine());
- System.out.println("Please Enter Size of Coffee:");
- c.setSize(in.nextLine());
- System.out.println("Please Enter Price of Coffee:");
- c.setPrice(in.nextDouble());
- coffId++;
- c.setCoffeeId(coffId);
- em.getTransaction().begin();
- em.persist(c);
- em.getTransaction().commit();
- }else{
- System.out.println("Error Incorrect value");
- }
- }else if("R".equalsIgnoreCase(choice)){ //finished
- System.out.println("From what class would you like to Read? Transaction(Enter T), Cashier(enter Cash) or Coffee(Enter Coff):");
- choice = in.nextLine();
- if("T".equalsIgnoreCase(choice)){
- System.out.println("Enter Transaction Id that you wish to view");
- IdChoice = in.nextInt();
- b = em.find(Bill.class,IdChoice);
- System.out.println(b);
- }else if("Cash".equalsIgnoreCase(choice)){
- System.out.println("Enter Cashier Id that you wish to view");
- IdChoice = in.nextInt();
- cashier = em.find(Cashier.class, IdChoice);
- System.out.println(cashier);
- }else if("Coff".equalsIgnoreCase(choice)){
- System.out.println("Enter Coffee Id that you wish to view");
- IdChoice = in.nextInt();
- c = em.find(Coffee.class, IdChoice);
- System.out.println(c);
- }else{
- System.out.println("Error Incorrect value");
- }
- }else if("U".equalsIgnoreCase(choice)){ //FINISHED
- System.out.println("From what class would you like to Update? Transaction(Enter T), Cashier(enter Cash) or Coffee(Enter Coff):");
- choice = in.nextLine();
- if("T".equalsIgnoreCase(choice)){
- System.out.println("Enter Transaction Id that you wish to Update");
- IdChoice = in.nextInt();
- b = em.find(Bill.class, IdChoice);
- System.out.println("What would you like to update? Cashier name(C) or Bill(B)");
- choice = in.nextLine();
- if("C".equalsIgnoreCase(choice)){
- System.out.println("Enter Cashier Id you wish to use:");
- IdChoice = in.nextInt();
- cashier = em.find(Cashier.class, IdChoice);
- b.setTempname(cashier.getName());
- }else if("B".equalsIgnoreCase(choice)){
- System.out.println("Enter new Bill Amount:");
- double tempbill = in.nextDouble();
- b.setBill(b.calcBill(tempbill));
- }else{
- System.out.println("Error Incorrect value");
- }
- }else if("Cash".equalsIgnoreCase(choice)){
- System.out.println("Enter Cashier Id that you wish to Update");
- IdChoice = in.nextInt();
- cashier = em.find(Cashier.class, IdChoice);
- System.out.println("What would you like to update? Cashier name(N) Or Address(A)"); // No date of birth option as you cant change when you are born
- choice = in.nextLine();
- if("N".equalsIgnoreCase(choice)){
- System.out.println("Enter New Name:");
- cashier.setName(in.nextLine());
- }else if("A".equalsIgnoreCase(choice)){
- System.out.println("Enter New Address:");
- cashier.setAddress(in.nextLine());
- }else{
- System.out.println("Error Incorrect value");
- }
- em.getTransaction().begin();
- em.persist(cashier);
- em.getTransaction().commit();
- }else if("Coff".equalsIgnoreCase(choice)){
- System.out.println("Enter Coffee Id that you wish to Update");
- IdChoice = in.nextInt();
- c = em.find(Coffee.class, IdChoice);
- System.out.println("Whar would you like to update? Coffee name(N),Size(S) or Price(P)");
- choice = in.nextLine();
- if("N".equalsIgnoreCase(choice)){
- System.out.println("Please Enter New Name of Coffee:");
- c.setType(in.nextLine());
- }else if("S".equalsIgnoreCase(choice)){
- System.out.println("Please Enter New Size of Coffee:");
- c.setSize(in.nextLine());
- }else if("P".equalsIgnoreCase(choice)){
- System.out.println("Please Enter Price of Coffee:");
- c.setPrice(in.nextDouble());
- }else{
- System.out.println("Error Incorrect value");
- }
- em.getTransaction().begin();
- em.persist(c);
- em.getTransaction().commit();
- }else{
- System.out.println("Error Incorrect value");
- }
- }else if("D".equalsIgnoreCase(choice)){ //UNFINISHED
- System.out.println("From what class would you like to Delete? Transaction(Enter T), Cashier(enter Cash) or Coffee(Enter Coff):");
- choice = in.nextLine();
- if("T".equalsIgnoreCase(choice)){
- System.out.println("Enter Transaction Id that you wish to Delete");
- IdChoice = in.nextInt();
- b = em.find(Bill.class, IdChoice);
- em.getTransaction().begin();
- em.remove(b);
- em.getTransaction().commit();
- }else if("Cash".equalsIgnoreCase(choice)){
- System.out.println("Enter Cashier Id that you wish to Delete");
- IdChoice = in.nextInt();
- cashier = em.find(Cashier.class, IdChoice);
- em.getTransaction().begin();
- em.remove(cashier);
- em.getTransaction().commit();
- }else if("Coff".equalsIgnoreCase(choice)){
- System.out.println("Enter Coffee Id that you wish to Delete");
- IdChoice = in.nextInt();
- c = em.find(Coffee.class, IdChoice);
- em.getTransaction().begin();
- em.remove(c);
- em.getTransaction().commit();
- }else{
- System.out.println("Error Incorrect value");
- }
- }else{
- System.out.println("Error Incorrect value");
- }
- System.out.println("Do you want to continue Y or N:");
- cont = in.nextLine();
- }
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
- <persistence-unit name="SDEVRepeatCAPU" transaction-type="RESOURCE_LOCAL">
- <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
- <class>SDev3Repeat.Cashier</class>
- <class>SDev3Repeat.Coffee</class>
- <class>SDev3Repeat.Bill</class>
- <properties>
- <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
- <property name="javax.persistence.jdbc.user" value="hr"/>
- <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
- <property name="javax.persistence.jdbc.password" value="passhr"/>
- <!-- <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@10.10.2.7:1521/global1"/>
- <property name="javax.persistence.jdbc.user" value=""/>
- <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
- <property name="javax.persistence.jdbc.password" value=""/>-->
- <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
- </properties>
- [EL Info]: 2017-08-22 17:45:37.248--ServerSession(1203142603)--EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd
- [EL Severe]: 2017-08-22 17:45:38.744--ServerSession(1203142603)--Exception
- [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd):
- org.eclipse.persistence.exceptions.IntegrityException
- Descriptor Exceptions:
- ---------------------------------------------------------
- Exception [EclipseLink-168] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
- Exception Description: Problem in creating new instance using the default constructor. The default constructor triggered an exception.
- Internal Exception: java.lang.reflect.InvocationTargetException
- Target Invocation Exception: java.lang.NullPointerException
- Descriptor: RelationalDescriptor(SDev3Repeat.Bill --> [DatabaseTable(BILL)])
- Runtime Exceptions:
- ---------------------------------------------------------
- [EL Info]: connection: 2017-08-22 17:45:38.749--ServerSession(1203142603)--file:/C:/Users/Aran/Documents/NetBeansProjects/SDEV3RepeatCA/build/classes/_SDEVRepeatCAPU logout successful
- Exception in thread "main" javax.persistence.PersistenceException: Exception
- [EclipseLink-28019] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.EntityManagerSetupException
- Exception Description: Deployment of PersistenceUnit [SDEVRepeatCAPU] failed. Close all factories for this PersistenceUnit.
- [EL Severe]: ejb: 2017-08-22 17:45:38.758--ServerSession(1203142603)--Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.IntegrityException
- Descriptor Exceptions:
- ---------------------------------------------------------
- Internal Exception: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.IntegrityException
- Descriptor Exceptions:
- ---------------------------------------------------------
- Exception [EclipseLink-168] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
- Exception Description: Problem in creating new instance using the default constructor. The default constructor triggered an exception.
- Exception [EclipseLink-168] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
- Internal Exception: java.lang.reflect.InvocationTargetException
- Target Invocation Exception: java.lang.NullPointerException
- Exception Description: Problem in creating new instance using the default constructor. The default constructor triggered an exception.
- Descriptor: RelationalDescriptor(SDev3Repeat.Bill --> [DatabaseTable(BILL)])
- Internal Exception: java.lang.reflect.InvocationTargetException
- Runtime Exceptions:
- ---------------------------------------------------------
- Target Invocation Exception: java.lang.NullPointerException
- Descriptor: RelationalDescriptor(SDev3Repeat.Bill --> [DatabaseTable(BILL)])
- Runtime Exceptions:
- ---------------------------------------------------------
- at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createDeployFailedPersistenceException(EntityManagerSetupImpl.java:820)
- at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:760)
- at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:204)
- at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:182)
- at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getDatabaseSession(EntityManagerFactoryImpl.java:527)
- at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:140)
- at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:177)
- at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
- at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
- at SDev3Repeat.TestCoffee.main(TestCoffee.java:15)
- Caused by: Exception [EclipseLink-28019] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.EntityManagerSetupException
- Exception Description: Deployment of PersistenceUnit [SDEVRepeatCAPU] failed. Close all factories for this PersistenceUnit.
- Internal Exception: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.IntegrityException
- Descriptor Exceptions:
- ---------------------------------------------------------
- Exception [EclipseLink-168] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
- Exception Description: Problem in creating new instance using the default constructor. The default constructor triggered an exception.
- Internal Exception: java.lang.reflect.InvocationTargetException
- Target Invocation Exception: java.lang.NullPointerException
- Descriptor: RelationalDescriptor(SDev3Repeat.Bill --> [DatabaseTable(BILL)])
- Runtime Exceptions:
- ---------------------------------------------------------
- at org.eclipse.persistence.exceptions.EntityManagerSetupException.deployFailed(EntityManagerSetupException.java:238)
- ... 10 more
- Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.IntegrityException
- Descriptor Exceptions:
- ---------------------------------------------------------
- Exception [EclipseLink-168] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
- Exception Description: Problem in creating new instance using the default constructor. The default constructor triggered an exception.
- Internal Exception: java.lang.reflect.InvocationTargetException
- Target Invocation Exception: java.lang.NullPointerException
- Descriptor: RelationalDescriptor(SDev3Repeat.Bill --> [DatabaseTable(BILL)])
- Runtime Exceptions:
- ---------------------------------------------------------
- at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:696)
- at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:632)
- at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:568)
- at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.postConnectDatasource(DatabaseSessionImpl.java:799)
- at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:743)
- at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:239)
- at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:685)
- ... 8 more
- Java Result: 1
- BUILD SUCCESSFUL (total time: 4 seconds)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement