views:

542

answers:

2

I have a JPA/Hibernate data model that I am using the Hibernate hbm2ddl tool to generate database DDL. I have some strings that should be CHAR and some that may be VARCHAR in the database. I want to minimize hand editing of the DDL (I realize that some will have to happen).

Anyone know how I should go about this? I realize that I can make all Strings VARCHARS or CHARS via a hacked dialect, but that isn't what is always appropriate in the database.

I would like to be able to do this with annotations or aop, avoiding custom column definition text in my classes.

Thanks.

+2  A: 

Annotate the strings you want to map to a CHAR in the database with @Column(columnDefinition="CHAR(<your-char-length>)").

Markus Knittig
Beat me to it :-)
Matt Solnit
Thanks for your answer, but in my last paragraph, I mention that. I am trying to avoid using columnDefinition. I was just hoping that there was a better way, as this would seem to be a reasonable want.
Jeff Walker
There's not much of a difference between adding an annotation vs adding an annotation attribute.
mtpettyp
Ah, but the difference is the contents of the attribute. That is text put directly into the schema ddl, thus, inherently database specific. Also, I have to duplicate the length of the column in the text. This particular instance isn't so bad, but worth avoiding if I can. It isn't looking promising though, I must admit.
Jeff Walker
A: 

So, I finally found that you can specify a length limit for a type in the dialect. So, I've decided that anything under a length of 10 should be char and anything over that should be varchar:

registerColumnType( Types.VARCHAR, 10, "char($l)" );

That isn't exactly what I was wanting, but that is good enough. Sad that I didn't find that earlier.

Jeff Walker