Hibernate实体内的复杂对象作为内部类

huangapple go评论54阅读模式
英文:

Complex Object as Inner Class inside Hibernate Entity

问题

现在,我知道内部类不能作为Hibernate中的实体。

我首先会展示我的代码,请参考我下面的问题:

@Entity
@Table(name = "bags")
public class Bags extends AbstractModel {

    private String brand;
    private String condition;
    private String size;
    private Extras extras;

    @ManyToOne
    private Customer customer;

    private class Extras {
        private boolean box;
        private boolean authenticity_card;
        private boolean shoulder_strap;
        private boolean dustbag;
        private boolean pouch;
        private boolean padlock_and_key;
        private boolean bagcharm;
        private boolean nameTag;
        private boolean mirror;
   }
}

已省略Getter和Setter。我的问题是:

如果我想要一个稍微复杂一些的对象,比如Extras,在其中表示多个配饰的存在与否,是创建与bags相关联的附加表会更好,还是有其他方法可以解决这个问题?

如果我表达不清楚或者您需要额外的信息,请告诉我。

英文:

Now, I'm aware that an Inner class cannot be an Entity in Hibernate.

I'll first show my code, please refer to my question below:

@Entity
@Table(name = "bags")
public class Bags extends AbstractModel {

    private String brand;
    private String condition;
    private String size;
    private Extras extras;

    @ManyToOne
    private Customer customer;

    private class Extras {
        private boolean box;
        private boolean authenticity_card;
        private boolean shoulder_strap;
        private boolean dustbag;
        private boolean pouch;
        private boolean padlock_and_key;
        private boolean bagcharm;
        private boolean nameTag;
        private boolean mirror;
   }
}

Getters and setters are ommited. My question is:

If I want to have a slightly more complex object such as Extras, in which I represent the absence or not of several accessories, would it be better to create an additional table associated with bags OR is there a way around this?

Please let me know if I was not clear or you require additional information.

答案1

得分: 4

@Embeddable注解用于声明一个类将被其他实体嵌入。

@Embeddable
public class Extras {
    private boolean box;
    private boolean authenticity_card;
    private boolean shoulder_strap;
    private boolean dustbag;
    private boolean pouch;
    private boolean padlock_and_key;
    private boolean bagcharm;
    private boolean nameTag;
    private boolean mirror;
}

@Embedded用于将一个类型嵌入另一个实体中。

@Entity
@Table(name = "bags")
public class Bags extends AbstractModel {

    private String brand;
    private String condition;
    private String size;
    private Extras extras;

    @ManyToOne
    private Customer customer;

    @Embedded
    private Extras extras;
}
英文:

@Embeddable annotation is used to declare a class will be embedded by other entities.

@Embeddable
public class Extras {
        private boolean box;
        private boolean authenticity_card;
        private boolean shoulder_strap;
        private boolean dustbag;
        private boolean pouch;
        private boolean padlock_and_key;
        private boolean bagcharm;
        private boolean nameTag;
        private boolean mirror;
}

@Embedded is used to embed a type into another entity.

@Entity
@Table(name = "bags")
public class Bags extends AbstractModel {

    private String brand;
    private String condition;
    private String size;
    private Extras extras;

    @ManyToOne
    private Customer customer;

    @Embedded
    private Extras extras;
}

huangapple
  • 本文由 发表于 2020年9月16日 18:07:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63917738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定