如何在JPA Buddy中使用`@CollectionTable`

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

How to use `@CollectionTable` with JPA Buddy

问题

我尝试使用JPA Buddy生成Liquibase初始化Changelog。

不知何故,JPA Buddy正在忽略使用@CollectionTable声明的表。这是JPA Buddy没有覆盖的功能吗?

这是我的定义:

@ElementCollection(targetClass = CareerLevel.class)
@Enumerated(EnumType.STRING)
@CollectionTable(name = "inventory_bundle_career_level",
        joinColumns = {@JoinColumn(name = "inventory_bundle_id", columnDefinition = "CHAR(36)")})
@Column(name = "career_level", columnDefinition = "VARCHAR(50)")
private List<CareerLevel> careerLevel = new ArrayList<>();
英文:

I try to generate a Liquibase init Changelog with JPA Buddy.

Somehow, JPA Buddy is ignoring tables declared with @CollectionTable. Is it a feature that JPA Buddy does not cover?

Here is my definition:

    @ElementCollection(targetClass = CareerLevel.class)
    @Enumerated(EnumType.STRING)
    @CollectionTable(name = &quot;inventory_bundle_career_level&quot;,
            joinColumns = {@JoinColumn(name = &quot;inventory_bundle_id&quot;, columnDefinition = &quot;CHAR(36)&quot;)})
    @Column(name = &quot;career_level&quot;, columnDefinition = &quot;VARCHAR(50)&quot;)
    private List&lt;CareerLevel&gt; careerLevel = new ArrayList&lt;&gt;();

答案1

得分: 2

我正在使用 JPA Buddy 的最新版本:2023.2.3,并且 JPA Buddy 正确为 @CollectionTable 生成表格。例如,我根据您提供的代码片段创建了以下模型:

@Entity
@Table(name = "inventory_bundle")
public class InventoryBundle {
    @Id
    @Column(name = "id", columnDefinition = "CHAR(36)")
    private String id;

    @ElementCollection(targetClass = CareerLevel.class)
    @Enumerated(EnumType.STRING)
    @CollectionTable(
            name = "inventory_bundle_career_level",
            joinColumns = {
                    @JoinColumn(name = "inventory_bundle_id", columnDefinition = "CHAR(36)")
            })
    @Column(name = "career_level", columnDefinition = "VARCHAR(50)")
    private List<CareerLevel> careerLevel = new ArrayList<>();
}

public enum CareerLevel {
    ENTRY_LEVEL,
    JUNIOR,
    MID_LEVEL,
    SENIOR,
    EXECUTIVE
}

而 JPA Buddy 为它生成了以下脚本:

CREATE TABLE inventory_bundle
(
    id CHAR(36) NOT NULL,
    CONSTRAINT pk_inventory_bundle PRIMARY KEY (id)
);

CREATE TABLE inventory_bundle_career_level
(
    inventory_bundle_id CHAR(36) NOT NULL,
    career_level        VARCHAR(50)
);

ALTER TABLE inventory_bundle_career_level
    ADD CONSTRAINT fk_inventory_bundle_career_level_on_inventory_bundle FOREIGN KEY (inventory_bundle_id) REFERENCES inventory_bundle (id);

这个脚本与 Hibernate ddl-auto 生成的内容完全匹配。也许您的模型有些许不同?如果您能提供更详细的情况描述,我将不胜感激,这样我就可以进一步检查它了。 😊

英文:

Which version of JPA Buddy are you using?
I'm using the latest version of JPA Buddy: 2023.2.3 and JPA Buddy generates a table for @CollectionTable correctly. For example, I created the following model based on the code snippet you provided:

@Entity
@Table(name = &quot;inventory_bundle&quot;)
public class InventoryBundle {
    @Id
    @Column(name = &quot;id&quot;, columnDefinition = &quot;CHAR(36)&quot;)
    private String id;

    @ElementCollection(targetClass = CareerLevel.class)
    @Enumerated(EnumType.STRING)
    @CollectionTable(
            name = &quot;inventory_bundle_career_level&quot;,
            joinColumns = {
                    @JoinColumn(name = &quot;inventory_bundle_id&quot;, columnDefinition = &quot;CHAR(36)&quot;)
            })
    @Column(name = &quot;career_level&quot;, columnDefinition = &quot;VARCHAR(50)&quot;)
    private List&lt;CareerLevel&gt; careerLevel = new ArrayList&lt;&gt;();
}

public enum CareerLevel {
    ENTRY_LEVEL,
    JUNIOR,
    MID_LEVEL,
    SENIOR,
    EXECUTIVE
}

And JPA Buddy generated the following script for it:

CREATE TABLE inventory_bundle
(
    id CHAR(36) NOT NULL,
    CONSTRAINT pk_inventory_bundle PRIMARY KEY (id)
);

CREATE TABLE inventory_bundle_career_level
(
    inventory_bundle_id CHAR(36) NOT NULL,
    career_level        VARCHAR(50)
);

ALTER TABLE inventory_bundle_career_level
    ADD CONSTRAINT fk_inventory_bundle_career_level_on_inventory_bundle FOREIGN KEY (inventory_bundle_id) REFERENCES inventory_bundle (id);

This script matches exactly what Hibernate ddl-auto generates. Maybe your model is slightly different? I would appreciate a more detailed description of the case so that I can check it out 🙂

huangapple
  • 本文由 发表于 2023年6月19日 20:58:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506881.html
匿名

发表评论

匿名网友

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

确定