all required queries prepared and tested

This commit is contained in:
Marcin Hutek 2023-12-29 16:19:25 +01:00
parent ddc6dba212
commit d8b7ed30f2
3 changed files with 76 additions and 193 deletions

View File

@ -41,10 +41,11 @@ public class FilmModel {
public void setFilmInfo(FilmInfoModel filmInfo) {
this.filmInfo = filmInfo;
}
// @Override
// public String toString() {
// return "Film{" +
// "id=" + id +
// ", filmTitle='" + filmTitle + '}';
// }
@Override
public String toString() {
return "Film{" +
"id=" + id +
", filmTitle='" + filmTitle + '}';
}
}

View File

@ -21,7 +21,9 @@ public class Main {
Queries queries = new Queries(session);
// queries.getFilmByLangYear("Japanese", "2006");
queries.getFilmByLangCategory("Action", "Japanese");
// queries.getFilmByLangCategory("Action", "Japanese");
queries.getAllFilmsByPage(4, session);
} catch (Exception e) {
e.printStackTrace();
} finally {

View File

@ -13,200 +13,80 @@ public class Queries {
this.session = session;
}
// public static void getAllFilms() {
// Configuration configuration = new Configuration();
// configuration.configure();
//
// try (SessionFactory sessionFactory = configuration.buildSessionFactory()) {
// try (Session session = sessionFactory.openSession()) {
// session.beginTransaction();
// List<FilmModel> films = session.createQuery("FROM FilmModel", FilmModel.class)
// .getResultList();
//
// films.forEach(System.out::println);
// session.getTransaction().commit();
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
//
// public static void getFilms() {
// Configuration configuration = new Configuration();
// configuration.configure();
//
// try (SessionFactory sessionFactory = configuration.buildSessionFactory()) {
// try (Session session = sessionFactory.openSession()) {
// session.beginTransaction();
// List<FilmModel> films = session
// .createQuery("SELECT f FROM FilmModel f JOIN FETCH f.filmInfo",
// FilmModel.class)
// .getResultList();
//
// int limit = 10;
// int counter = 0;
//
// for (FilmModel film : films) {
// if (counter >= limit) {
// break;
// }
// FilmInfoModel filmInfoModel = film.getFilmInfo();
// String result = "Film Id: " + film.getId() + '\n' +
// "Film Title: " + film.getFilmTitle() + '\n' +
// "Film Description: " + filmInfoModel.getFilmDescription() +
// "\n-----------------------------------";
// System.out.println(result);
// counter++;
// }
////
//// films.forEach(System.out::println);
//// session.getTransaction().commit();
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
//
// public void printFilmDetails() {
// try (Session session = HibernateUtil.getSessionFactory().openSession()) {
// String hql = "SELECT f.id, f.filmTitle, fi.filmDescription FROM FilmModel f JOIN f.filmInfo fi";
//// String hql = "SELECT a, b FROM FilmModel a LEFT JOIN FETCH a.filmInfo b";
// Query<Object[]> query = session.createQuery(hql, Object[].class);
// List<Object[]> results = query.getResultList();
//
//
//// String sqlQuery = query.unwrap(org.hibernate.query.Query.class).getQueryString();
//// System.out.println("Generated SQL Query: " + sqlQuery);
//// int length = results.size();
//// System.out.println(length);
//
//
// int limit = 10;
// int counter = 0;
//
// for (Object[] result : results) {
//
// if (counter >= limit) {
// break;
// }
//
// int filmId = (int) result[0];
// String filmTitle = (String) result[1];
// String filmDescription = (String) result[2];
//
// System.out.println("Film ID: " + filmId);
// System.out.println("Film Title: " + filmTitle);
// System.out.println("Description: " + filmDescription);
// System.out.println("-----------------------------------");
//// FilmInfoModel filmInfo = filmMo.getFilmInfo();
//// String description = filmInfo.getDescription();
//// System.out.println("Film Description: " + description);
//
//
////
//// FilmModel film = (FilmModel) result[0];
////// FilmInfoModel filmInfo = (FilmInfoModel) result[1];
////
//// FilmInfoModel filmInfoModel = film.getFilmInfo();
//// String resultString = "Film Id: " + film.getId() + '\n' +
//// "Film Title: " + film.getFilmTitle() + '\n' +
//// "Film Description: " + filmInfoModel.getFilmDescription() +
//// "\n-----------------------------------";
//// String test = film.getFilmTitle();
//// System.out.println(test);
// counter++;
////
// }
// }
// }
//
// public void getFilmLanguage() {
// try (Session session = HibernateUtil.getSessionFactory().openSession()) {
// String hql = "SELECT f.id, f.filmDescription, fl.filmLanguage FROM FilmInfoModel f JOIN f.filmLanguageModel fl";
//// String hql = "SELECT a, b FROM FilmModel a LEFT JOIN FETCH a.filmInfo b";
// Query<Object[]> query = session.createQuery(hql, Object[].class);
// List<Object[]> results = query.getResultList();
//
// int limit = 10;
// int counter = 0;
//
// for (Object[] result : results) {
//
// if (counter >= limit) {
// break;
// }
//
// int filmId = (int) result[0];
// String filmDescription = (String) result[1];
// String filmLanguage = (String) result[2];
//
// System.out.println("Film ID: " + filmId);
// System.out.println("Film Description: " + filmDescription);
// System.out.println("Language: " + filmLanguage);
// System.out.println("-----------------------------------");
//
//
// }
// }
// }
public void getFilmByLangYear(String language, String releaseYear) {
public void getFilmByLangYear(String language, String releaseYear) {
String hql = "SELECT f1.id, f2.filmTitle, f3.filmLanguage " +
"FROM FilmInfoModel f1 " +
"JOIN FilmModel f2 ON f1.id = f2.id " +
"JOIN FilmLanguageModel f3 ON f1.filmLanguage = f3.id " +
"WHERE f3.filmLanguage = :language AND f1.releaseYear = :releaseYear";
String hql = "SELECT f1.id, f2.filmTitle, f3.filmLanguage " +
"FROM FilmInfoModel f1 " +
"JOIN FilmModel f2 ON f1.id = f2.id " +
"JOIN FilmLanguageModel f3 ON f1.filmLanguage = f3.id " +
"WHERE f3.filmLanguage = :language AND f1.releaseYear = :releaseYear";
Query<Object[]> query = session.createQuery(hql, Object[].class)
.setParameter("language", language)
.setParameter("releaseYear", releaseYear);
List<Object[]> results = query.getResultList();
Query<Object[]> query = session.createQuery(hql, Object[].class)
.setParameter("language", language)
.setParameter("releaseYear", releaseYear);
List<Object[]> results = query.getResultList();
for (Object[] result : results) {
for (Object[] result : results) {
int filmId = (int) result[0];
String filmTitle = (String) result[1];
String filmLanguage = (String) result[2];
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("-----------------------------------");
}
}
public void getFilmByLangCategory(String categoryName, String languageName) {
String hql = "SELECT f1.id, f2.filmTitle, f3.filmLanguage, f4.filmCategory " +
"FROM FilmInfoModel f1 " +
"JOIN FilmModel f2 ON f1.id = f2.id " +
"JOIN FilmLanguageModel f3 ON f1.filmLanguage = f3.id " +
"JOIN FilmCategoryModel f4 ON f1.filmCategory = f4.id " +
"WHERE f3.filmLanguage = :language AND f4.filmCategory = :categoryName";
Query<Object[]> query = session.createQuery(hql, Object[].class)
.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("-----------------------------------");
System.out.println("Film ID: " + filmId);
System.out.println("Film Title: " + filmTitle);
System.out.println("Language: " + filmLanguage);
System.out.println("-----------------------------------");
}
}
public void getFilmByLangCategory(String categoryName, String languageName) {
String hql = "SELECT f1.id, f2.filmTitle, f3.filmLanguage, f4.filmCategory " +
"FROM FilmInfoModel f1 " +
"JOIN FilmModel f2 ON f1.id = f2.id " +
"JOIN FilmLanguageModel f3 ON f1.filmLanguage = f3.id " +
"JOIN FilmCategoryModel f4 ON f1.filmCategory = f4.id " +
"WHERE f3.filmLanguage = :language AND f4.filmCategory = :categoryName";
}
Query<Object[]> query = session.createQuery(hql, Object[].class)
.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("-----------------------------------");
}
}
public static List<FilmModel> getAllFilmsByPage(int pageNumber, Session session) {
//calculate total number
Query queryTotal = session.createQuery("Select count(f) from FilmModel f");
long countResult = (long)queryTotal.getSingleResult();
// create query
Query<FilmModel> query = session.createQuery("Select f from FilmModel f");
//page size and number of pages
int pageSize = 10;
int pageTotal = (int) ((countResult / pageSize) + 1);
if (pageNumber > pageTotal) pageNumber = pageTotal;
query.setFirstResult((pageNumber - 1) * pageSize);
query.setMaxResults(pageSize);
return query.getResultList();
}
}