Skip to content

Add PostgreSQL, H2, and Hibernate configuration with environment profiles#7

Merged
Abhay2133 merged 2 commits intomainfrom
copilot/fix-32565608-3e26-46ef-9c76-e9f659ff4a60
Sep 28, 2025
Merged

Add PostgreSQL, H2, and Hibernate configuration with environment profiles#7
Abhay2133 merged 2 commits intomainfrom
copilot/fix-32565608-3e26-46ef-9c76-e9f659ff4a60

Conversation

Copy link
Contributor

Copilot AI commented Sep 27, 2025

Summary

This PR implements comprehensive database configuration for the Spring Boot application, adding support for PostgreSQL (development/production), H2 (testing), and Hibernate ORM with environment-based configuration management.

Changes Made

Database Dependencies

  • Added spring-boot-starter-data-jpa for Hibernate/JPA support
  • Added PostgreSQL driver for production database connectivity
  • Added H2 database for in-memory testing

Environment Configuration

Created profile-specific application properties:

  • Development (application-dev.properties): PostgreSQL with hibernate.ddl-auto=update and SQL logging
  • Testing (application-test.properties): H2 in-memory with hibernate.ddl-auto=create-drop
  • Production (application-prod.properties): PostgreSQL with hibernate.ddl-auto=validate for safety

Environment Variables Support

  • Added .env.example template with all required database configuration variables
  • Updated main application.properties to use environment variables for database credentials
  • Added .env files to .gitignore for security

Sample Implementation

  • Created User entity demonstrating JPA/Hibernate functionality
  • Added UserRepository with common database operations (findByUsername, existsByEmail, etc.)
  • Implemented REST API endpoints for database interaction:
    • POST /users - Create new users
    • GET /users - List all users
    • GET /users/search?username= - Find users by username

Testing

  • Updated existing tests to use the test profile with H2 database
  • Added comprehensive UserRepositoryTest with JPA repository testing
  • All 19 tests passing, including new database functionality tests

Documentation

  • Updated README with complete database setup instructions
  • Added PostgreSQL installation guide (Docker and local)
  • Documented environment variable configuration
  • Updated technology stack and features sections
  • Added API endpoint documentation for database operations

Verification

The implementation has been thoroughly tested:

  1. PostgreSQL Configuration: Application correctly attempts to connect to PostgreSQL in dev/prod profiles
  2. H2 Testing: All tests pass using H2 in-memory database with proper schema creation
  3. API Functionality: Database endpoints successfully create, retrieve, and search users
  4. Environment Switching: Profiles switch correctly between database configurations
  5. Schema Management: Hibernate properly handles schema creation/validation per environment

Database Schema

The application automatically creates the following table structure:

CREATE TABLE users (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    username VARCHAR(255) NOT NULL UNIQUE,
    email VARCHAR(255) NOT NULL,
    created_at TIMESTAMP
);

Usage Example

After setting up .env with database credentials:

# Development with PostgreSQL
SPRING_PROFILES_ACTIVE=dev ./mvnw spring-boot:run

# Testing with H2 (automatic in tests)
./mvnw test

# Create a user via API
curl -X POST "http://localhost:8080/users?username=john&email=john@example.com"

This implementation provides a solid foundation for database operations while maintaining backward compatibility with existing functionality.

Original prompt

This section details on the original issue you should resolve

<issue_title>Add PostgreSQL, H2 (testing), and environment configuration with Hibernate</issue_title>
<issue_description>
The Spring Boot application currently lacks proper database configuration and ORM setup. We want to add support for:

  1. PostgreSQL as the primary database for development and production.
  2. H2 in-memory database for testing purposes.
  3. Hibernate (via Spring Data JPA) as the ORM.
  4. .env or environment-based configuration to manage DB credentials securely.

Tasks:

  • Add dependencies in pom.xml:

    • spring-boot-starter-data-jpa (Hibernate + JPA)
    • PostgreSQL driver
    • H2 database for testing
  • Configure application.properties or application.yml for multiple environments (dev, test, prod).

  • Use environment variables (.env) for DB credentials.

  • Set spring.jpa.hibernate.ddl-auto appropriately:

    • update for dev
    • create-drop for test
    • validate for prod
  • Verify Hibernate entities are mapped correctly to both PostgreSQL and H2.

  • Document steps to configure .env and run the application.

Acceptance Criteria:

  • The app connects to PostgreSQL in dev/production via Hibernate.
  • Tests run against H2 in-memory DB.
  • DB credentials are securely loaded from environment variables.
  • Hibernate logs SQL queries for development.
    </issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #6

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Abhay2133 Abhay2133 marked this pull request as ready for review September 27, 2025 16:04
@coderabbitai
Copy link

coderabbitai bot commented Sep 27, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


Comment @coderabbitai help to get the list of available commands and usage tips.

…nvironment profiles

Co-authored-by: Abhay2133 <63441094+Abhay2133@users.noreply.github.com>
Copilot AI changed the title [WIP] Add PostgreSQL, H2 (testing), and environment configuration with Hibernate Add PostgreSQL, H2, and Hibernate configuration with environment profiles Sep 27, 2025
Copilot AI requested a review from Abhay2133 September 27, 2025 16:19
@Abhay2133 Abhay2133 merged commit a455af9 into main Sep 28, 2025
6 checks passed
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.

Add PostgreSQL, H2 (testing), and environment configuration with Hibernate

2 participants