简体   繁体   English

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

[英]Complex Object as Inner Class inside Hibernate Entity

Now, I'm aware that an Inner class cannot be an Entity in Hibernate.现在,我知道 Inner 类不能是 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.省略了 getter 和 setter。 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?如果我想要一个稍微复杂一些的对象,例如 Extras,在其中我表示没有几个配件,那么创建一个与包相关的附加表会更好,或者有没有办法解决这个问题?

Please let me know if I was not clear or you require additional information.如果我不清楚或者您需要其他信息,请告诉我。

@Embeddable annotation is used to declare a class will be embedded by other entities. @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 is used to embed a type into another entity. @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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM