Should I follow both standards and use the name attribute on @Table and @Column? Or should I follow the Java conventions and rely on the default JPA mappings.
If the JPA default conventions don't match the preferred conventions of your company (there is no "one true" standard), override them. This can be done using the @Table and @Column annotations (in the particular case of Hibernate, you could also provide your own implementation of a NamingStrategy).
What is the most common approach and/or the best approach on this conflict of standards?
There is no conflict, there are Java naming conventions, there is one default convention on the JPA side for the mapping of objects to tables (because JPA had to pick one) and there is no "one true" standard on the SQL side. So:
if your company doesn't have any SQL naming conventions, you could use the JPA conventions
if you don't like them, override them
if your company has conventions in place, follow them and override the JPA defaults
I suppose that this depends on whose conventions you're referring to. I do not put the table name into the column name - what's the point of losing half your namespace just to repeat what you already know? (Some of) the rules I (try to) follow are:
Long, meaningful names are better than short names, e.g. TRANSACTION_DATE rather than TRAN_DT. Yes, I'm old enough to have written Fortran when you were limited to 6-character variable names, and I recall Basic variants where you only had A-Z, A0-Z0...A9-Z9 - but I'm also old enough to have learned better. Single-character variable names for indices, etc, are fine - and in fact traditional - but when I find a function with twelve single-letter variable names each used for multiple purposes I...am not amused.
Artificial primary keys are named ID_<<"name of table">>.
Single-field natural data primary keys are best. Two-field natural primary keys are OK. Three or more fields - create an artificial primary key and make the natural key an alternate unique key.
Thou shalt never, ever, ever count on a date, time, or date/time field to be unique. Ever. Don't forget this. I mean it.
Obfuscatory coding techniques are equivalent to incompetence.
I'm sure there's more, but it's a start. All IMHO. YMMV.
Follow both. The db convention should be there for DBA sake and manual reports and queries where the mind set is different. Use the name params on annotations to achieve this.
As far as I'm concerned either are acceptable. But if you decide you don't want the default camel case, you CAN get a different naming strategy without resorting to the tedious and error-prone task of adding the name attribute to every annotation.
Take a look at Hibernate's org.hibernate.cfg.ImprovedNamingStrategy class. It uses underscores instead of camel case. It is simply a matter of setting a property on your Hibernate configuration to use it.
You could also extend the ImprovedNamingStrategy to prepend the table name or do all uppercase if you really want, but that seems unnecessary.
发布评论
评论(4)
如果 JPA 默认约定与您公司的首选约定不匹配(不存在“唯一正确的”标准),请覆盖它们。这可以使用
@Table
和@Column
注释来完成(在 Hibernate 的特定情况下,您还可以提供自己的NamingStrategy
的实现)。没有冲突,有 Java 命名约定,JPA 端有一个默认约定用于将对象映射到表(因为 JPA 必须选择一个)并且没有“ SQL 方面的一个真正的标准。所以:
If the JPA default conventions don't match the preferred conventions of your company (there is no "one true" standard), override them. This can be done using the
@Table
and@Column
annotations (in the particular case of Hibernate, you could also provide your own implementation of aNamingStrategy
).There is no conflict, there are Java naming conventions, there is one default convention on the JPA side for the mapping of objects to tables (because JPA had to pick one) and there is no "one true" standard on the SQL side. So:
我想这取决于您所指的约定。我不会将表名放入列名中 - 只是为了重复您已经知道的内容而丢失一半的命名空间有什么意义?我(尝试)遵循的(一些)规则是:
长而有意义的名称比短名称更好,例如 TRANSACTION_DATE 而不是 TRAN_DT。是的,当你只能使用 6 个字符的变量名时,我已经足够老了,可以编写 Fortran,而且我记得基本变体中只有 AZ、A0-Z0...A9-Z9 - 但我也足够老了学得更好。索引等的单字符变量名很好 - 事实上也是传统的 - 但是当我发现一个函数有 12 个单字母变量名,每个变量名都有多种用途时,我......不觉得好笑。
人工主键被命名为 ID_<<“表名称”>>。
单字段自然数据主键是最好的。两个字段的自然主键就可以了。三个或更多字段 - 创建一个人工主键并使自然键成为备用唯一键。
您永远、永远、永远都不能指望日期、时间或日期/时间字段是唯一的。曾经。别忘了这一点。我是认真的。
混淆编码技术相当于无能。
我确信还有更多,但这只是一个开始。恕我直言。 YMMV。
分享并享受。
I suppose that this depends on whose conventions you're referring to. I do not put the table name into the column name - what's the point of losing half your namespace just to repeat what you already know? (Some of) the rules I (try to) follow are:
Long, meaningful names are better than short names, e.g. TRANSACTION_DATE rather than TRAN_DT. Yes, I'm old enough to have written Fortran when you were limited to 6-character variable names, and I recall Basic variants where you only had A-Z, A0-Z0...A9-Z9 - but I'm also old enough to have learned better. Single-character variable names for indices, etc, are fine - and in fact traditional - but when I find a function with twelve single-letter variable names each used for multiple purposes I...am not amused.
Artificial primary keys are named ID_<<"name of table">>.
Single-field natural data primary keys are best. Two-field natural primary keys are OK. Three or more fields - create an artificial primary key and make the natural key an alternate unique key.
Thou shalt never, ever, ever count on a date, time, or date/time field to be unique. Ever. Don't forget this. I mean it.
Obfuscatory coding techniques are equivalent to incompetence.
I'm sure there's more, but it's a start. All IMHO. YMMV.
Share and enjoy.
两者都遵循。数据库约定应该是为了 DBA 的缘故而存在的,并且在思维方式不同的情况下进行手动报告和查询。使用注释上的名称参数来实现此目的。
Follow both. The db convention should be there for DBA sake and manual reports and queries where the mind set is different. Use the name params on annotations to achieve this.
就我而言,两者都是可以接受的。但是,如果您决定不想要默认的驼峰式大小写,则可以获得不同的命名策略,而无需诉诸向每个注释添加名称属性的繁琐且容易出错的任务。
看一下 Hibernate 的 org.hibernate.cfg.ImprovedNamingStrategy 类。它使用下划线而不是驼峰式大小写。只需在 Hibernate 配置上设置一个属性即可使用它。
如果您确实愿意,您还可以扩展 ImprovementNamingStrategy 以在表名前面添加或全部大写,但这似乎没有必要。
As far as I'm concerned either are acceptable. But if you decide you don't want the default camel case, you CAN get a different naming strategy without resorting to the tedious and error-prone task of adding the name attribute to every annotation.
Take a look at Hibernate's org.hibernate.cfg.ImprovedNamingStrategy class. It uses underscores instead of camel case. It is simply a matter of setting a property on your Hibernate configuration to use it.
You could also extend the ImprovedNamingStrategy to prepend the table name or do all uppercase if you really want, but that seems unnecessary.