字段名称最佳实践(影子名称或复合名称)

发布于 2024-08-14 19:13:44 字数 1167 浏览 4 评论 0原文

正如上面的红色块(警告这是一个主观问题,可能会被关闭),可能没有关于此的石刻法律,但我不明白为什么这会保证关闭问题。

...除了咆哮之外,

我计划实现 Hibernate 作为我的持久性框架,这可能会在实现时解决我的问题,但我有可以转换为类和子类的数据库表(省略了现实生活中存在的许多细节和复杂性: ) ):

//dbo.a with column Name
class a {
  public String Name;
}

//dbo.b with column Name and a foreign key to dbo.a
class b extends a {
  public String Name;
}

那么,对于应该做什么以及为什么:

影子:
我可以将它们保留原样,这需要一些反射智慧(根据 http://forums .sun.com/thread.jspa?threadID=5419973 ),当处理编译时类型未知的对象时。

化合物名称:
我可以在其类名之前命名所有字段,即 a.aNameb.bName,这在现实生活中变得非常丑陋:Door.DoorName< /code> 和 RotatingDoor.RotatingDoorName

Getters 和 Setters:
我没有提到这一点,因为对于 JavaBeans,这些将从字段名称派生,并且我相信 Hibernate 使用带注释的 POJO。

为了稍微影响结果,阴影似乎是最强大的,至少在我的例子中,类 a 扩展了一个定义了 Name 的抽象类,然后定义了 b 阴影在适用时具有自己的名称。使用复合名称意味着,如果我想向所有数据库表添加 NickName 列,那么我必须将该字段添加到每种类型(然后继承的意义何在?!

)我决定找出人们,希望那些经历过实施一种或多种这些技术的优点/缺点的人,对这个问题有什么看法;或者方便的石刻最佳实践就可以了:)

-Nomad311

As the red block above (warning that this is a subjective question and may be closed) there may not be a stone etched law on this, but I don't see why that would warrant closing a question.

...Rant aside

I am planning on implementing Hibernate as my persistence framework, which may fix my problem upon implementation, but I have DB tables that translate into class and sub-class (many specifics and complications that exist in real life are omitted :) ):

//dbo.a with column Name
class a {
  public String Name;
}

//dbo.b with column Name and a foreign key to dbo.a
class b extends a {
  public String Name;
}

So, for the what should be done and why:

Shadowing:
I could leave these as is, which would require some reflection cleverness (per http://forums.sun.com/thread.jspa?threadID=5419973 ), when working with objects whose types are unknown at compile.

Compound Names:
I could name all of my fields preceded by its class's name i.e. a.aName and b.bName, which gets really ugly in real life: Door.DoorName and RotatingDoor.RotatingDoorName

Getters and Setters:
I didn't mention this one, since with JavaBeans these will be derived from the field names, and I believe Hibernate uses annotated POJOs.

To influence the results a little, shadowing seems to be the most robust, at least in my case where class a extends an abstract class with Name defined, then b shadows with its own Name when applicable. Using compound names would mean that if I wanted to add a NickName column to all my DB tables then I would have to add that field to each type (and then what's the point of inheritance?!)

In the end I decided to find out what people, hopefully who have experienced pros/cons of an implementation of one or more of these technique, have to say on the issue; or that convenient stone etched best practice will do :)

-Nomad311

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪若未夕 2024-08-21 19:13:44

如果您在所有子类中都需要成员,则应该仅在基类中定义成员。 hibernate 为类树提供了各种类型的映射。查看继承映射手册来感受一下。

您可以通过 xml 文件或注释来定义映射。

you should only define your member in the base class if you need it in all subclasses. hibernate offers various types of mappings for class trees. take a look at Inheritance mapping in the manual to get a feeling of it.

you can define your mapping either via an xml file or via annotations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文