当我更改实体字段的大小时,它不会在数据库中更改

时间:2015-03-27 09:03:59

标签: mysql hibernate jpa

我有一个实体类组。

更改前

@Entity
@Table(name = "Group")

public class Group implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;
    private String name;


    @Deprecated
    public Group() {
        // Do not Use. Required by JPA Spec
    }

    public Group(Long id) {
        this.id = id;
    }

    @Id
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @NotNull
    @Size(min = 2, max = 75)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof User) {
            return getId().equals(((Group) o).getId());
        }
        else {
            return false;
        }
    }

现在我将名称的最大尺寸从75更改为1000。

@Entity
@Table(name = "Group")

public class Group implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;
    private String name;


    @Deprecated
    public Group() {
        // Do not Use. Required by JPA Spec
    }

    public Group(Long id) {
        this.id = id;
    }

    @Id
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @NotNull
    @Size(min = 2, max = 1000)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof User) {
            return getId().equals(((Group) o).getId());
        }
        else {
            return false;
        }
    }

在此更改之后,数据库中名称的长度不会从75变为1000。

persistance.xml看起来像

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="primary" transaction-type="JTA">
        <jta-data-source>java:jboss/datasources/sampleDS</jta-data-source>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>

1 个答案:

答案 0 :(得分:0)

Hibernate的架构更新功能(hibernate.hbm2ddl.auto=update)非常有限,无法依赖。它将添加新的表,字段和外键,但这就是它所做的一切。

如果您的架构发生了变化,您将需要某种迁移机制,例如Liquibase。在生产中依赖Hbm2ddl是一种灾难。