generated from kappsegla/maven-java-template
-
Notifications
You must be signed in to change notification settings - Fork 19
Feature #4
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
Closed
Closed
Feature #4
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cd58553
add deadline
github-classroom[bot] f043eb4
Add Main and Film class.
linneawardalhotmailcom 86c2ac2
Add Director Class with getters and setters.
linneawardalhotmailcom 5c33218
Add entity manager to Main.
linneawardalhotmailcom c5b0d5f
Add OneToMany in Director Class.
linneawardalhotmailcom 3f3262f
Made yearOfDeath nullable.
linneawardalhotmailcom 5125f3a
Fixed issues according to Coderabbit.
linneawardalhotmailcom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| 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); | ||
|
|
||
| }); | ||
|
|
||
| } | ||
|
|
||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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
🤖 Prompt for AI Agents