adding comments and organizing the code

This commit is contained in:
Marcin Hutek 2023-12-31 18:34:49 +01:00
parent 237977fb5c
commit 6c65e5b48b
2 changed files with 43 additions and 31 deletions

View File

@ -21,16 +21,20 @@ public class Main {
try (Session session = sessionFactory.openSession()) {
Queries queries = new Queries(session);
// WYWOLANIE ZAPYTAN Z PARAMETRAMI
// queries.getFilmByLangYear("Japanese", "2006");
// queries.getFilmByLangCategory("Action", "Japanese");
// WYWOLANIE ZAPYTANIA STRONICOWANEGO
// queries.getAllFilmsByPage(4, session);
Transaction tx = null;
try {
tx = session.beginTransaction();
FilmModel filmModel = queries.createFilmModel();
session.save(filmModel);
//DODANIE DO BAZY NOWEGO REKORDU
// FilmModel filmModel = queries.createFilmModel();
// session.save(filmModel);
tx.commit();
} catch (Exception e) {

View File

@ -16,7 +16,8 @@ public class Queries {
this.session = session;
}
public void getFilmByLangYear(String language, String releaseYear) {
// ZAPYTANIE Z PARAMETREM
public List<Object[]> getFilmByLangYear(String language, String releaseYear) {
String hql = "SELECT f1.id, f2.filmTitle, f3.filmLanguage " +
@ -30,20 +31,23 @@ public class Queries {
.setParameter("releaseYear", releaseYear);
List<Object[]> results = query.getResultList();
for (Object[] result : results) {
int filmId = (int) result[0];
String filmTitle = (String) result[1];
String filmLanguage = (String) result[2];
System.out.println("Film ID: " + filmId);
System.out.println("Film Title: " + filmTitle);
System.out.println("Language: " + filmLanguage);
System.out.println("-----------------------------------");
}
// wypisanie wynikow bezposrednio w konsoli
// for (Object[] result : results) {
//
// int filmId = (int) result[0];
// String filmTitle = (String) result[1];
// String filmLanguage = (String) result[2];
//
// System.out.println("Film ID: " + filmId);
// System.out.println("Film Title: " + filmTitle);
// System.out.println("Language: " + filmLanguage);
// System.out.println("-----------------------------------");
// }
return results;
}
public void getFilmByLangCategory(String categoryName, String languageName) {
// ZAPYTANIE Z PARAMETREM
public List<Object[]> getFilmByLangCategory(String categoryName, String languageName) {
String hql = "SELECT f1.id, f2.filmTitle, f3.filmLanguage, f4.filmCategory " +
"FROM FilmInfoModel f1 " +
@ -56,22 +60,24 @@ public class Queries {
.setParameter("categoryName", categoryName)
.setParameter("language", languageName);
List<Object[]> results = query.getResultList();
for (Object[] result : results) {
int filmId = (int) result[0];
String filmTitle = (String) result[1];
String filmCategory = (String) result[2];
String filmLanguage = (String) result[3];
System.out.println("Film ID: " + filmId);
System.out.println("Film Title: " + filmTitle);
System.out.println("Category: " + filmCategory);
System.out.println("Film Language: " + filmLanguage);
System.out.println("-----------------------------------");
}
// wypisanie wynikow bezposrednio w konsoli
// for (Object[] result : results) {
//
// int filmId = (int) result[0];
// String filmTitle = (String) result[1];
// String filmCategory = (String) result[2];
// String filmLanguage = (String) result[3];
//
// System.out.println("Film ID: " + filmId);
// System.out.println("Film Title: " + filmTitle);
// System.out.println("Category: " + filmCategory);
// System.out.println("Film Language: " + filmLanguage);
// System.out.println("-----------------------------------");
// }
return results;
}
// ZAPYTANIE STRONICOWANE
public static List<FilmModel> getAllFilmsByPage(int pageNumber, Session session) {
//calculate total number
Query queryTotal = session.createQuery("Select count(f) from FilmModel f");
@ -92,6 +98,7 @@ public class Queries {
return query.getResultList();
}
// DODANIE DO BAZY NOWEGO REKORDU
public static FilmModel createFilmModel() {
FilmModel filmModel = new FilmModel();
FilmInfoModel filmInfoModel = new FilmInfoModel();
@ -104,6 +111,7 @@ public class Queries {
filmInfoModel.setFilmRating("test");
filmInfoModel.setFilmCategory(1);
// Pole ZonedDateTime
ZonedDateTime currentTimestamp = ZonedDateTime.now();
filmInfoModel.setLastUpdate(currentTimestamp);