java 1.5:保留数据库表列名常量的最佳实践?
技术:
- Java 1.5 或 1.6
- Hibernate 3.4
为了避免在更改列名或表名时在多个位置更新列名,我希望有一个相同的常量文件。
我有以下疑问?
一种可能的解决方案是维护一个全局文件,该文件存储数据库中所有表的列名常量。 喜欢
类 DbConstants { 公共静态最终字符串 EMPLOYEE__PERFORMANCE_DESC="performance_desc"; }
在上面的例子中,employees 是表的名称,performance_desc 是列名称的名称。 因此,遵循 tablename__columnname
格式来命名常量,以避免两个不同表的两个常量之间发生冲突(如果两个表都有列名)。
我发现这种方法的一个问题是,随着数据库的增长,该文件中的常量不会增长到数千,这很难管理。另一个问题是,如果表名称更改,我必须更改所有表的前缀表名称。
假设我将上面示例中的列名称从 Performance_desc 更改为 Achievement_desc。在这种情况下,我很可能也想更改常量,即从
EMPLOYEE__PERFORMANCE_DESC
更改为EMPLOYEE__ACHIEVMENT_DESC
。由于在这种情况下,我需要更改列名称和常量名称,因此我没有看到直接在代码中使用常量而不是列名称有太多用处,尽管有一个好处是在更改常量名称时我可以使用折射来反映常量名称 名称在任何引用处都会发生变化。 看起来要么没有太多使用常量的用途,要么我使用错误的方式。在项目代码中,我似乎有人为每个表列列表定义一个类来定义常量,如下所示。
公共类 tbl_Employee { 公共静态最终 PERFORMANCE_DESC=performance_desc; }
这可以解决全局文件的一些问题,例如表名更改只会导致类名更改。 一个主要问题是我使用类的唯一目的是定义常量,这不是良好的编码实践。
阅读一些关于带有值字符串而不是 int 的 Enum 的内容,不确定它在 java 1.5 或 1.6 中是否可用,以及是否建议在给定场景中使用它。
给定定义数据库常量的最佳实践是什么?
使用数据库常量真的有用吗?
如果我像上面提到的那样为每个表使用一个类,我面临的一个问题是命名约定。表名与定义表列常量的相应类名之间应该是什么关系。
以上情况仅涵盖列名称的情况,而不包括表名称的情况。我可能喜欢在代码中使用常量而不是表名称,那么定义表名称常量的方法应该是什么。
人们经常认为,一旦产品或相关版本发布,表名和列名就不会发生太大变化。表名和列名的更改主要发生在开发阶段或功能增强(新版本)期间。避免使用表名或列名常量是否有强有力的论据?
Technology:
- Java 1.5 or 1.6
- Hibernate 3.4
To avoid update of column name on multiple places on change of column name or tablename, i want to have a constant file for same.
I have following queries?
One possible solution is to maintain one global file which stores constants for column names of all of tables in database.
likeclass DbConstants { public static final String EMPLOYEE__PERFORMANCE_DESC="performance_desc"; }
In above case employees is name of table and performance_desc is name of column name.
So kind of tablename__columnname
format is followed for naming a constant to avoid collision between two constants of two different tables if both have have column name.
One problem with this approach i see is that as database grows no of constants in this file will grow to thousands which is difficult to manage. Other problem is if table name is changed, i have to change prefix table name for all of tables.
Suppose if i change name of column in above example from performance_desc to achievements_desc. In this case it is very likely that i will like to change constant also i.e from
EMPLOYEE__PERFORMANCE_DESC
toEMPLOYEE__ACHIEVEMENT_DESC
. Since in this case i needed to change both column name and constant name i don't see much use of using constant instead of column name directly in my code although there is one benefit that on change of constant name i can use refraction to reflect constant name name change wherever referenced.
It seems either there is not much use of using constants or i am using it wrong way.In project code i have seem people defining one class for each table columns list to define constants as shown below.
public class tbl_Employee { public static final PERFORMANCE_DESC=performance_desc; }
this can solve some of issues with global file like table name change will lead to class name change only.
One major issue with this is that i am using class for sole purpose of defining constants which is not good coding practice.
Read some where about Enum with value string rather than int not sure is it available in java 1.5 or 1.6 and if its is advisable to use in given scenario.
What is best practice for given defining db constants?
Is it really useful to use db constants?
If i use one class for each table like mentioned above, one problem i face is naming convention. What should be relation between name of table and corresponding class' name which define constants for columns of the table.
Above cases covers case for only column names not table name. I may like to use constant rather table name in code so what should be approach for defining constants for table names.
It is often argued that table name and column names doesn't change much once product or related version is released. Changes in table name and column name happen mostly during development phase or feature enhancement (new version). Is it strong argument to avoid using constants for table name or column names?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Lombok 的
@FieldNameConstants
就是您要寻找的。Lombok's
@FieldNameConstants
is what you're looking for.听起来您正在问所有正确的问题 - 您想让代码更易于维护,但意识到这可能会变得笨拙并最终使代码变得更糟而不是更好。想像“颜色.红色,颜色.黑色”之类的东西。
我发现合理数量的此类常量可以使代码更具可读性。我不认为数据库列名属于这样的东西,因为
它们不会经常更改,或者至少它们不应该
它们的数量足够多,以至于您最终会得到一大堆常量列表,此时人们会停止使用它们,因为找到常量比在中查找该死的名称更困难数据库。
我见过这样的数据库文件,其中包含数千个常量,包括自定义查询、查询的一部分等(甚至像
public static Final String COMMA=",";
这样的 gem 来处理逗号的拼写将来可能会改变)。此时它们就变成了“使用一次”的字符串,没有人敢改变它们。关于字符串常量的另一个警告 - 决赛作为字符串编译到您的类中。因此,如果您重新编译常量类,但不重新编译使用该定义的类,则最终可能会导致新定义无法传播。
It sounds like you're asking all the right questions - you want to make the code more maintainable, but realize that this could get unwieldy and end up making the code worse rather than better. Think of something like "Color.RED, Color.BLACK".
I've found that a reasonable amount of constants like this makes the code more readable. I don't think db column names belong in something like this, because
they're not going to be changed often, or at least they shouldn't be
there's enough of them that you'll end with a large list of constants, at which point people stop using them because it's harder to find the constant than to just look up the damn name in the db.
I've seen db files like this with thousands of constants, including custom queries, parts of queries, etc. etc (even a gem like
public static final String COMMA=",";
to take care of the possibility that the spelling of commas will change in the future). At this point they devolve into "use once" strings, and nobody dares to change them.One other caveat about string constants - finals get compiled into your class as strings. So if you recompile the constant class, but not the class that uses the definition, it's possible to end up with the new definition not propagating.
您是否考虑过使用实体映射框架(如 Hibernate)?
它可以将所有数据库表信息(以及所有其他数据库特定信息)存储在配置文件中。它还在“硬”数据库设计和应用程序之间提供了一个隔离层(这将使吸收其中任何一个的更改变得更容易)。
Have you considered using an Entity Mapping Framework (like Hibernate)?
It can store all of the database table information (as well as all other DB specific information) in a configuration file. It also offers a separation layer between "hard" DB design and your application (which would make it easier to absorb changes to either).
在我当前的项目中,我们大量使用注释< /a> 用于许多与数据库相关的元数据,因为我们无法使用像 Hibernate 这样的框架。对于实际的列常量,是的,我们使用经过验证的
public static final String
。是的,它相当脆弱。On my current project, we are making heavy use of annotations for a lot of the DB-related metadata because we can't use a framework like Hibernate. For actual column constants, yes, we use the tried and true
public static final String
. And yes, it's fairly fragile.您可以创建一个定义常量的接口。
这是 Android 中的一个很好的示例。查找
DataColumns
接口。You could create an interface that defines the constants.
Here's a good example in Android. Look for the
DataColumns
interface.当我有关于数据库的元数据时,我也会将其存储在数据库中。这就是我看到其他系统运行的方式,如果不是最初,那么最终也是如此。为了确保维护这些数据,您可以检查数据库的架构。
When I have meta data about a database, I would store this in the database also. This is how I have seen other system run, if not initially then eventually. To ensure this data is maintained you could check against the schema of the data base.