Skip to content

Instantly share code, notes, and snippets.

@LebedevVA2018
Created January 28, 2019 12:16
Show Gist options
  • Save LebedevVA2018/5c4a523a12df6802ccf6e4af8880c44b to your computer and use it in GitHub Desktop.
Save LebedevVA2018/5c4a523a12df6802ccf6e4af8880c44b to your computer and use it in GitHub Desktop.
package orderHibernate.model;
import javax.persistence.*;
@Entity
@Table(name = "Clients")
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "client_id")
private long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String surname;
@Column(nullable = false)
private int phone;
@Column(nullable = false)
private String e_mail;
public Client() {
}
public Client(String name, String surname, int phone, String e_mail) {
this.name = name;
this.surname = surname;
this.phone = phone;
this.e_mail = e_mail;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public long getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public String getE_mail() {
return e_mail;
}
public void setE_mail(String e_mail) {
this.e_mail = e_mail;
}
@Override
public String toString() {
return "Client{" +
"id=" + id +
", name='" + name + '\'' +
", surname='" + surname + '\'' +
", phone=" + phone +
", e_mail='" + e_mail + '\'' +
'}' + "\n";
}
}
package orderHibernate.dao;
import javax.persistence.*;
import java.sql.SQLException;
import java.util.Scanner;
public interface ClientDAO {
//create
void add(Scanner scanner, EntityManager em) throws SQLException;
//read
void getAll(EntityManager em) throws SQLException;
}
package orderHibernate.services;
import orderHibernate.dao.ClientDAO;
import orderHibernate.model.Client;
import javax.persistence.*;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
public class ClientDAOImpl implements ClientDAO {
@Override
public void add(Scanner scanner, EntityManager em) throws SQLException {
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter surname: ");
String surname = scanner.nextLine();
System.out.print("Enter phone: ");
String sPhone = scanner.nextLine();
int phone = Integer.parseInt(sPhone);
System.out.print("Enter e_mail: ");
String e_mail = scanner.nextLine();
em.getTransaction().begin();
try {
Client client = new Client(name, surname, phone, e_mail);
em.persist(client);
em.getTransaction().commit();
} catch (Exception ex) {
em.getTransaction().rollback();
}
}
@Override
public void getAll(EntityManager em) throws SQLException {
Query query = em.createQuery("SELECT c FROM Client c", Client.class);
List<Client> clientList = query.getResultList();
for (Client client : clientList) {
System.out.println(client);
}
}
}
package orderHibernate.model;
import javax.persistence.*;
@Entity
@Table(name = "Orders_Hibernate")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "order_id")
private long id;
@Column(name = "id_client")
private long id_client;
public Order() {
}
public Order(long id_client) {
this.id_client = id_client;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getId_client() {
return id_client;
}
public void setId_client(int id_client) {
this.id_client = id_client;
}
@Override
public String toString() {
return "Order{" +
"id=" + id +
", id_client=" + id_client +
'}' + "\n";
}
}
package orderHibernate.services;
import orderHibernate.model.Order;
import orderHibernate.model.ProductInOrder;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
public class OrderManager {
public void createOrder(Scanner scanner, EntityManager em) throws SQLException {
System.out.print("Enter id client: ");
String sId_client = scanner.nextLine();
long id_client = Long.parseLong(sId_client);
em.getTransaction().begin();
try {
Order order = new Order(id_client);
em.persist(order);
em.getTransaction().commit();
} catch (Exception ex) {
em.getTransaction().rollback();
}
}
public void getAll(EntityManager em) throws SQLException {
Query query = em.createQuery("SELECT c FROM Order c", Order.class);
List<Order> orderList = query.getResultList();
//MySQL: select o.order_id, c.e_mail from clients c, orders_hibernate o where o.id_client = c.client_id;
//Hibernate???
for (Order order : orderList) {
System.out.println(order);
}
}
public void addProductToOrder(Scanner scanner, EntityManager em) throws SQLException {
System.out.print("Enter id order: ");
String sId_order = scanner.nextLine();
long id_order = Long.parseLong(sId_order);
System.out.print("Enter id product: ");
String sId_product = scanner.nextLine();
long id_product = Long.parseLong(sId_product);
System.out.print("Enter the quantity of products: ");
String sAmount = scanner.nextLine();
int amount = Integer.parseInt(sAmount);
em.getTransaction().begin();
try {
ProductInOrder productInOrder = new ProductInOrder(id_order, id_product, amount);
em.persist(productInOrder);
em.getTransaction().commit();
} catch (Exception ex) {
em.getTransaction().rollback();
}
}
public void getAllProductsInOrders(EntityManager em) throws SQLException {
Query query = em.createQuery("SELECT c FROM ProductInOrder c", ProductInOrder.class);
List<ProductInOrder> productInOrderList = query.getResultList();
for (ProductInOrder productInOrder : productInOrderList) {
System.out.println(productInOrder);
}
}
}
package orderHibernate.model;
import javax.persistence.*;
@Entity
@Table(name = "Products")
@NamedQuery(name = "Products.getAll", query = "SELECT c from Product c")
public class Product {
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
@Column(name = "product_id")
private long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private int price;
public Product() {
}
public Product(String name, int price) {
this.name = name;
this.price = price;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
'}' + "\n";
}
}
package orderHibernate.model;
import javax.persistence.*;
@Entity
@Table(name = "ProductInOrder")
public class ProductInOrder {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "id_order")
private long id_order;
@Column(name = "id_product")
private long id_product;
private int amount;
public ProductInOrder() {
}
public ProductInOrder(long id_order, long id_product, int amount) {
this.id_order = id_order;
this.id_product = id_product;
this.amount = amount;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getId_order() {
return id_order;
}
public void setId_order(long id_order) {
this.id_order = id_order;
}
public long getId_product() {
return id_product;
}
public void setId_product(long id_product) {
this.id_product = id_product;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
@Override
public String toString() {
return "ProductInOrder{" +
"id=" + id +
", id_order=" + id_order +
", id_product=" + id_product +
", amount=" + amount +
'}';
}
}
package orderHibernate.services;
import orderHibernate.model.Product;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
public class ProductService {
public void add(Scanner scanner, EntityManager em) throws SQLException {
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter price: ");
String sPrice = scanner.nextLine();
int price = Integer.parseInt(sPrice);
em.getTransaction().begin();
try {
Product product = new Product(name, price);
em.persist(product);
em.getTransaction().commit();
} catch (Exception ex) {
em.getTransaction().rollback();
}
}
public void getAll(EntityManager em) throws SQLException {
TypedQuery<Product> namedQuery = em.createNamedQuery("Products.getAll", Product.class);
List<Product> clientList = namedQuery.getResultList();
for (Product product : clientList) {
System.out.println(product);
}
}
}
package orderHibernate;
import orderHibernate.services.ClientDAOImpl;
import orderHibernate.services.OrderManager;
import orderHibernate.services.ProductService;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.Scanner;
public class Runner {
static EntityManagerFactory emf;
static EntityManager em;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
try {
// create connection
emf = Persistence.createEntityManagerFactory("JPATest");
em = emf.createEntityManager();
ClientDAOImpl clientDAO = new ClientDAOImpl();
ProductService productService = new ProductService();
OrderManager orderManager = new OrderManager();
while (true) {
System.out.println("1: add client");
System.out.println("2: show all clients");
System.out.println("3: add product");
System.out.println("4: show all products");
System.out.println("5: create order");
System.out.println("6: show all orders");
System.out.println("7: add product to order");
System.out.println("8: show products in orders");
System.out.print("-> ");
String s = scanner.nextLine();
switch (s) {
case "1":
clientDAO.add(scanner, em);
break;
case "2":
clientDAO.getAll(em);
break;
case "3":
productService.add(scanner, em);
break;
case "4":
productService.getAll(em);
break;
case "5":
orderManager.createOrder(scanner, em);
break;
case "6":
orderManager.getAll(em);
break;
case "7":
orderManager.addProductToOrder(scanner, em);
break;
case "8":
orderManager.getAllProductsInOrders(em);
break;
default:
return;
}
}
} finally {
scanner.close();
em.close();
emf.close();
}
} catch (Exception ex) {
ex.printStackTrace();
return;
}
}
}
//-> 2
//Client{id=1, name='Petro', surname='Petrov', phone=55555, e_mail='petrov.p@ukr.net'}
//-> 4
//Product{id=2, name='Acer Nitro 5', price=19999}
//-> 6
//Order{id=3, id_client=1}
//-> 8
//ProductInOrder{id=4, id_order=3, id_product=2, amount=4}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment