简体   繁体   中英

Is it possible to generate a default value for a certain database column using hbm2ddl

Env: JPA 1, Hibernate 3.3.x, MySQL 5.x

We auto generate database schema using hbm2ddl export operation. Would it be possible to generate a default value for a certain @Entity member during SQL generation. (eg archive field in mytable entity class.

create table mytable (
...
'archive‘ tinyint(1) default ’0 ’,
...
)

There is no portable way to do that and the columnDefinition "trick" is definitely not a good solution. Actually, setting defaults in the generated DDL is just not a good idea, this would require the provider to go back to the database to see the result after an insert 1 . Better default in your Java code.

1 Just in case, note that you can tell Hibernate to do that using the @Generated annotation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM