Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Data type of model object in hibernate after creation

I am learning hibernate . I am bit confused with hbm2dll.auto update property . Change in model object data type is not changing data type of table in mysql.

Firstly I have created User Pojo with userId as data type int . Initially I have made hbm2dll property as CREATE .

@Entity(name="user")
public class User {
    @Id
    private int userid; 
    private String username;
    ....

}

For persisting data firstly I have created user object like below and saved using sessionFactory .

User user = new user();
     user.setUserid(1);
     user.setUsername("First User");

Because of above effort User table got created where userid data is int.

But later I changed the pojo’s userid data type to string .

Also I changed hbm2ddl property to update .

So after this change even user object is having string as userid , table userid data type is not changing to varchar . How to change the data type of table using hibernate .

like image 666
Sumeet Kumar Yadav Avatar asked Apr 21 '15 12:04

Sumeet Kumar Yadav


1 Answers

I believe update can not change the data type.

Imagine the field of type string will be change to Integer. What will happen to existing data?

So update (data type update) can not be guaranteed if the table is not empty.

like image 177
dieter Avatar answered Sep 22 '22 15:09

dieter