first query which connects two entities created

This commit is contained in:
Marcin Hutek 2023-12-29 00:51:51 +01:00
parent b6063cd0b9
commit 771698b40b
7 changed files with 193 additions and 60 deletions

View File

@ -34,8 +34,8 @@ public class FilmCategoryModel {
this.filmCategory = filmCategory;
}
@OneToMany(mappedBy = "category_id", fetch = FetchType.LAZY)
private Set<FilmInfoModel> filmInfoSet = new HashSet<>();
// @OneToMany(mappedBy = "filmCategoryModel", fetch = FetchType.LAZY)
// private Set<FilmInfoModel> filmInfoSet = new HashSet<>();
@Override
public String toString() {

View File

@ -86,25 +86,33 @@ public class FilmInfoModel {
this.filmCategory = filmCategory;
}
@ManyToOne
@JoinColumn(name = "language_id")
private FilmLanguageModel filmLanguageModel;
// @ManyToOne
// @JoinColumn(name = "language_id")
// private FilmLanguageModel filmLanguageModel;
@ManyToOne
@JoinColumn(name = "category_id")
private FilmCategoryModel filmCategoryModel;
// @ManyToOne
// @JoinColumn(name = "category_id")
// private FilmCategoryModel filmCategoryModel;
@OneToOne
@JoinColumn(name = "film_id")
private FilmModel film;
@JoinColumn(name = "film_id", referencedColumnName = "film_id")
private FilmModel filmModel;
@Override
public String toString() {
return "FilmInfo{" +
"id=" + id +
", filmDescription=" + filmDescription + '\n' +
", releaseYear=" + releaseYear + ", filmLanguage=" + filmLanguage + '\n' +
", filmLength=" + filmLength + ", filmRating=" + filmRating + '\n' +
", filmCategory=" + filmCategory + '}';
public FilmModel getFilmModel() {
return filmModel;
}
public void setFilmModel(FilmModel filmModel) {
this.filmModel = filmModel;
}
// @Override
// public String toString() {
// return "FilmInfo{" +
// "id=" + id +
// ", filmDescription=" + filmDescription + '\n' +
// ", releaseYear=" + releaseYear + ", filmLanguage=" + filmLanguage + '\n' +
// ", filmLength=" + filmLength + ", filmRating=" + filmRating + '\n' +
// ", filmCategory=" + filmCategory + '}';
// }
}

View File

@ -34,8 +34,8 @@ public class FilmLanguageModel {
this.filmLanguage = filmLanguage;
}
@OneToMany(mappedBy = "language_id", fetch = FetchType.LAZY)
private Set<FilmInfoModel> filmInfoSet = new HashSet<>();
// @OneToMany(mappedBy = "filmLanguageModel", fetch = FetchType.LAZY)
// private Set<FilmInfoModel> filmInfoSet = new HashSet<>();
@Override
public String toString() {

View File

@ -31,13 +31,20 @@ public class FilmModel {
this.filmTitle = filmTitle;
}
@OneToOne(mappedBy = "film", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@OneToOne(mappedBy = "filmModel", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private FilmInfoModel filmInfo;
@Override
public String toString() {
return "Film{" +
"id=" + id +
", filmTitle='" + filmTitle + '}';
public FilmInfoModel getFilmInfo() {
return filmInfo;
}
public void setFilmInfo(FilmInfoModel filmInfo) {
this.filmInfo = filmInfo;
}
// @Override
// public String toString() {
// return "Film{" +
// "id=" + id +
// ", filmTitle='" + filmTitle + '}';
// }
}

View File

@ -0,0 +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;
}
}

View File

@ -1,13 +1,8 @@
//package org.example;
//
//public class Main {
// public static void main(String[] args) {
// System.out.println("Hello world!");
// }
//}
package org.example;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.cfg.Configuration;
import java.util.List;
@ -17,35 +12,13 @@ public class Main {
public static void main(String[] args) {
System.out.println("Start");
// Tworzenie obiektu konfiguracji Hibernate
Configuration configuration = new Configuration();
configuration.configure(); // Domyślnie wczytuje plik hibernate.cfg.xml
Queries queries = new Queries();
// queries.getAllFilms();
queries.printFilmDetails();
// Tworzenie obiektu sesji
try (SessionFactory sessionFactory = configuration.buildSessionFactory()) {
// Otwieranie sesji
try (Session session = sessionFactory.openSession()) {
// Rozpoczęcie transakcji
session.beginTransaction();
// Wykonanie zapytania SQL
List<Object[]> result = session.createQuery("SELECT * FROM film").list();
// Wypisywanie wyników na konsoli
for (Object[] row : result) {
for (Object column : row) {
System.out.print(column + " ");
}
System.out.println();
}
// Zakończenie transakcji
session.getTransaction().commit();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Done");
}
}

View File

@ -0,0 +1,118 @@
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();
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.releaseYear 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];
int releaseYear = (int) result[2];
System.out.println("Film ID: " + filmId);
System.out.println("Film Title: " + filmTitle);
System.out.println("Release Year: " + releaseYear);
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++;
//
}
}
}
}