Home > Mobile >  Store Custom Object Attributes as Columns in Hibernate
Store Custom Object Attributes as Columns in Hibernate

Time:09-23

how can we generate table using custom object attribute as table column

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

   @Column
   public CustomObject cobject;
}

RESULT table generate as follow

id attr1 attr2

CodePudding user response:

From Hibernate_User_Guide documentation:

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).

So you can annotate your classes like below:

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

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

   @Embedded
   public CustomObject cobject;
}
  • Related