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

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

Complex Object as Inner Class inside Hibernate Entity

问题

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

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

  1. @Entity
  2. @Table(name = "bags")
  3. public class Bags extends AbstractModel {
  4. private String brand;
  5. private String condition;
  6. private String size;
  7. private Extras extras;
  8. @ManyToOne
  9. private Customer customer;
  10. private class Extras {
  11. private boolean box;
  12. private boolean authenticity_card;
  13. private boolean shoulder_strap;
  14. private boolean dustbag;
  15. private boolean pouch;
  16. private boolean padlock_and_key;
  17. private boolean bagcharm;
  18. private boolean nameTag;
  19. private boolean mirror;
  20. }
  21. }

已省略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:

  1. @Entity
  2. @Table(name = "bags")
  3. public class Bags extends AbstractModel {
  4. private String brand;
  5. private String condition;
  6. private String size;
  7. private Extras extras;
  8. @ManyToOne
  9. private Customer customer;
  10. private class Extras {
  11. private boolean box;
  12. private boolean authenticity_card;
  13. private boolean shoulder_strap;
  14. private boolean dustbag;
  15. private boolean pouch;
  16. private boolean padlock_and_key;
  17. private boolean bagcharm;
  18. private boolean nameTag;
  19. private boolean mirror;
  20. }
  21. }

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注解用于声明一个类将被其他实体嵌入。

  1. @Embeddable
  2. public class Extras {
  3. private boolean box;
  4. private boolean authenticity_card;
  5. private boolean shoulder_strap;
  6. private boolean dustbag;
  7. private boolean pouch;
  8. private boolean padlock_and_key;
  9. private boolean bagcharm;
  10. private boolean nameTag;
  11. private boolean mirror;
  12. }

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

  1. @Entity
  2. @Table(name = "bags")
  3. public class Bags extends AbstractModel {
  4. private String brand;
  5. private String condition;
  6. private String size;
  7. private Extras extras;
  8. @ManyToOne
  9. private Customer customer;
  10. @Embedded
  11. private Extras extras;
  12. }
英文:

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

  1. @Embeddable
  2. public class Extras {
  3. private boolean box;
  4. private boolean authenticity_card;
  5. private boolean shoulder_strap;
  6. private boolean dustbag;
  7. private boolean pouch;
  8. private boolean padlock_and_key;
  9. private boolean bagcharm;
  10. private boolean nameTag;
  11. private boolean mirror;
  12. }

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

  1. @Entity
  2. @Table(name = "bags")
  3. public class Bags extends AbstractModel {
  4. private String brand;
  5. private String condition;
  6. private String size;
  7. private Extras extras;
  8. @ManyToOne
  9. private Customer customer;
  10. @Embedded
  11. private Extras extras;
  12. }

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:

确定