Simplify persistence configuration using Spring Boot JPA auto-configuration#127
Simplify persistence configuration using Spring Boot JPA auto-configuration#127hyunjilee1132 wants to merge 2 commits intojaygajera17:master2from
Conversation
…ration for easier startup
|
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? |
|
Hi @abdurraffaysyed, thank you for the feedback! I've created the issue #128 and linked it to this PR as requested. |
|
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! |
There was a problem hiding this comment.
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(manualSessionFactory/transaction wiring). - Enabled default Spring Boot JPA auto-configuration by removing the explicit auto-config exclusion.
- Replaced legacy
db.*/hibernate.*properties withspring.datasource.*andspring.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.
| import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; | ||
|
|
||
| @SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class) | ||
| @SpringBootApplication |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| spring.datasource.username= | ||
| spring.datasource.password= |
There was a problem hiding this comment.
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.
| spring.datasource.username= | |
| spring.datasource.password= | |
| spring.datasource.username=${DB_USERNAME:root} | |
| spring.datasource.password=${DB_PASSWORD:} |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Closes #128
Summary
This PR simplifies the persistence configuration by relying on Spring Boot’s JPA auto-configuration.
The following changes were made:
HibernateConfiguration.javaexcludesettings from@SpringBootApplicationapplication.propertiesEntityManagerFactoryand transaction configuration automaticallyThese 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
SessionFactoryand 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-autotovalidateto fully align entity mappings with the existing database schema.However, doing so would require updating all entities in the
modelspackage 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:
ddl-auto=validate, andplease leave a comment and I’ll be happy to follow up with another PR.