Skip to content

Simplify persistence configuration using Spring Boot JPA auto-configuration#127

Open
hyunjilee1132 wants to merge 2 commits intojaygajera17:master2from
hyunjilee1132:hyunjilee1132
Open

Simplify persistence configuration using Spring Boot JPA auto-configuration#127
hyunjilee1132 wants to merge 2 commits intojaygajera17:master2from
hyunjilee1132:hyunjilee1132

Conversation

@hyunjilee1132
Copy link
Copy Markdown

@hyunjilee1132 hyunjilee1132 commented Dec 30, 2025

Closes #128

Summary

This PR simplifies the persistence configuration by relying on Spring Boot’s JPA auto-configuration.

The following changes were made:

  • Removed the legacy HibernateConfiguration.java
  • Removed exclude settings from @SpringBootApplication
  • Unified database and JPA settings in application.properties
  • Let Spring Boot manage EntityManagerFactory and transaction configuration automatically

These changes reduce configuration conflicts between manual Hibernate setup and Spring Data JPA, and make the project easier to start and understand.


Motivation

The main reason for this change is developer experience, especially for beginners.

When cloning the project for learning purposes, the application could not be started easily due to multiple configuration errors related to persistence setup.
From a beginner’s point of view, this makes the learning process unnecessarily difficult.

Today, most Spring Boot projects rely on JPA with Hibernate as the underlying engine, managed through auto-configuration.
Compared to manually configuring Hibernate SessionFactory and transactions, working with Spring Data JPA is generally more approachable and widely used in modern Spring projects.

For this reason, I refactored the configuration to use a more beginner-friendly and commonly adopted setup, so that new learners can clone the repository and run the project with minimal friction.


Notes

After this change, the application runs successfully, but some Hibernate warning logs may still appear in the console (they are warnings, not errors).

I considered switching spring.jpa.hibernate.ddl-auto to validate to fully align entity mappings with the existing database schema.
However, doing so would require updating all entities in the models package to explicitly match table and column names.

To keep this PR focused on fixing startup and configuration issues, I did not include those entity changes here.

If you would like me to:

  • switch to ddl-auto=validate, and
  • update all entity mappings accordingly,

please leave a comment and I’ll be happy to follow up with another PR.

@abdurraffaysyed
Copy link
Copy Markdown

Hi @hyunjilee1132,

Thank you for your contribution. I was planning to do this enhancement but thank you for doing it and making the process a bit easier. The PR looks good to me.

I think it would be best if you can raise an issue and attach that issue to this PR. What do you think?

Copy link
Copy Markdown

@Mohrip Mohrip left a comment

Choose a reason for hiding this comment

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

LGT

@hyunjilee1132
Copy link
Copy Markdown
Author

Hi @abdurraffaysyed, thank you for the feedback! I've created the issue #128 and linked it to this PR as requested.
And thanks @Mohrip for the approval and the comment!

@hyunjilee1132
Copy link
Copy Markdown
Author

Thank you for the approval, @abdurraffaysyed and @Mohrip! I checked the action logs and noticed that the 'label' check failed because .github/labeler.yml is missing in the repository. Since this seems to be a repository configuration issue rather than an error in my code, could you please check if it's possible to proceed with the merge? Thank you!

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to simplify persistence setup by removing the custom Hibernate configuration and relying on Spring Boot’s Spring Data JPA auto-configuration, consolidating the relevant settings into application.properties.

Changes:

  • Removed legacy HibernateConfiguration.java (manual SessionFactory/transaction wiring).
  • Enabled default Spring Boot JPA auto-configuration by removing the explicit auto-config exclusion.
  • Replaced legacy db.* / hibernate.* properties with spring.datasource.* and spring.jpa.* settings.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
JtProject/src/main/resources/application.properties Switches to Spring Boot datasource + JPA properties and removes legacy persistence keys.
JtProject/src/main/java/com/jtspringproject/JtSpringProject/JtSpringProjectApplication.java Removes HibernateJpaAutoConfiguration exclusion to allow Boot JPA auto-config.
JtProject/src/main/java/com/jtspringproject/JtSpringProject/HibernateConfiguration.java Deletes the manual Hibernate SessionFactory/transaction configuration.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread JtProject/src/main/resources/application.properties Outdated
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;

@SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class)
@SpringBootApplication
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

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

With HibernateConfiguration removed, the project now relies on Boot JPA auto-configuration. However, multiple existing DAO classes still @Autowired an org.hibernate.SessionFactory and call getCurrentSession() (e.g., dao/userDao, dao/productDao, etc.). Boot’s JPA auto-config does not guarantee a SessionFactory bean or getCurrentSession() semantics, so this can fail at runtime with missing-bean or “No Session found for current thread” errors. Either migrate these DAOs to JPA (EntityManager/Spring Data repositories), or explicitly expose a SessionFactory bean derived from the JPA EntityManagerFactory and ensure transaction/session binding is configured accordingly.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the thorough review! You're absolutely right that the existing DAO classes still rely on SessionFactory and getCurrentSession(), which won't be automatically provided by Boot's JPA auto-configuration.

However, the scope of this PR is intentionally limited to simplifying the persistence configuration so that beginners can at least get the project running without manual Hibernate setup. Migrating all DAO classes from Hibernate SessionFactory to JPA EntityManager (or Spring Data repositories) would be a much larger change and is better suited as a separate follow-up.

I'll create a follow-up issue to track the DAO migration. For now, this PR focuses on removing the legacy configuration barrier that blocks initial project setup.

Comment on lines +7 to +8
spring.datasource.username=
spring.datasource.password=
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

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

spring.datasource.username and spring.datasource.password are set to empty values. With MySQL this typically results in authentication failing (attempting an anonymous user), which undermines the PR goal of “clone and run”. Consider either providing sensible local defaults (e.g., username=root) or using environment-variable placeholders with defaults (e.g., ${DB_USERNAME:root} / ${DB_PASSWORD:}) so the app starts out-of-the-box but remains configurable.

Suggested change
spring.datasource.username=
spring.datasource.password=
spring.datasource.username=${DB_USERNAME:root}
spring.datasource.password=${DB_PASSWORD:}

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Simplify persistence configuration using Spring Boot JPA auto-configuration

4 participants