-
Notifications
You must be signed in to change notification settings - Fork 19
Feature/film #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/film #10
Changes from all commits
cd58553
f043eb4
86c2ac2
5c33218
c5b0d5f
3f3262f
5125f3a
bc6883c
efe47e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import jakarta.persistence.*; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| @Entity | ||
| public class Director { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @OneToMany(mappedBy = "director") | ||
| private Set<Film> films = new HashSet<>(); | ||
|
|
||
| private String name; | ||
| private String country; | ||
| private int birthYear; | ||
| @Column(nullable = true) | ||
| private Integer yearOfDeath; | ||
|
|
||
| public Set<Film> getFilms() { | ||
| return films; | ||
| } | ||
|
|
||
| public void setFilms(Set<Film> films) { | ||
| this.films = films; | ||
| } | ||
|
|
||
| public Integer getYearOfDeath() { | ||
| return yearOfDeath; | ||
| } | ||
|
|
||
| public void setYearOfDeath(Integer yearOfDeath) { | ||
| this.yearOfDeath = yearOfDeath; | ||
| } | ||
|
|
||
| public int getBirthYear() { | ||
| return birthYear; | ||
| } | ||
|
|
||
| public void setBirthYear(int birthYear) { | ||
| this.birthYear = birthYear; | ||
| } | ||
|
|
||
| public String getCountry() { | ||
| return country; | ||
| } | ||
|
|
||
| public void setCountry(String country) { | ||
| this.country = country; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public void setId(Long id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,61 @@ | ||||||||||||||||||||||||||||||||||
| import jakarta.persistence.*; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @Entity | ||||||||||||||||||||||||||||||||||
| public class Film { | ||||||||||||||||||||||||||||||||||
| @Id | ||||||||||||||||||||||||||||||||||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||||||||||||||||||||||||||||||||||
| private Long id; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| private String title; | ||||||||||||||||||||||||||||||||||
| @ManyToOne | ||||||||||||||||||||||||||||||||||
| @JoinColumn(name = "director_id") | ||||||||||||||||||||||||||||||||||
| private Director director; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| public Director getDirector() { | ||||||||||||||||||||||||||||||||||
| return director; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| public void setDirector(Director director) { | ||||||||||||||||||||||||||||||||||
| this.director = director; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| public String getTitle() { | ||||||||||||||||||||||||||||||||||
| return title; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| public void setTitle(String title) { | ||||||||||||||||||||||||||||||||||
| this.title = title; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| public void setId(Long id) { | ||||||||||||||||||||||||||||||||||
| this.id = id; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| public Long getId() { | ||||||||||||||||||||||||||||||||||
| return id; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| //Lägg till en ny film | ||||||||||||||||||||||||||||||||||
| public static void addFilm(EntityManager em, String title, Director director) { | ||||||||||||||||||||||||||||||||||
| Film film = new Film(); | ||||||||||||||||||||||||||||||||||
| film.setTitle(title); | ||||||||||||||||||||||||||||||||||
| film.setDirector(director); | ||||||||||||||||||||||||||||||||||
| em.persist(film); | ||||||||||||||||||||||||||||||||||
| System.out.println("Film tillagd: " + title); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| //Hämta alla filmer | ||||||||||||||||||||||||||||||||||
| public static List<Film> getAllFilms(EntityManager em) { | ||||||||||||||||||||||||||||||||||
| return em.createQuery("FROM Film", Film.class).getResultList(); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+49
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add null validation for EntityManager. The method should validate that the EntityManager parameter is not null to prevent NullPointerException. ✅ Proposed fix public static List<Film> getAllFilms(EntityManager em) {
+ if (em == null) {
+ throw new IllegalArgumentException("EntityManager cannot be null");
+ }
return em.createQuery("FROM Film", Film.class).getResultList();
}🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| //Hämta filmer regisserade av en viss regissör | ||||||||||||||||||||||||||||||||||
| public static List<Film> getFilmsByDirector(EntityManager em, Director director) { | ||||||||||||||||||||||||||||||||||
| return em.createQuery("FROM Film f WHERE f.director = :director", Film.class) | ||||||||||||||||||||||||||||||||||
| .setParameter("director", director) | ||||||||||||||||||||||||||||||||||
| .getResultList(); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+54
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add null validation for parameters. Both EntityManager and Director parameters should be validated to prevent NullPointerException. ✅ Proposed fix public static List<Film> getFilmsByDirector(EntityManager em, Director director) {
+ if (em == null) {
+ throw new IllegalArgumentException("EntityManager cannot be null");
+ }
+ if (director == null) {
+ throw new IllegalArgumentException("Director cannot be null");
+ }
return em.createQuery("FROM Film f WHERE f.director = :director", Film.class)
.setParameter("director", director)
.getResultList();
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,63 @@ | ||||||||||||||
| import jakarta.persistence.Entity; | ||||||||||||||
| import jakarta.persistence.EntityManagerFactory; | ||||||||||||||
| import jakarta.persistence.Persistence; | ||||||||||||||
| import jakarta.persistence.PersistenceConfiguration; | ||||||||||||||
| import org.hibernate.jpa.HibernatePersistenceConfiguration; | ||||||||||||||
|
|
||||||||||||||
| import java.util.List; | ||||||||||||||
|
|
||||||||||||||
| public class Main { | ||||||||||||||
| public static void main(String[] args) { | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| final PersistenceConfiguration cfg = new HibernatePersistenceConfiguration("emf") | ||||||||||||||
| .jdbcUrl("jdbc:mysql://localhost:3306/film_database") | ||||||||||||||
| .jdbcUsername("root") | ||||||||||||||
| .jdbcPassword("root") | ||||||||||||||
|
Comment on lines
+14
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove hardcoded database credentials. Hardcoded credentials pose a security risk and make the application less flexible. Consider using environment variables, a configuration file, or a secrets management system. 🔒 Proposed fix using environment variables- .jdbcUrl("jdbc:mysql://localhost:3306/film_database")
- .jdbcUsername("root")
- .jdbcPassword("root")
+ .jdbcUrl(System.getenv().getOrDefault("DB_URL", "jdbc:mysql://localhost:3306/film_database"))
+ .jdbcUsername(System.getenv().getOrDefault("DB_USER", "root"))
+ .jdbcPassword(System.getenv().getOrDefault("DB_PASSWORD", ""))📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||
| .property("hibernate.hbm2ddl.auto", "update") | ||||||||||||||
| .property("hibernate.show_sql", "true") | ||||||||||||||
| .property("hibernate.format_sql", "true") | ||||||||||||||
| .property("hibernate.highlight_sql", "true") | ||||||||||||||
| .managedClasses(Film.class, Director.class); | ||||||||||||||
| try (EntityManagerFactory emf = cfg.createEntityManagerFactory()) { | ||||||||||||||
| emf.runInTransaction(em -> { | ||||||||||||||
| //Skapa regissör för test | ||||||||||||||
| Director director = new Director(); | ||||||||||||||
| director.setName("Christopher Bolan"); | ||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix typo in director name. "Christopher Bolan" should likely be "Christopher Nolan". ✏️ Proposed fix- director.setName("Christopher Bolan");
+ director.setName("Christopher Nolan");📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||
| director.setCountry("United States"); | ||||||||||||||
| director.setBirthYear(1970); | ||||||||||||||
| em.persist(director); | ||||||||||||||
|
|
||||||||||||||
| //Testa att lägga till filmer | ||||||||||||||
| Film.addFilm(em, "The Dirk Knight", director); | ||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix typo in film title. "The Dirk Knight" should likely be "The Dark Knight". ✏️ Proposed fix- Film.addFilm(em, "The Dirk Knight", director);
+ Film.addFilm(em, "The Dark Knight", director);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||
| Film.addFilm(em, "Inception", director); | ||||||||||||||
|
|
||||||||||||||
| //Testa att hämta alla filmer | ||||||||||||||
| System.out.println("\nAlla filmer:"); | ||||||||||||||
| List<Film> films = Film.getAllFilms(em); | ||||||||||||||
| films.forEach(film -> System.out.println(film.getTitle())); | ||||||||||||||
|
|
||||||||||||||
| //Testa att hämta filmer av en viss regissör | ||||||||||||||
| System.out.println("\nAlla filmer av regissör:"); | ||||||||||||||
| List<Film> filmsByDirector = Film.getFilmsByDirector(em, director); | ||||||||||||||
| filmsByDirector.forEach(film -> System.out.println(film.getTitle())); | ||||||||||||||
|
|
||||||||||||||
| //If no Films in database, add some | ||||||||||||||
| // if (em.createQuery("select count(o) from Film o", Long.class) | ||||||||||||||
| // .getSingleResult() == 0) { | ||||||||||||||
| // Film film1 = new Film(); | ||||||||||||||
| // em.persist(film1); | ||||||||||||||
| // em.flush(); | ||||||||||||||
| // Film film2 = new Film(); | ||||||||||||||
| // em.persist(film2); | ||||||||||||||
| // } | ||||||||||||||
| // System.out.println("==== Using select query, N + 1 ===="); | ||||||||||||||
| // em.createQuery("from Film", Film.class) | ||||||||||||||
| // .getResultList().forEach(System.out::println); | ||||||||||||||
|
|
||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add null validation and consider extracting to a repository class.
The method lacks null checks for parameters, which could lead to NullPointerException or constraint violations. Additionally, mixing data access logic with entity classes violates separation of concerns—consider moving CRUD operations to a dedicated repository or DAO class.
✅ Proposed fix with validation
public static void addFilm(EntityManager em, String title, Director director) { + if (em == null) { + throw new IllegalArgumentException("EntityManager cannot be null"); + } + if (title == null || title.trim().isEmpty()) { + throw new IllegalArgumentException("Title cannot be null or empty"); + } + if (director == null) { + throw new IllegalArgumentException("Director cannot be null"); + } Film film = new Film(); film.setTitle(title); film.setDirector(director); em.persist(film); System.out.println("Film tillagd: " + title); }📝 Committable suggestion
🤖 Prompt for AI Agents