【问题标题】:JPA, put complex data type inside an entity in the same tableJPA,将复杂数据类型放在同一张表中的实体内
【发布时间】:2026-01-20 07:40:01
【问题描述】:

我有一个类 Customer,它是一个实体: @实体

public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="customerId_Sequence")
@SequenceGenerator(name="customerId_Sequence",sequenceName="CUSTOMER_SEQ",allocationSize=1)
private long customerId;
private String firstName;
private String lastName; 
private BillingDetails billingDetails

我有一个类似于 BillingDetails 的类:

public class BillingDetails {
private String type;
private long ccNumber;

我使用休眠作为我的持久性提供程序。我希望它在 sql 中只创建一个表,其中包含 customerId、firstName、lastName、type、ccNumber 列。我希望所有内容都在一张表中,我不希望帐单详细信息成为一个实体。这可能吗?

当我这样尝试时,我收到一个错误:无法确定类型:****.BillingDetails

【问题讨论】:

    标签: java sql hibernate jpa orm


    【解决方案1】:

    将 BillingDetails 建模为 @Embeddable。

    @Entity
    public class Customer {
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="customerId_Sequence")
    @SequenceGenerator(name="customerId_Sequence",sequenceName="CUSTOMER_SEQ",allocationSize=1)
    private long customerId;
    private String firstName;
    private String lastName; 
    
    @Embedded
    private BillingDetails billingDetails;
    ...
    }
    
    
    
    @Embeddable
    public class BillingDetails {
    private String type;
    private long ccNumber;
    ...
    }
    

    【讨论】: