在 Hibernate 中将自定义对象属性存储为列

Store Custom Object Attributes as Columns in Hibernate

我们如何使用自定义对象属性作为 table 列

生成 table
private class CustomObject {   
    String attr1;
    String attr2;
}
@Entity
@Table(name = "result")
public class Result {
   @Column
   private  String id;

   @Column
   public CustomObject cobject;
}

结果table生成如下

id attr1 attr2

来自 Hibernate_User_Guide 文档:

PA defines two terms for working with an embeddable type: @Embeddable and @Embedded. @Embeddable is used to describe the mapping type itself (e.g. Publisher).@Embedded is for referencing a given embeddable type (e.g. book.publisher).

所以您可以像下面这样注释您的 类:

@Embeddable
private class CustomObject {   
    String attr1;
    String attr2;
}

@Entity
@Table(name = "result")
public class Result {
   @Column
   private  String id;

   @Embedded
   public CustomObject cobject;
}