Hibernate mappings allow developers to define how Java objects are persisted in the database. With Firebird and Spring Boot, you can use advanced mapping techniques to handle complex relationships efficiently.
1. Basic Setup
Use the Firebird JDBC driver and Spring Data JPA dependency in your
pom.xml:
<dependency>
<groupId>org.firebirdsql.jdbc</groupId>
<artifactId>jaybird-jdk18</artifactId>
<version>5.0.3.java18</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
And in
application.properties:
spring.datasource.url=jdbc:firebirdsql://localhost:3050/demo.fdb
spring.datasource.username=sysdba
spring.datasource.password=masterkey
spring.jpa.hibernate.ddl-auto=update
2. One-to-One Mapping
Use the
@OneToOne annotation to link two entities, such as
User and
Profile.
@Entity
public class User {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "profile_id")
private Profile profile;
}
his ensures Firebird maintains referential integrity between tables.
3. One-to-Many Mapping
For parent-child relationships, use
@OneToMany with
mappedBy:
@Entity
public class Department {
@Id
private Long id;
private String name;
@OneToMany(mappedBy = "department", cascade = CascadeType.ALL)
private List<Employee> employees;
}
And the
Employee entity:
@Entity
public class Employee {
@Id
private Long id;
private String name;
@ManyToOne
@JoinColumn(name = "department_id")
private Department department;
}
4. Many-to-Many Mapping
To model many-to-many relationships, such as
Student and
Course, use a join table:
@Entity
public class Student {
@Id
private Long id;
private String name;
@ManyToMany
@JoinTable(
name = "student_course",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "course_id")
)
private Set<Course> courses = new HashSet<>();
}
5. Inheritance Mapping
Hibernate supports inheritance for Firebird using
@Inheritance. Example:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Payment {
@Id
private Long id;
private Double amount;
}
@Entity
public class CreditCardPayment extends Payment {
private String cardNumber;
}
This structure keeps each subclass in a separate table while maintaining parent relationships.
6. Using Composite Keys
Firebird supports composite keys via
@Embeddable. Example:
@Embeddable
public class OrderId implements Serializable {
private Long customerId;
private Long orderNumber;
}
@Entity
public class Order {
@EmbeddedId
private OrderId id;
private LocalDate orderDate;
}
This allows complex identifiers that reflect real-world constraints.
image quote pre code