英文:
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论