在Hibernate中是否有一种方法可以使用可嵌入共享对象?

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

Is there a way to use share embeddable object in Hibernate?

问题

我遇到了可重复使用的类字段的情况,我想将它们标记为@embeddable,然而问题是 - JPA是否允许在其他不同的类中多次重复使用一个类作为embeddable呢?

例如,我的可嵌入类如下所示:

  1. @Embeddable
  2. @Data
  3. public class Audit{
  4. private String name;
  5. private Audit auditor;
  6. private LocalDateTime creationDate;
  7. }

是否可以像以下示例一样将Audit嵌入多个不同的类中:

  1. @Entity
  2. @Table(name = "BANK")
  3. public class Bank{
  4. @Id
  5. private Long id;
  6. @Column(name = "BANK_NAME")
  7. private String bankName;
  8. @Embedded
  9. private Audit audit;
  10. }

以及

  1. @Entity
  2. @Table(name = "CORPORATION")
  3. public class Corporation{
  4. @Id
  5. private Long id;
  6. @Column(name = "CORPORATION_NAME")
  7. private String corporationName;
  8. @Embedded
  9. private Audit audit;
  10. }
英文:

I have a situation with repeatable class fields which I want to mark as @embeddable, however the question is - does JPA allow re-utilizing a class multiple times as embeddable in other different classes?

E.g. my embeddable class looks as follows:

  1. @Embeddable
  2. @Data
  3. public class Audit{
  4. private String name;
  5. private Audit auditor;
  6. private LocalDateTime creationDate;
  7. }

Is it possible to embed the Audit into multiple different classes as for ex.:

  1. @Entity
  2. @Table(name = "BANK")
  3. public class Bank{
  4. @Id
  5. private Long id;
  6. @Column(name = "BANK_NAME")
  7. private String bankName;
  8. @Embedded
  9. private Audit audit;
  10. }

AND

  1. @Entity
  2. @Table(name = "CORPORATION")
  3. public class Corporation{
  4. @Id
  5. private Long id;
  6. @Column(name = "CORPORATION_NAME")
  7. private String corporationName;
  8. @Embedded
  9. private Audit audit;
  10. }

答案1

得分: 0

  1. 历史上,Hibernate 称这些组件为 "components"。而 JPA 则称其为 [embeddables](https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#embeddables)。不管怎样,概念是相同的:一种值的组合。
  2. 通常,可嵌入类型被用于**将多个基本类型映射**分组,并在多个实体之间**重用**。
  3. Java 代码示例:
  4. ```java
  5. @Data
  6. @Entity(name = "Book")
  7. public class Book {
  8. @Id
  9. @GeneratedValue
  10. private Long id;
  11. private String title;
  12. private String author;
  13. private Publisher publisher;
  14. }
  15. @Data
  16. @Embeddable
  17. public static class Publisher {
  18. @Column(name = "publisher_name")
  19. private String name;
  20. @Column(name = "publisher_country")
  21. private String country;
  22. }

以下是 SQL,展示了你的表应该是什么样子的:

  1. create table Book (
  2. id bigint not null,
  3. author varchar(255),
  4. publisher_country varchar(255),
  5. publisher_name varchar(255),
  6. title varchar(255),
  7. primary key (id)
  8. )

更多细节可以在文档中找到 🙂

  1. <details>
  2. <summary>英文:</summary>
  3. Historically Hibernate called these components. JPA calls them [embeddables](https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#embeddables). Either way, the concept is the same: a composition of values.
  4. Most often, embeddable types are used **to group** multiple basic type mappings **and reuse them** across several entities.
  5. Java Code Example:
  6. @Data
  7. @Entity(name = &quot;Book&quot;)
  8. public class Book {
  9. @Id
  10. @GeneratedValue
  11. private Long id;
  12. private String title;
  13. private String author;
  14. private Publisher publisher;
  15. }
  16. @Data
  17. @Embeddable
  18. public static class Publisher {
  19. @Column(name = &quot;publisher_name&quot;)
  20. private String name;
  21. @Column(name = &quot;publisher_country&quot;)
  22. private String country;
  23. }
  24. And this is SQL to show how your table should look like:
  25. create table Book (
  26. id bigint not null,
  27. author varchar(255),
  28. publisher_country varchar(255),
  29. publisher_name varchar(255),
  30. title varchar(255),
  31. primary key (id)
  32. )
  33. More details can be found in the [documentation](https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#embeddables) :)
  34. </details>

huangapple
  • 本文由 发表于 2020年8月17日 03:45:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63441268.html
匿名

发表评论

匿名网友

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

确定