Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/_uV8Mn8f)
# 📘 Projektarbete: JPA + Hibernate med GitHub-flöde

Projektet genomförs som antingen en Java CLI-applikation eller med hjälp av JavaFX om ni vill ha ett grafiskt gränssnitt.
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/Director.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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;
}


}


39 changes: 39 additions & 0 deletions src/main/java/Film.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import jakarta.persistence.*;

@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;
}


}
42 changes: 42 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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")
.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);
Comment on lines +13 to +21
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Externalize database configuration to avoid hardcoding credentials.

Database credentials and connection details are hardcoded. This is a security risk and reduces portability. Consider using environment variables, a properties file, or a configuration class to externalize these settings.

Example using environment variables:

         final PersistenceConfiguration cfg = new HibernatePersistenceConfiguration("emf")
-            .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", ""))
             .property("hibernate.hbm2ddl.auto", "update")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
final PersistenceConfiguration cfg = new HibernatePersistenceConfiguration("emf")
.jdbcUrl("jdbc:mysql://localhost:3306/film_database")
.jdbcUsername("root")
.jdbcPassword("root")
.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);
final PersistenceConfiguration cfg = new HibernatePersistenceConfiguration("emf")
.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", ""))
.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);
🤖 Prompt for AI Agents
In src/main/java/Main.java around lines 13 to 21, the database URL, username and
password are hardcoded; replace these literals by reading configuration from
external sources (environment variables or a properties/config file) and pass
those values into the PersistenceConfiguration. Specifically: remove hardcoded
strings and obtain jdbcUrl, jdbcUsername and jdbcPassword from
System.getenv(...) or from a loaded java.util.Properties (with sensible defaults
or validation), wire the loaded values into the
HibernatePersistenceConfiguration call, and ensure secrets are not committed
(add .env or config file to .gitignore or use an external secrets manager).

try (EntityManagerFactory emf = cfg.createEntityManagerFactory()) {
emf.runInTransaction(em -> {
//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);

});

}

}
}