JPA/Hibernate DDL generation; CHAR vs. VARCHAR

≯℡__Kan透↙ 提交于 2019-11-30 13:57:54

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/2290727/jpa-hibernate-ddl-generation-char-vs-varchar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!