JPA的可嵌入类中可以包含对象引用吗?

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

Can JPA Embeddable class contain an object reference?

问题

我正在尝试将一个包含对象引用的类嵌入到另一个类中。我总是会得到以下异常:
org.hibernate.MappingException: 无法确定类型:...
我的问题是,是否有任何方法可以嵌入一个包含对象引用的类,或者可嵌入的类只能存储原始数据类型。

我已经尝试过使用@Target()注解,但没有帮助。

英文:

I'm trying to embed a class to another which contains object reference. I always get

  1. org.hibernate.MappingException: Could not determine type for: ...

exception. My question is there any way I can embed a class which contains object references, or embeddable classes only stores primitives.

I'm already tried @Target() annotation but not helps.

答案1

得分: 2

是的,这是可能的。查看文档

  1. @Embeddable
  2. public static class Publisher {
  3. private String name;
  4. @ManyToOne(fetch = FetchType.LAZY)
  5. private Country country;
  6. //Getters and setters, equals and hashCode methods omitted for brevity
  7. }
  8. @Entity(name = "Book")
  9. @AttributeOverrides({
  10. @AttributeOverride(
  11. name = "ebookPublisher.name",
  12. column = @Column(name = "ebook_publisher_name")
  13. ),
  14. @AttributeOverride(
  15. name = "paperBackPublisher.name",
  16. column = @Column(name = "paper_back_publisher_name")
  17. )
  18. })
  19. @AssociationOverrides({
  20. @AssociationOverride(
  21. name = "ebookPublisher.country",
  22. joinColumns = @JoinColumn(name = "ebook_publisher_country_id")
  23. ),
  24. @AssociationOverride(
  25. name = "paperBackPublisher.country",
  26. joinColumns = @JoinColumn(name = "paper_back_publisher_country_id")
  27. )
  28. })
  29. public static class Book {
  30. @Id
  31. @GeneratedValue
  32. private Long id;
  33. private Publisher ebookPublisher;
  34. private Publisher paperBackPublisher;
  35. //Getters and setters are omitted for brevity
  36. }
英文:

Yes, this is possible. Look at the documentation.

  1. @Embeddable
  2. public static class Publisher {
  3. private String name;
  4. @ManyToOne(fetch = FetchType.LAZY)
  5. private Country country;
  6. //Getters and setters, equals and hashCode methods omitted for brevity
  7. }
  8. @Entity(name = "Book")
  9. @AttributeOverrides({
  10. @AttributeOverride(
  11. name = "ebookPublisher.name",
  12. column = @Column(name = "ebook_publisher_name")
  13. ),
  14. @AttributeOverride(
  15. name = "paperBackPublisher.name",
  16. column = @Column(name = "paper_back_publisher_name")
  17. )
  18. })
  19. @AssociationOverrides({
  20. @AssociationOverride(
  21. name = "ebookPublisher.country",
  22. joinColumns = @JoinColumn(name = "ebook_publisher_country_id")
  23. ),
  24. @AssociationOverride(
  25. name = "paperBackPublisher.country",
  26. joinColumns = @JoinColumn(name = "paper_back_publisher_country_id")
  27. )
  28. })
  29. public static class Book {
  30. @Id
  31. @GeneratedValue
  32. private Long id;
  33. private Publisher ebookPublisher;
  34. private Publisher paperBackPublisher;
  35. //Getters and setters are omitted for brevity
  36. }

huangapple
  • 本文由 发表于 2020年8月22日 19:34:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63535685.html
匿名

发表评论

匿名网友

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

确定