0

I have a entity class group.

before change

@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;
        }
    }

Now I changed the max size of the name from 75 to 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;
        }
    }

After this change the length of the name in the database is not getting changd from 75 to 1000.

The persistance.xml looks like

<?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>
Himanshu Jain
  • 39
  • 1
  • 8
  • you do realise that the schema is set when you create it the first time? there is no update schema process; you have to do updates manually – Neil Stockton Mar 27 '15 at 09:07
  • Then what is the use of entity frame work if we have to manually update it – Himanshu Jain Mar 27 '15 at 09:11
  • 1
    JPA is a mechanism for persistence/retrieval of data. The management of the data schema is a side concern of JPA only. Reading any JPA documentation would have revealed that – Neil Stockton Mar 27 '15 at 09:12
  • Using entity framework, you can represent database tables in domain model objects, adds another layer of separation, can work directly with object relationships and can query against entities instead of databases. – Prasad Kharkar Mar 27 '15 at 09:15
  • @PrasadKharkar could you please elaborate your comment. – Himanshu Jain Mar 27 '15 at 09:24
  • What do you want exactly? I will explain. – Prasad Kharkar Mar 27 '15 at 09:25
  • @PrasadKharkar I just wanted to know what is the use of this statement. – Himanshu Jain Mar 27 '15 at 09:37
  • It updates the database schema, but its unsafe. YOu can check http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do and http://stackoverflow.com/questions/221379/hibernate-hbm2ddl-auto-update-in-production – Prasad Kharkar Mar 27 '15 at 09:44

1 Answers1

0

Hibernate's schema update feature (hibernate.hbm2ddl.auto=update) is very limited and cannot be relied on. It will add new tables, fields and foreign keys, but that's about all it does.

If your schema changes, you'll need some kind of migration mechanism, such as Liquibase. Relying on Hbm2ddl in production is a recipe for disaster.

Henning
  • 16,063
  • 3
  • 51
  • 65
  • Is there any thing, that can help with the change in persistance.xml file. – Himanshu Jain Mar 27 '15 at 09:15
  • @user1730833, no, there is no switch that you can flip to make this work. Database schema changes need to be managed by the developer, the ORM framework only helps in very simple cases. – Henning Mar 27 '15 at 09:16