@ElementCollection @Embeddable

直接上代码:

@Entity
@Data
public class TestA {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ElementCollection
    private List<TestB> testBList;
    @Data
    @Embeddable
    public static class TestB{
        private String address;
    }

    @ElementCollection
    private List<String> info;

    @Embedded
    private TestC testC;
    @Data
    @Embeddable
    public static class TestC{
        private String able;
    }
}

对应生成的表结构是:

CREATE TABLE IF NOT EXISTS testa (
  id BIGINT AUTO_INCREMENT PRIMARY KEY,
  able VARCHAR(128)
);

CREATE TABLE IF NOT EXISTS testa_testblist (
  testa_id BIGINT,
  address VARCHAR(128)
);

CREATE TABLE IF NOT EXISTS testa_info (
  testa_id BIGINT,
  info VARCHAR(128)
);

看了一下源码:

@ElementCollection Defines a collection of instances of a basic type or embeddable class . Must be specified if the collection is to be mapped by means of a collection table.

@Embeddable Defines a class whose instances are stored as an intrinsic part of an owning entity and share the identity of the entity. Each of the persistent properties or fields of the embedded object is mapped to the database table for the entity.

@ Embedded Specifies a persistent field or property of an entity whose value is an instance of an embeddable class. The embeddable class must be annotated as {@Embeddable}.

转载于:https://my.oschina.net/maomaolsm/blog/920385

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值