1

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. (e.g. archive field in mytable entity class.

create table mytable (
...
'archive‘ tinyint(1) default ’0 ’,
...
)
DataNucleus
  • 15,497
  • 3
  • 32
  • 37
Sam
  • 8,387
  • 19
  • 62
  • 97
  • 1
    http://stackoverflow.com/questions/197045/setting-default-values-for-columns-in-jpa – Don Roby Jul 11 '10 at 16:12
  • @donroby - thanks for the link, adding the default value in columnDefinition is not portable and hence wouldn't want to use that mechanism. Alternatively I could use a default value in the Entity model. Is there a different way to specify this? – Sam Jul 11 '10 at 16:16
  • I don't think there's a portable way to specify a default in the mapping. It might be best to set a default value either in the field declaration or in your constructor. – Don Roby Jul 11 '10 at 17:23

1 Answers1

2

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 insert1. Better default in your Java code.

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

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124