Liquibase:创建表时如何建模集合和关系?

时间:2014-10-12 16:12:02

标签: java jpa liquibase

考虑这个实体

@Entity
public class User {
    @Id
    private String id;
    private String firstName;
    private String lastName;
    private String nickName;
    @ElementCollection
    private final List<String> emails = new ArrayList<>();
    @OneToMany
    private final List<Address> addresses = new ArrayList<>();
    @OneToMany
    private final List<Phone> phones = new ArrayList<>();

    public User(@Nonnull final String firstName, @Nonnull final String lastName) {
        id = UUID.randomUUID().toString();
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public User() {
        // required by JPA
    }

    public String getId() {
        return id;
    }

    @Nonnull
    public String getFirstName() {
        return firstName;
    }

    @Nonnull
    public String getLastName() {
        return lastName;
    }

    @Nonnull
    public String getNickName() {
        return nickName;
    }

    public void setNickName(@Nonnull final String nickName) {
        this.nickName = nickName;
    }

    @Nonnull
    public List<String> getEmails() {
        return Collections.unmodifiableList(emails);
    }

    @Nonnull
    public List<Address> getAddresses() {
        return Collections.unmodifiableList(addresses);
    }

    @Nonnull
    public List<Phone> getPhones() {
        return Collections.unmodifiableList(phones);
    }

    public void addAddress(@Nonnull final Address address) {
        addresses.add(address);
    }

    public void addPhone(@Nonnull final Phone phone) {
        phones.add(phone);
    }

    public void addEmail(@Nonnull final String email) {
        emails.add(email);
    }

    // likewise remove address, phones, emails could be added
}

我现在正在为此表创建一个liquibase变更集,并且不知道如何建模

    @ElementCollection
    private final List<String> emails = new ArrayList<>();
    @OneToMany
    private final List<Address> addresses = new ArrayList<>();
    @OneToMany
    private final List<Phone> phones = new ArrayList<>();

有人可以帮忙吗?

我当前(不完整)的变更集看起来像

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
    <changeSet author="harit" id="10122014.0907">
        <comment>Add User Table</comment>
        <createTable tableName="user">
            <column name="id" type="VARCHAR(36)">
                <constraints nullable="false" unique="true"/>
            </column>
            <column name="firstName" type="VARCHAR(50)">
                <constraints nullable="false"/>
            </column>
            <column name="lastName" type="VARCHAR(50)">
                <constraints nullable="false"/>
            </column>
            <column name="nickName" type="VARCHAR(20)"/>
    </changeSet>
</databaseChangeLog>

1 个答案:

答案 0 :(得分:0)

您可以为支持电子邮件,地址和电话的每个表创建单独的changeSet。

我不确定这些对象中有什么,但更改日志会是这样的:

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
    <changeSet author="harit" id="10122014.0907">
        <comment>Add User Table</comment>
        <createTable tableName="user">
            <column name="id" type="VARCHAR(36)">
                <constraints nullable="false" unique="true"/>
            </column>
            <column name="firstName" type="VARCHAR(50)">
                <constraints nullable="false"/>
            </column>
            <column name="lastName" type="VARCHAR(50)">
                <constraints nullable="false"/>
            </column>
            <column name="nickName" type="VARCHAR(20)"/>
    </changeSet>

    <changeSet author="harit" id="10122014.1001">
        <createTable tableName="address">
            <column name="id" type="VARCHAR(36)">
                <constraints nullable="false" unique="true"/>
            </column>
            <column name="line1" type="VARCHAR(50)">
                <constraints nullable="false"/>
            </column>
            <column name="line2" type="VARCHAR(50)"/>
            <column name="city" type="VARCHAR(50)">
                <constraints nullable="false"/>
            </column>
    </changeSet>

......

</databaseChangeLog>