You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot重构时JPA EntityManagerFactory初始化失败求助

JPA映射错误解决方案:Could not determine type for Document

错误信息

Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: ru.kataproject.p_sm_airlines_1.entity.Document, at table: passenger, for columns: [org.hibernate.mapping.Column(document)]

问题根源

你在Passenger实体中给Document类型字段标记了@Column注解——JPA仅支持用@Column映射基本数据类型(字符串、数字、日期等),对于实体类之间的关联,必须使用JPA关联注解(如@OneToOne、@ManyToOne等)定义关系,否则Hibernate无法识别实体间的映射规则。

修复步骤

1. 修正Passenger类的Document关联

将@Column(name="document")替换为@OneToOne关联注解,同时配置关联字段和级联操作:

  • 用@JoinColumn指定数据库中存储关联ID的列名(规范命名为document_id,替代原不合理的document列)
  • 根据业务需求添加级联操作(比如CascadeType.ALL,实现创建/更新乘客时同步处理关联证件)

2. 修正Document类的Person关联(todo占位符替换)

原Document类中的person字符串占位符需改为实体关联,假设是与Person实体的一对一关系,使用@OneToOne注解定义反向关联。


修改后的代码

Document.java

import lombok.Getter;
import lombok.Setter;
import ru.kataproject.p_sm_airlines_1.util.enums.DocumentType;

import javax.persistence.*;
import java.time.LocalDateTime;

@Entity
@Getter
@Setter
@Table(name="document")
public class Document {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "document_gen")
    @SequenceGenerator(name = "document_gen", sequenceName = "document_sequence", allocationSize = 1)
    @Column(name = "id", nullable = false)
    private Long id;

    @Column(name = "document_type", nullable = false)
    private DocumentType documentType;

    @Column(name = "document_number")
    private String documentNumber;

    @Column(name = "registration_address")
    private String registrationAddress;

    @Column(name = "where_issued")
    private String whereIssued;

    @Column(name = "department_code")
    private String departmentCode;

    @Column(name = "date_of_issue")
    private LocalDateTime dateOfIssue;

    // 一对一关联Person实体(关系反向端)
    @OneToOne(mappedBy = "document")
    private Person person;
}

Passenger.java

import lombok.*;

import javax.persistence.*;
import java.time.LocalDate;

@Getter
@Setter
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "passenger", schema = "public")
public class Passenger {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private Long id;

    @NonNull
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "document_id", nullable = false) // 指定存储Document ID的外键列
    private Document document;

    @NonNull
    @Column(name="email")
    private String email;

    @NonNull
    @Column(name="first_name")
    private String first_name;

    @NonNull
    @Column(name="last_name")
    private String last_name;

    @NonNull
    @Column(name="middle_name")
    private String middle_name;

    @NonNull
    @Column(name="date_of_birth")
    private LocalDate date_of_birth;
}

额外注意事项

  • 数据库表需同步调整:passenger表需添加document_id外键列(关联document.id),删除原无效的document列
  • 若使用Liquibase,需编写对应变更集修改表结构
  • 确保Person实体已正确定义(若未实现需补充对应代码)

内容的提问来源于stack exchange,提问作者Kristina

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 06:05:20