Last active
January 11, 2019 22:37
-
-
Save EnioRich/e5dbc5508945b9acbe5097d731edbf34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.*; | |
@Entity | |
@Table(name = "apartment") | |
public class Apartment { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
@Column(name = "id") | |
private int id; | |
@Column(name ="district", nullable = false) | |
private double district; | |
@Column(name ="address", nullable = false) | |
private String address; | |
private double square; | |
private int rooms; | |
private double price; | |
public Apartment(double district, String address, double square, int rooms, double price) { | |
this.district = district; | |
this.address = address; | |
this.square = square; | |
this.rooms = rooms; | |
this.price = price; | |
} | |
public Apartment() { | |
} | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public double getDistrict() { | |
return district; | |
} | |
public void setDistrict(double district) { | |
this.district = district; | |
} | |
public String getAddress() { | |
return address; | |
} | |
public void setAddress(String address) { | |
this.address = address; | |
} | |
public double getSquare() { | |
return square; | |
} | |
public void setSquare(double square) { | |
this.square = square; | |
} | |
public int getRooms() { | |
return rooms; | |
} | |
public void setRooms(int rooms) { | |
this.rooms = rooms; | |
} | |
public double getPrice() { | |
return price; | |
} | |
public void setPrice(double price) { | |
this.price = price; | |
} | |
@Override | |
public String toString() { | |
return "Apartment [" + | |
"id=" + id + | |
", district = " + district + | |
", address = '" + address + '\'' + | |
", square = " + square + | |
", rooms = " + rooms + | |
", price = " + price + | |
']'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.EntityManager; | |
import java.util.Scanner; | |
/* addition to the method changeApartmentParameter in Implementation */ | |
public class ApartmentDataBaseSetters { | |
ApartmentDataBaseSetters (){} | |
static void setDistrict (Scanner scanner, Apartment a, EntityManager em) { | |
System.out.println("Enter new district: "); | |
int newDistrict = scanner.nextInt(); | |
em.getTransaction().begin(); | |
try { | |
a.setDistrict(newDistrict); | |
em.getTransaction().commit(); | |
} catch (Exception e) { | |
em.getTransaction().rollback(); | |
} | |
} | |
static void setAddress (Scanner scanner, Apartment a, EntityManager em) { | |
System.out.println("Enter new address: "); | |
String newAddress = scanner.nextLine(); | |
while (newAddress.isEmpty()) { | |
newAddress = scanner.nextLine(); | |
} | |
em.getTransaction().begin(); | |
try { | |
a.setAddress(newAddress); | |
em.getTransaction().commit(); | |
} catch (Exception e) { | |
em.getTransaction().rollback(); | |
} | |
} | |
static void setSquare (Scanner scanner, Apartment a, EntityManager em) { | |
System.out.println("Enter new square: "); | |
double newSquare = scanner.nextDouble(); | |
em.getTransaction().begin(); | |
try { | |
a.setSquare(newSquare); | |
em.getTransaction().commit(); | |
} catch (Exception e) { | |
em.getTransaction().rollback(); | |
} | |
} | |
static void setRoomsCount (Scanner scanner, Apartment a, EntityManager em) { | |
System.out.println("Enter new count of rooms: "); | |
int newRooms = scanner.nextInt(); | |
em.getTransaction().begin(); | |
try { | |
a.setRooms(newRooms); | |
em.getTransaction().commit(); | |
} catch (Exception e) { | |
em.getTransaction().rollback(); | |
} | |
} | |
static void setPrice (Scanner scanner, Apartment a, EntityManager em) { | |
System.out.println("Enter new price: "); | |
double newPrice = scanner.nextDouble(); | |
em.getTransaction().begin(); | |
try { | |
a.setPrice(newPrice); | |
em.getTransaction().commit(); | |
} catch (Exception e) { | |
em.getTransaction().rollback(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.EntityManager; | |
import javax.persistence.NoResultException; | |
import javax.persistence.NonUniqueResultException; | |
import javax.persistence.Query; | |
/* addition to the method changeApartmentParameter in Implementation */ | |
public class ApartmentSelection { | |
ApartmentSelection(){} | |
static Apartment selectApartment(int id, EntityManager em){ | |
Apartment a = null; | |
try { | |
Query query = em.createQuery("SELECT a FROM Apartment a WHERE a.id = :id", Apartment.class); | |
query.setParameter("id", id); | |
a = (Apartment) query.getSingleResult(); | |
} catch (NoResultException e) { | |
System.out.println("Apartment not found"); | |
} catch (NonUniqueResultException e) { | |
System.out.println("Unique result"); | |
} | |
return a; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.EntityManager; | |
import javax.persistence.Query; | |
import java.util.List; | |
import java.util.Scanner; | |
public class Implementation { | |
private static EntityManager em; | |
Implementation(EntityManager em) { | |
Implementation.em = em; | |
} | |
void addApartment(Scanner scanner) { | |
System.out.println("Enter number of district: "); | |
int district = scanner.nextInt(); | |
System.out.println("Enter address: "); | |
String address = scanner.nextLine(); | |
while (address.isEmpty()) { | |
address = scanner.nextLine(); | |
} | |
System.out.println("Enter square: "); | |
double square = scanner.nextDouble(); | |
System.out.println("Enter number of rooms: "); | |
int rooms = scanner.nextInt(); | |
System.out.println("Enter price: "); | |
double price = scanner.nextDouble(); | |
em.getTransaction().begin(); | |
try { | |
Apartment apartment = new Apartment(district, address, square, rooms, price); | |
em.persist(apartment); | |
em.getTransaction().commit(); | |
} catch (Exception e) { | |
em.getTransaction().rollback(); | |
} | |
} | |
void deleteApartment(Scanner scanner) { | |
System.out.println("Enter client id: "); | |
int id = scanner.nextInt(); | |
System.out.println("Deleted!"); | |
Apartment apartment = em.find(Apartment.class, id); | |
if (apartment == null) { | |
System.out.println("Apartment not found"); | |
return; | |
} | |
em.getTransaction().begin(); | |
try { | |
em.remove(apartment); | |
em.getTransaction().commit(); | |
} catch (Exception e) { | |
em.getTransaction().rollback(); | |
} | |
} | |
void showApartments() { | |
Query query = em.createQuery("SELECT a FROM Apartment a", Apartment.class); | |
List apartments = query.getResultList(); | |
System.out.println(); | |
if (apartments.isEmpty()) { | |
System.out.println("Empty list. Try to add apartments"); | |
} | |
int count = 1; | |
for (Object a : apartments) { | |
System.out.println(count + " " + a); | |
count++; | |
} | |
} | |
void changeApartmentParameter(Scanner scanner) { | |
// For this method two additional classes with static methods are introduced: | |
// ApartmentSelection and ApartmentDataBaseSetters | |
System.out.println("Enter apartment id: "); | |
int id = scanner.nextInt(); | |
Apartment a = ApartmentSelection.selectApartment(id, em); | |
while (true) { | |
System.out.println("Enter what you wanna to change: "); | |
System.out.println("1. District"); | |
System.out.println("2. Address"); | |
System.out.println("3. Square"); | |
System.out.println("4. Rooms"); | |
System.out.println("5. Price"); | |
System.out.println(); | |
System.out.println("Choose number of operation: "); | |
int number = scanner.nextInt(); | |
switch (number) { | |
case 1: | |
ApartmentDataBaseSetters.setDistrict(scanner, a, em); | |
return; | |
case 2: | |
ApartmentDataBaseSetters.setAddress(scanner, a, em); | |
return; | |
case 3: | |
ApartmentDataBaseSetters.setSquare(scanner, a, em); | |
return; | |
case 4: | |
ApartmentDataBaseSetters.setRoomsCount(scanner, a, em); | |
return; | |
case 5: | |
ApartmentDataBaseSetters.setPrice(scanner, a, em); | |
return; | |
default: | |
System.out.println("There is no parameter like that"); | |
System.out.println("Try again"); | |
System.out.println(); | |
break; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.EntityManager; | |
import javax.persistence.EntityManagerFactory; | |
import javax.persistence.Persistence; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPA"); | |
EntityManager em = emf.createEntityManager(); | |
Implementation impl = new Implementation(em); | |
try { | |
try { | |
while (true) { | |
System.out.println(); | |
System.out.println("1. Add Apartment: "); | |
System.out.println("2. Delete Apartment: "); | |
System.out.println("3. Change apartment characteristic"); | |
System.out.println("4. Show Apartments: "); | |
System.out.println("5. Exit"); | |
System.out.println(); | |
System.out.println("Choose number of operation: "); | |
int choose = scanner.nextInt(); | |
switch (choose) { | |
case 1: | |
impl.addApartment(scanner); | |
break; | |
case 2: | |
impl.deleteApartment(scanner); | |
break; | |
case 3: | |
impl.changeApartmentParameter(scanner); | |
break; | |
case 4: | |
impl.showApartments(); | |
break; | |
case 5: | |
return; | |
default: | |
System.out.println("There is no operation like that"); | |
System.out.println("Try again"); | |
System.out.println(); | |
break; | |
} | |
} | |
} finally { | |
scanner.close(); | |
em.close(); | |
emf.close(); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<persistence xmlns="http://java.sun.com/xml/ns/persistence" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" | |
version="2.0"> | |
<persistence-unit name="JPA"> | |
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> | |
<properties> | |
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> | |
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hhw"/> | |
<property name="javax.persistence.jdbc.user" value="root" /> | |
<property name="javax.persistence.jdbc.password" value="12345" /> | |
<property name="hibernate.show_sql" value="true" /> | |
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> | |
<property name="hibernate.hbm2ddl.auto" value="create"/> | |
</properties> | |
</persistence-unit> | |
</persistence> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment