fixed entity relationships and prepared 2 basic queries
This commit is contained in:
parent
771698b40b
commit
35b6a89702
@ -86,9 +86,13 @@ public class FilmInfoModel {
|
||||
this.filmCategory = filmCategory;
|
||||
}
|
||||
|
||||
// @ManyToOne
|
||||
// @JoinColumn(name = "language_id")
|
||||
// private FilmLanguageModel filmLanguageModel;
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "language_id", referencedColumnName = "language_id", insertable=false, updatable = false)
|
||||
private FilmLanguageModel filmLanguageModel;
|
||||
|
||||
public FilmLanguageModel getFilmLanguageModel() {
|
||||
return filmLanguageModel;
|
||||
}
|
||||
|
||||
// @ManyToOne
|
||||
// @JoinColumn(name = "category_id")
|
||||
@ -102,9 +106,9 @@ public class FilmInfoModel {
|
||||
return filmModel;
|
||||
}
|
||||
|
||||
public void setFilmModel(FilmModel filmModel) {
|
||||
this.filmModel = filmModel;
|
||||
}
|
||||
// public void setFilmModel(FilmModel filmModel) {
|
||||
// this.filmModel = filmModel;
|
||||
// }
|
||||
|
||||
// @Override
|
||||
// public String toString() {
|
||||
|
@ -2,6 +2,7 @@ package org.example;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
@ -34,8 +35,12 @@ public class FilmLanguageModel {
|
||||
this.filmLanguage = filmLanguage;
|
||||
}
|
||||
|
||||
// @OneToMany(mappedBy = "filmLanguageModel", fetch = FetchType.LAZY)
|
||||
// private Set<FilmInfoModel> filmInfoSet = new HashSet<>();
|
||||
@OneToMany(mappedBy = "filmLanguageModel", fetch = FetchType.LAZY)
|
||||
private List<FilmInfoModel> filmInfoModel;
|
||||
|
||||
public FilmInfoModel getFilmInfo() {
|
||||
return (FilmInfoModel) filmInfoModel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
@ -14,7 +14,8 @@ public class Main {
|
||||
|
||||
Queries queries = new Queries();
|
||||
// queries.getAllFilms();
|
||||
queries.printFilmDetails();
|
||||
// queries.printFilmDetails();
|
||||
queries.getFilmLanguage();
|
||||
|
||||
|
||||
|
||||
|
@ -14,7 +14,7 @@ public class Queries {
|
||||
Configuration configuration = new Configuration();
|
||||
configuration.configure();
|
||||
|
||||
try (SessionFactory sessionFactory = configuration.buildSessionFactory()){
|
||||
try (SessionFactory sessionFactory = configuration.buildSessionFactory()) {
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
List<FilmModel> films = session.createQuery("FROM FilmModel", FilmModel.class)
|
||||
@ -32,7 +32,7 @@ public class Queries {
|
||||
Configuration configuration = new Configuration();
|
||||
configuration.configure();
|
||||
|
||||
try (SessionFactory sessionFactory = configuration.buildSessionFactory()){
|
||||
try (SessionFactory sessionFactory = configuration.buildSessionFactory()) {
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
session.beginTransaction();
|
||||
List<FilmModel> films = session
|
||||
@ -63,9 +63,10 @@ public class Queries {
|
||||
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 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();
|
||||
@ -88,11 +89,11 @@ public class Queries {
|
||||
|
||||
int filmId = (int) result[0];
|
||||
String filmTitle = (String) result[1];
|
||||
int releaseYear = (int) result[2];
|
||||
String filmDescription = (String) result[2];
|
||||
|
||||
System.out.println("Film ID: " + filmId);
|
||||
System.out.println("Film Title: " + filmTitle);
|
||||
System.out.println("Release Year: " + releaseYear);
|
||||
System.out.println("Description: " + filmDescription);
|
||||
System.out.println("-----------------------------------");
|
||||
// FilmInfoModel filmInfo = filmMo.getFilmInfo();
|
||||
// String description = filmInfo.getDescription();
|
||||
@ -115,4 +116,34 @@ public class Queries {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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("-----------------------------------");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user