Skip to main content

Quiz: Database Configuration

Test your knowledge on PostgreSQL, Hibernate, and Lombok.

30 questions - Click on each question to reveal the answer.


Questions 1-10: PostgreSQL​

Question 1: How to add PostgreSQL to a Spring Boot project?

Answer:

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>

Question 2: How to configure the PostgreSQL connection?

Answer:

spring:
datasource:
url: jdbc:postgresql://localhost:5432/mydb
username: postgres
password: secret
driver-class-name: org.postgresql.Driver

Question 3: How to run PostgreSQL with Docker?

Answer:

docker run -d \
--name postgres \
-e POSTGRES_DB=mydb \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=secret \
-p 5432:5432 \
postgres:15

Question 4: What is the difference between H2 and PostgreSQL?

Answer:

H2PostgreSQL
In-memory (default)Persistent
Development/TestingProduction
No installationInstallation required
Data lost on restartPersistent data
LightweightFull-featured and robust

Question 5: How to configure the connection pool?

Answer: Spring Boot uses HikariCP by default:

spring:
datasource:
hikari:
maximum-pool-size: 10
minimum-idle: 5
connection-timeout: 30000
idle-timeout: 600000
max-lifetime: 1800000

Question 6: How to set a default schema?

Answer:

spring:
jpa:
properties:
hibernate:
default_schema: my_schema

Or in the URL:

url: jdbc:postgresql://localhost:5432/mydb?currentSchema=my_schema

Question 7: How to handle TimeZones with PostgreSQL?

Answer:

spring:
jpa:
properties:
hibernate:
jdbc:
time_zone: UTC

Recommended: store in UTC, convert on display.


Question 8: How to configure SSL for PostgreSQL?

Answer:

spring:
datasource:
url: jdbc:postgresql://host:5432/db?ssl=true&sslmode=require

Modes: disable, allow, prefer, require, verify-ca, verify-full


Question 9: How to create a database with Docker Compose?

Answer:

# docker-compose.yml
services:
db:
image: postgres:15
environment:
POSTGRES_DB: mydb
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
volumes:
- postgres-data:/var/lib/postgresql/data
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
ports:
- "5432:5432"

volumes:
postgres-data:

Question 10: How to externalize database credentials?

Answer:

spring:
datasource:
url: ${DB_URL}
username: ${DB_USER}
password: ${DB_PASSWORD}

Set environment variables:

export DB_URL=jdbc:postgresql://localhost/mydb
export DB_USER=postgres
export DB_PASSWORD=secret

Questions 11-20: Hibernate DDL​

Question 11: What is hibernate.ddl-auto?

Answer: Property that controls automatic schema generation:

ValueBehavior
noneNo action
validateValidates the schema
updateUpdates the schema
createCreates the schema (drop before)
create-dropCreates and drops on shutdown

Question 12: Which value to use in production?

Answer: none or validate

spring:
jpa:
hibernate:
ddl-auto: validate

In production, use migration tools (Flyway, Liquibase).


Question 13: How does Hibernate generate table names?

Answer: Default strategy: camelCase to snake_case

@Entity
public class UserProfile { }
// Table: user_profile

Customize:

spring:
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

Question 14: How to specify a table name?

Answer:

@Entity
@Table(name = "utilisateurs")
public class User {
// ...
}

Question 15: How to specify a column name?

Answer:

@Entity
public class User {

@Column(name = "nom_complet")
private String fullName;

@Column(name = "courriel", unique = true, nullable = false)
private String email;
}

Question 16: How to define an auto-generated primary key?

Answer:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

// Strategies:
// IDENTITY: DB auto-increment
// SEQUENCE: DB sequence
// TABLE: sequence table
// AUTO: automatic choice

Question 17: How to define a unique constraint?

Answer:

// On a column
@Column(unique = true)
private String email;

// On multiple columns
@Table(uniqueConstraints = {
@UniqueConstraint(columnNames = {"firstName", "lastName"})
})
public class User { }

Question 18: How to define an index?

Answer:

@Entity
@Table(indexes = {
@Index(name = "idx_email", columnList = "email"),
@Index(name = "idx_name", columnList = "lastName, firstName")
})
public class User { }

Question 19: How to see the SQL generated by Hibernate?

Answer:

spring:
jpa:
show-sql: true
properties:
hibernate:
format_sql: true

logging:
level:
org.hibernate.SQL: DEBUG
org.hibernate.orm.jdbc.bind: TRACE

Question 20: How to validate the schema on startup?

Answer:

spring:
jpa:
hibernate:
ddl-auto: validate

The application fails to start if the schema does not match the entities.


Questions 21-30: Lombok​

Question 21: What is Lombok?

Answer: Library that generates repetitive code via annotations:

  • Getters/Setters
  • Constructors
  • equals/hashCode
  • toString
  • Builder

Significantly reduces boilerplate.


Question 22: How to add Lombok?

Answer:

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

Also install the IDE plugin (IntelliJ, Eclipse).


Question 23: How to generate getters and setters?

Answer:

@Getter
@Setter
public class User {
private Long id;
private String name;
}

// Generates:
// getId(), setId()
// getName(), setName()

Question 24: What is @Data?

Answer: Combines several annotations:

@Data  // = @Getter + @Setter + @ToString + @EqualsAndHashCode + @RequiredArgsConstructor
public class User {
private Long id;
private String name;
}

Question 25: How to create a builder?

Answer:

@Builder
public class User {
private Long id;
private String name;
private String email;
}

// Usage:
User user = User.builder()
.name("John")
.email("john@example.com")
.build();

Question 26: How to generate constructors?

Answer:

@NoArgsConstructor  // Empty constructor
@AllArgsConstructor // All fields
@RequiredArgsConstructor // Final and @NonNull fields
public class User {
private final Long id;
@NonNull
private String name;
private String email;
}

Question 27: How to use @Slf4j for logging?

Answer:

@Slf4j
@Service
public class UserService {

public void process() {
log.info("Processing...");
log.debug("Details: {}", data);
log.error("Error occurred", exception);
}
}

Generates: private static final Logger log = LoggerFactory.getLogger(UserService.class);


Question 28: How to exclude a field from toString?

Answer:

@ToString(exclude = "password")
public class User {
private String email;
private String password; // Excluded from toString
}

// Or
@ToString.Exclude
private String password;

Question 29: How to exclude a field from equals/hashCode?

Answer:

@EqualsAndHashCode(exclude = "orders")
public class User {
private Long id;
private List<Order> orders; // Excluded
}

// Or
@EqualsAndHashCode.Exclude
private List<Order> orders;

Question 30: What are the best practices with Lombok and JPA?

Answer:

  1. Avoid @Data on entities (issues with lazy loading)
  2. Exclude relationships from equals/hashCode
  3. Use @ToString.Exclude for bidirectional relationships
  4. Prefer @Getter and @Setter separately
  5. Be cautious with @EqualsAndHashCode on entities
@Entity
@Getter
@Setter
@NoArgsConstructor
@ToString(exclude = "orders")
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class User {

@Id
@EqualsAndHashCode.Include
private Long id;

@OneToMany(mappedBy = "user")
private List<Order> orders;
}

Score​

ScoreLevel
27-30Excellent - Configuration mastered
21-26Good - Some revisions needed
15-20Average - Review the chapters
Less than 15Needs improvement

Back to Module 4