Hibernate 中 n 个表到 1 个类的运行时单向映射

发布于 2024-09-10 08:38:32 字数 825 浏览 2 评论 0原文

我的数据位于不同的表中,但位于同一数据库中,所有这些都具有相同的架构。根据某些运行时变量,我想选择在查询 Hibernate 时使用哪个表。这可能吗?

请注意,我仅使用 Hibernate 将表数据读取到对象。

一种解决方案(我认为)是每个表一个 *.hbm.xml 文件和每个表一个 SessionFactory:

ClassTable1.hbm.xml: <class name="Class" table="table1">...</class>
ClassTable2.hbm.xml: <class name="Class" table="table2">...</class>
ClassTable3.hbm.xml: <class name="Class" table="table3">...</class>

HibernateUtil.java:
    getSessionFactoryTable1() {...} // load mapping ClassTable1.hbm.xml
    getSessionFactoryTable2() {...} // load mapping ClassTable2.hbm.xml
    getSessionFactoryTable3() {...} // load mapping ClassTable3.hbm.xml

丑陋。特别是考虑到 *hbm.xml 文件之间的唯一区别是表属性。

有没有一些更奇特的方法来做到这一点?理想情况下使用一个 Class.hbm.xml

I have data in different tables but in the same database, all of which have the same schema. Depending on some runtime variable, I want to choose which table to use when querying Hibernate. Is this possible?

Note that I only use Hibernate to read table-data to objects.

A solution (I think) would be one *.hbm.xml-file per table and one SessionFactory per table:

ClassTable1.hbm.xml: <class name="Class" table="table1">...</class>
ClassTable2.hbm.xml: <class name="Class" table="table2">...</class>
ClassTable3.hbm.xml: <class name="Class" table="table3">...</class>

HibernateUtil.java:
    getSessionFactoryTable1() {...} // load mapping ClassTable1.hbm.xml
    getSessionFactoryTable2() {...} // load mapping ClassTable2.hbm.xml
    getSessionFactoryTable3() {...} // load mapping ClassTable3.hbm.xml

Ugly. Especially considering that the only difference between the *hbm.xml-files is the table attribute.

Is there some fancier way of doing this? Ideally with one Class.hbm.xml.

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

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

发布评论

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

评论(2

萌梦深 2024-09-17 08:38:32

根据某些运行时变量,我想选择在查询 Hibernate 时使用哪个表。

如果您问此类问题,那么这告诉我 Hibernate 可能不是适合您的用例的最佳工具。

作为一个简单的解决方案,为什么不以编程方式控制在 HibernateUtil.getSessionFactory() 方法的配置中使用哪些 hbm.xml 文件呢? Hibernate 配置对象 可以是以编程方式配置

Depending on some runtime variable, I want to choose which table to use when querying Hibernate.

If you are asking this type of question, then this tells me that Hibernate might not be the best tool for your use case.

As an easy solution, why not just programmatically control which hbm.xml files are used in the Configuration in your HibernateUtil.getSessionFactory() method? The Hibernate Configuration object can be programmatically configured.

无边思念无边月 2024-09-17 08:38:32

您可以使用注释而不是在 hbm.xml 文件中定义类吗?您也许可以编写一个定义所有列映射的基类(因为您说它们不会更改),然后为每个“table1”、“table2”、“table3”场景扩展它一次,其中每个场景子类有一个不同的注释,指向该类的正确表。可能有用。

Could you use annotations instead of defining the class in an hbm.xml file? You might be able to write a base class that defines all the column mappings (since you said they don't change), then extend it once for each of your "table1", "table2", "table3" scenarios where each of the child classes has a different annotation that points to the correct table for that class. Might work.

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