-
Notifications
You must be signed in to change notification settings - Fork 1
/
RemoveTowns.java
65 lines (52 loc) · 2.58 KB
/
RemoveTowns.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package exercises;
import entities.Address;
import entities.Employee;
import entities.Town;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
public class RemoveTowns {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
EntityManagerFactory factory = Persistence.createEntityManagerFactory("PersistenceUnit");
EntityManager entityManager = factory.createEntityManager();
System.out.println("Enter town for delete: ");
String townForDelete = reader.readLine();
entityManager.getTransaction().begin();
setEmployeeAdressToNull(entityManager, townForDelete);
List<Address> addresses = deleteAddressesFromTheGivenTown(entityManager, townForDelete);
deleteTownWithTheGivenName(entityManager, townForDelete);
System.out.println(String.format("%d address in %s deleted", addresses.size(), townForDelete));
entityManager.getTransaction().commit();
entityManager.close();
factory.close();
}
private static void deleteTownWithTheGivenName(EntityManager entityManager, String townForDelete) {
String hqlDeleteTownQuery = "SELECT t FROM Town AS t WHERE t.name = '" + townForDelete + "'";
Query queryTown = entityManager.createQuery(hqlDeleteTownQuery);
Town town = (Town) queryTown.getSingleResult();
entityManager.remove(town);
}
private static List<Address> deleteAddressesFromTheGivenTown(EntityManager entityManager, String townForDelete) {
String hqlQuery = "SELECT a FROM Address AS a WHERE a.town.name = '" + townForDelete + "'";
Query queryAddressesByTown = entityManager.createQuery(hqlQuery);
List<Address> addresses = queryAddressesByTown.getResultList();
for (Address address : addresses) {
entityManager.remove(address);
}
return addresses;
}
private static void setEmployeeAdressToNull(EntityManager entityManager, String townForDelete) {
String query = "SELECT e FROM Employee AS e WHERE e.address.town.name = '" + townForDelete + "'";
Query queryGetEmplFromAddress = entityManager.createQuery(query);
List<Employee> employees = queryGetEmplFromAddress.getResultList();
for (Employee employee : employees) {
employee.setAddress(null);
}
}
}