two parametrized queries created

This commit is contained in:
Marcin Hutek 2023-12-29 15:55:00 +01:00
parent 35b6a89702
commit ddc6dba212
5 changed files with 234 additions and 156 deletions

View File

@ -2,6 +2,7 @@ package org.example;
import jakarta.persistence.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Entity
@ -10,10 +11,10 @@ public class FilmCategoryModel {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="film_id")
@Column(name="category_id")
private int id;
@Column(name="title")
@Column(name="name")
private String filmCategory;
public FilmCategoryModel() {}
@ -34,8 +35,12 @@ public class FilmCategoryModel {
this.filmCategory = filmCategory;
}
// @OneToMany(mappedBy = "filmCategoryModel", fetch = FetchType.LAZY)
// private Set<FilmInfoModel> filmInfoSet = new HashSet<>();
@OneToMany(mappedBy = "filmCategoryModel", fetch = FetchType.LAZY)
private List<FilmInfoModel> filmInfoModel;
public FilmInfoModel getFilmInfo() {
return (FilmInfoModel) filmInfoModel;
}
@Override
public String toString() {

View File

@ -94,9 +94,13 @@ public class FilmInfoModel {
return filmLanguageModel;
}
// @ManyToOne
// @JoinColumn(name = "category_id")
// private FilmCategoryModel filmCategoryModel;
@ManyToOne
@JoinColumn(name = "category_id", referencedColumnName = "category_id", insertable=false, updatable = false)
private FilmCategoryModel filmCategoryModel;
public FilmCategoryModel getFilmCategoryModel() {
return filmCategoryModel;
}
@OneToOne
@JoinColumn(name = "film_id", referencedColumnName = "film_id")

View File

@ -1,27 +1,27 @@
package org.example;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder().build();
sessionFactory = metadata.getSessionFactoryBuilder().build();
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
//package org.example;
//
//import org.hibernate.SessionFactory;
//import org.hibernate.boot.Metadata;
//import org.hibernate.boot.MetadataSources;
//import org.hibernate.boot.registry.StandardServiceRegistry;
//import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
//
//public class HibernateUtil {
//
// private static final SessionFactory sessionFactory;
//
// static {
// try {
// // Create the SessionFactory from hibernate.cfg.xml
// StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
// Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder().build();
// sessionFactory = metadata.getSessionFactoryBuilder().build();
// } catch (Exception e) {
// throw new ExceptionInInitializerError(e);
// }
// }
//
// public static SessionFactory getSessionFactory() {
// return sessionFactory;
// }
//}

View File

@ -12,14 +12,20 @@ public class Main {
public static void main(String[] args) {
System.out.println("Start");
Queries queries = new Queries();
// queries.getAllFilms();
// queries.printFilmDetails();
queries.getFilmLanguage();
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configuration.buildSessionFactory();
try (Session session = sessionFactory.openSession()) {
Queries queries = new Queries(session);
// queries.getFilmByLangYear("Japanese", "2006");
queries.getFilmByLangCategory("Action", "Japanese");
} catch (Exception e) {
e.printStackTrace();
} finally {
sessionFactory.close();
}
}
}

View File

@ -1,149 +1,212 @@
package org.example;
import org.hibernate.Cache;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class Queries {
public static void getAllFilms() {
Configuration configuration = new Configuration();
configuration.configure();
Session session;
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 Queries(Session session) {
this.session = session;
}
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++;
}
// 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();
}
}
// }
// } 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 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();
public void getFilmByLangYear(String language, String releaseYear) {
// 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);
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();
int limit = 10;
int counter = 0;
for (Object[] result : results) {
if (counter >= limit) {
break;
}
for (Object[] result : results) {
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("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("-----------------------------------");
}
}
}
}