Answer To: This assessment focuses on using Java Persistence API (JPA), Object Relational Mapping (ORM) and...
Prateek answered on Aug 24 2020
jpqldemo/pom.xml
4.0.0
com.tfth
jpqldemo
1.0-SNAPSHOT
jar
UTF-8
1.8
1.8
org.apache.derby
derbyclient
10.14.2.0
javax.persistence
javax.persistence-api
2.2
org.eclipse.persistence
eclipselink
2.5.2
org.eclipse.persistence
org.eclipse.persistence.jpa.modelgen.processor
2.5.2
provided
jpqldemo/src/main/java/com/tfth/jpqldemo/Car.java
jpqldemo/src/main/java/com/tfth/jpqldemo/Car.java
package com.tfth.jpqldemo;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name = "CAR")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name="model")
public abstract class Car implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private long carID;
private String make;
private String model;
private long year_of_manufacture;
private String registration_number;
private String transmission_type;
private int number_of_seats;
private String colour;
private long price;
public Car() {
}
Car(long carID,String make, String model,long year_of_manufacture,String registration_number,String transmission_type,int number_of_seats,String colour,long price){
this.carID = carID;
this.make = make;
this.model = model;
this.year_of_manufacture = year_of_manufacture;
this.registration_number = registration_number;
this.transmission_type = transmission_type;
this.number_of_seats = number_of_seats;
this.colour=colour;
this.price = price;
}
public Long getId() {
return carID;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
@Override
public String toString() {
return String.format("%d, %s, %s", this.carID, this.make, this.model);
}
}
jpqldemo/src/main/java/com/tfth/jpqldemo/Customer.java
jpqldemo/src/main/java/com/tfth/jpqldemo/Customer.java
package com.tfth.jpqldemo;
import java.io.Serializable;
import javax.persistence.*;
import java.util.*;
@Entity
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long customerID;
private String firstname;
private String lastname;
private String streetaddress;
private String suburb;
private String state;
private String postcode;
private String emailaddress;
private String mobilenumber;
public Customer() {
}
Customer(long customerID,String firstname,String lastname,String streetaddress,String suburb,String state,String postcode,String emailaddress,String mobilenumber) {
this.customerID = customerID;
this.firstname = firstname;
this.lastname = lastname;
this.streetaddress = streetaddress;
this.suburb = suburb;
this.state = state;
this.postcode = postcode;
this.emailaddress = emailaddress;
this.mobilenumber = mobilenumber;
}
public Long getcustomerID() {
return customerID;
}
public String getfirstname() {
return firstname;
}
public String getlastname() {
return lastname;
}
public String getstreetaddress() {
return streetaddress;
}
public String getsuburb() {
return suburb;
}
public String getstate() {
return state;
}
public String getpostcode() {
return postcode;
}
public String getemailaddress() {
return emailaddress;
}
public String getmobilenumber() {
return mobilenumber;
}
@Override
public String toString() {
return String.format("%d, %s , %s , %s , %s , %s , %s , %s, %s", this.customerID, this.firstname,this.lastname,this.streetaddress,this.suburb,this.state,this.postcode,this.emailaddress,this.mobilenumber);
}
}
jpqldemo/src/main/java/com/tfth/jpqldemo/Demo.java
jpqldemo/src/main/java/com/tfth/jpqldemo/Demo.java
package com.tfth.jpqldemo;
import javax.persistence.*;
import java.util.*;
public class Demo {
public static void main(String args[]){
Customer[] cust_arr = new Customer[10];
Car[] car_arr = new Car[10];
{
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("TFTHPU");
EntityManager em = emf.createEntityManager();
for (int i = 0; i < 10; i++) {
cust_arr[i] = new Customer(i,"FN"+i,"LN"+i,"test","test","test","test","test","test");
}
for (int i = 0; i < 5; i++) {
car_arr[i] = new Echo(i,"ABC","Echo",2000,"test","auto",4,"blue",10000,true,true,true,true,true);
}
for (int i = 5; i < 10; i++) {
car_arr[i] = new Liana(i,"ABC","Liana",2000,"test","auto",4,"blue",10000,true,true,true,true,true);
}
em.getTransaction().begin();
for (int i = 0; i < 10; i++) {
Sales s = new Sales(i,"05-06-2018",cust_arr[i],car_arr[i]);
em.persist(s);
}
em.getTransaction().commit();
//JPQL
//Total number of customer records
Query q1 = em.createQuery("SELECT COUNT(c) FROM Customer c");
System.out.println("Total Customers: " + q1.getSingleResult());
// Retrieve all the Sales objects from the database:
TypedQuery query =
em.createQuery("SELECT s FROM Sales s", Sales.class);
List results = query.getResultList();
for (Sales s : results) {
System.out.println(s);
}
em.close();
emf.close();
}
}
}
//Query q1 = em.createQuery("SELECT COUNT(c) FROM Customer c");
//System.out.println("Total Customers: " + q1.getSingleResult());
// Retrieve all the Customer objects from the database:
/*
TypedQuery query =
em.createQuery("SELECT c FROM Customer c", Customer.class);
List results = query.getResultList();
for (Customer c : results) {
System.out.println(c);
}
*/
jpqldemo/src/main/java/com/tfth/jpqldemo/Echo.java
jpqldemo/src/main/java/com/tfth/jpqldemo/Echo.java
package com.tfth.jpqldemo;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@DiscriminatorValue(value="Ëcho")
public class Echo extends Car implements Serializable {
private static final long serialVersionUID = 1L;
//@Id
//private long carID;
private boolean inbuilt_navigator;
private boolean audio;
private boolean airconditioning;
private boolean child_safety_lock;
private boolean fog_lights;
public Echo() {
}
Echo(long carID,String make, String model,long year_of_manufacture,String registration_number,String transmission_type,int number_of_seats,String colour,long price,boolean inbuilt_navigator,boolean audio,boolean airconditioning,boolean child_safety_lock,boolean fog_lights){
super(carID,make,model,year_of_manufacture,registration_number,transmission_type,number_of_seats,colour,price);
this.inbuilt_navigator=inbuilt_navigator;
this.audio=audio;
this.airconditioning=airconditioning;
this.child_safety_lock=child_safety_lock;
this.fog_lights=fog_lights;
}
}
jpqldemo/src/main/java/com/tfth/jpqldemo/Liana.java
jpqldemo/src/main/java/com/tfth/jpqldemo/Liana.java
package com.tfth.jpqldemo;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@DiscriminatorValue(value="Liana")
public class Liana extends Car implements Serializable {
private static final long serialVersionUID = 1L;
//@Id
//private long carID;
private boolean inbuilt_navigator;
private boolean audio;
private boolean airconditioning;
private boolean child_safety_lock;
private boolean fog_lights;
public Liana() {
}
Liana(long carID,String make, String model,long year_of_manufacture,String registration_number,String transmission_type,int number_of_seats,String colour,long price,boolean inbuilt_navigator,boolean audio,boolean airconditioning,boolean child_safety_lock,boolean fog_lights){
super(carID,make,model,year_of_manufacture,registration_number,transmission_type,number_of_seats,colour,price);
this.inbuilt_navigator=inbuilt_navigator;
this.audio=audio;
this.airconditioning=airconditioning;
this.child_safety_lock=child_safety_lock;
this.fog_lights=fog_lights;
}
}
jpqldemo/src/main/java/com/tfth/jpqldemo/Sales.java
jpqldemo/src/main/java/com/tfth/jpqldemo/Sales.java
package com.tfth.jpqldemo;
import java.io.Serializable;
import javax.persistence.*;
@Entity
public class Sales implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private long salesID;
private String date;
@ManyToOne(optional=false,cascade=CascadeType.PERSIST)
@JoinColumn(name = "customerID")
private Customer customer;
@OneToOne(optional=false,cascade=CascadeType.PERSIST)
@JoinColumn(name="carID")
private Car car;
public Sales() {
}
Sales(long salesID,String date,Customer cust,Car car) {
this.salesID = salesID;
this.date = date;
this.customer=cust;//customerID;
this.car = car;
}
public Long getsalesID() {
return salesID;
}
public String getdate() {
return date;
}
public Customer getCustomer() {
return customer;
}
public Car getCar() {
return car;
}
@Override
public String toString() {
return String.format("%d, %s, %d, %d", this.salesID, this.date, this.customer.getcustomerID(), this.car.getId());
}
}
jpqldemo/src/main/resources/META-INF/persistence.xml
org.eclipse.persistence.jpa.PersistenceProvider
com.tfth.jpqldemo.Echo
com.tfth.jpqldemo.Liana
com.tfth.jpqldemo.Car
com.tfth.jpqldemo.Customer
com.tfth.jpqldemo.Sales
jpqldemo/target/classes/.netbeans_automatic_build
jpqldemo/target/classes/com/tfth/jpqldemo/Car.class
package com.tfth.jpqldemo;
public abstract synchronized class Car implements java.io.Serializable {
private static final long serialVersionUID = 1;
private long carID;
private String make;
private String model;
private long year_of_manufacture;
private String registration_number;
private String transmission_type;
private int number_of_seats;
private String colour;
private long price;
public void Car();
void Car(long, String, String, long, String, String, int, String, long);
public Long getId();
public String getMake();
public String getModel();
public String toString();
}
jpqldemo/target/classes/com/tfth/jpqldemo/Customer.class
package com.tfth.jpqldemo;
public synchronized class Customer implements java.io.Serializable {
private static final long serialVersionUID = 1;
private Long customerID;
private String firstname;
private String lastname;
private String streetaddress;
private String suburb;
private String state;
private String postcode;
private String emailaddress;
private String mobilenumber;
public void Customer();
void Customer(long, String, String, String, String, String, String, String, String);
public Long getcustomerID();
public String getfirstname();
public String getlastname();
public String getstreetaddress();
public String getsuburb();
public String getstate();
public String getpostcode();
public String getemailaddress();
public String getmobilenumber();
public String toString();
}
jpqldemo/target/classes/com/tfth/jpqldemo/Demo.class
package com.tfth.jpqldemo;
public synchronized class Demo {
public void Demo();
public static void main(String[]);
}
jpqldemo/target/classes/com/tfth/jpqldemo/Echo.class
package com.tfth.jpqldemo;
public synchronized class Echo extends Car implements java.io.Serializable {
private static final long serialVersionUID = 1;
private boolean inbuilt_navigator;
private boolean audio;
private boolean airconditioning;
private boolean child_safety_lock;
private boolean fog_lights;
public void Echo();
void Echo(long, String, String, long, String, String, int, String, long, boolean, boolean, boolean, boolean, boolean);
}
jpqldemo/target/classes/com/tfth/jpqldemo/Liana.class
package com.tfth.jpqldemo;
public synchronized class Liana extends Car implements java.io.Serializable {
private static final long serialVersionUID = 1;
private boolean inbuilt_navigator;
private boolean audio;
private boolean airconditioning;
private boolean child_safety_lock;
private boolean fog_lights;
public void Liana();
void Liana(long, String, String, long, String, String, int, String, long, boolean, boolean, boolean, boolean, boolean);
}
jpqldemo/target/classes/com/tfth/jpqldemo/one/Car_.class
package com.tfth.jpqldemo.one;
public abstract synchronized class Car_ {
public static volatile javax.persistence.metamodel.SingularAttribute colour;
public static volatile javax.persistence.metamodel.SingularAttribute registration_number;
public static volatile javax.persistence.metamodel.SingularAttribute price;
public static volatile javax.persistence.metamodel.SingularAttribute model;
public static volatile javax.persistence.metamodel.SingularAttribute year_of_manufacture;
public static volatile javax.persistence.metamodel.SingularAttribute make;
public static volatile javax.persistence.metamodel.SingularAttribute transmission_type;
public static volatile javax.persistence.metamodel.SingularAttribute number_of_seats;
public static volatile javax.persistence.metamodel.SingularAttribute carID;
public void Car_();
}
jpqldemo/target/classes/com/tfth/jpqldemo/one/Customer_.class
package com.tfth.jpqldemo.one;
public synchronized class Customer_ {
public static volatile javax.persistence.metamodel.SingularAttribute firstname;
public static volatile javax.persistence.metamodel.SingularAttribute mobilenumber;
public static volatile javax.persistence.metamodel.SingularAttribute...