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

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

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

问题

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

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

@Embeddable
@Data
public class Audit{
    private String name;
    private Audit auditor;
    private LocalDateTime creationDate;
}

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

@Entity
@Table(name = "BANK")
public class Bank{
    @Id
    private Long id;
    
    @Column(name = "BANK_NAME")
    private String bankName;
    
    @Embedded
    private Audit audit;
}

以及

@Entity
@Table(name = "CORPORATION")
public class Corporation{
    @Id
    private Long id;
    
    @Column(name = "CORPORATION_NAME")
    private String corporationName;
    
    @Embedded
    private Audit audit;
}
英文:

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:

@Embeddable
@Data
public class Audit{
private String name;
private Audit auditor;
private LocalDateTime creationDate;
}

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

    @Entity
    @Table(name = "BANK")
    public class Bank{
   
    @Id
    private Long id;
    
    @Column(name = "BANK_NAME")
    private String bankName;
    
    @Embedded
    private Audit audit;
    }

AND

 @Entity
    @Table(name = "CORPORATION")
    public class Corporation{
   
    @Id
    private Long id;
    
    @Column(name = "CORPORATION_NAME")
    private String corporationName;
    
    @Embedded
    private Audit audit;
    }

答案1

得分: 0

历史上,Hibernate 称这些组件为 "components"。而 JPA 则称其为 [embeddables](https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#embeddables)。不管怎样,概念是相同的:一种值的组合。

通常,可嵌入类型被用于**将多个基本类型映射**分组,并在多个实体之间**重用**。

Java 代码示例:

```java
@Data
@Entity(name = "Book")
public class Book {

    @Id
    @GeneratedValue
    private Long id;

    private String title;

    private String author;

    private Publisher publisher;

}

@Data
@Embeddable
public static class Publisher {

    @Column(name = "publisher_name")
    private String name;
    @Column(name = "publisher_country")
    private String country;

}

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

create table Book (
    id bigint not null,
    author varchar(255),
    publisher_country varchar(255),
    publisher_name varchar(255),
    title varchar(255),
    primary key (id)
)

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


<details>
<summary>英文:</summary>

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.

Most often, embeddable types are used **to group** multiple basic type mappings **and reuse them** across several entities.

Java Code Example:

    @Data
    @Entity(name = &quot;Book&quot;)
    public class Book {
    
    	@Id
    	@GeneratedValue
    	private Long id;
    
    	private String title;
    
    	private String author;
    
    	private Publisher publisher;
    
    }
    
    @Data
    @Embeddable
    public static class Publisher {
    
        @Column(name = &quot;publisher_name&quot;)
    	private String name;
        @Column(name = &quot;publisher_country&quot;)
    	private String country;

    }

And this is SQL to show how your table should look like:

    create table Book (
        id bigint not null,
        author varchar(255),
        publisher_country varchar(255),
        publisher_name varchar(255),
        title varchar(255),
        primary key (id)
    )

More details can be found in the [documentation](https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#embeddables) :)

</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:

确定