created a method which can add data to the database

This commit is contained in:
Marcin Hutek 2023-12-29 18:32:11 +01:00
parent d8b7ed30f2
commit 05b449a115
5 changed files with 68 additions and 32 deletions

View File

@ -1,5 +1,7 @@
package org.example;
import jakarta.persistence.*;
import org.hibernate.Session;
import org.hibernate.Transaction;
@Entity
@Table(name="films_info")
@ -110,9 +112,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() {

View File

@ -10,7 +10,7 @@ import java.util.Set;
public class FilmLanguageModel {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@GeneratedValue
@Column(name="language_id")
private int id;

View File

@ -1,27 +0,0 @@
//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

@ -2,6 +2,7 @@ package org.example;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.cfg.Configuration;
@ -22,12 +23,27 @@ public class Main {
Queries queries = new Queries(session);
// queries.getFilmByLangYear("Japanese", "2006");
// queries.getFilmByLangCategory("Action", "Japanese");
queries.getAllFilmsByPage(4, session);
// queries.getAllFilmsByPage(4, session);
// queries.addFilm("TestMovieTitle", session);
Transaction tx = null;
try {
tx = session.beginTransaction();
FilmModel filmModel = queries.createFilmModel();
session.save(filmModel);
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
sessionFactory.close();
}
}
}

View File

@ -1,6 +1,7 @@
package org.example;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import java.util.List;
@ -89,4 +90,48 @@ public class Queries {
return query.getResultList();
}
public void addFilm(String filmTitle, Session session) {
Transaction transaction = null;
try {
transaction = session.beginTransaction();
FilmModel filmModel = new FilmModel();
filmModel.setFilmTitle(filmTitle);
FilmInfoModel filmInfo = new FilmInfoModel();
filmInfo.setFilmDescription("testowy opis filmu");
filmModel.setFilmInfo(filmInfo);
filmInfo.setFilmModel(filmModel);
session.save(filmModel);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
}
}
public static FilmModel createFilmModel() {
FilmModel filmModel = new FilmModel();
FilmInfoModel filmInfoModel = new FilmInfoModel();
// filmModel.setId(1001);
filmModel.setFilmTitle("Test Film Title");
filmInfoModel.setFilmDescription("Test Movie Description");
filmInfoModel.setReleaseYear(2023);
filmInfoModel.setFilmLanguage(1);
filmInfoModel.setFilmLength(100);
filmInfoModel.setFilmRating("test");
filmInfoModel.setFilmCategory(1);
filmModel.setFilmInfo(filmInfoModel);
return filmModel;
}
}