Hibernate:有没有一种方法可以以编程方式创建类似于现有表的新表?

发布于 2024-07-14 07:29:10 字数 1169 浏览 5 评论 0原文

我有一个网络应用程序,有很多表(每个表代表一个 POJO)。 我为每个类编写了映射文件,然后使用 Hibernate 的 SchemaExport 在数据库中生成表。 现在我想为创建的每个现有表创建 2 个附加表:

  • 用户权限表 - 存储特定于每个字段的 POJO 上的用户权限
    每一列代表POJO中的一个字段,每一行代表一个用户,每个单元格都会有“读”、“写”等值,代表用户对该字段的权限

  • 数据历史表< /strong> - 使用版本号存储所有数据历史记录
    该表将包含 POJO 表的所有列,以及 4 个附加字段:数据对象版本、事务 GUID(主键)、数据戳和执行此事务的用户。

我希望能够在创建这些表后通过 Hibernate 访问这些表。 这样我就可以轻松地添加/删除/更新其中的条目。


我的问题

附加表上的大多数列将与 POJO 表相同。 因此,我认为以某种方式引用 POJO 表可能比创建全新的表更好。 这样,如果 POJO 表中添加了新字段,这些表将自动发生更改。 但我似乎不知道该怎么做。 我想也许有某种方法:

  • 创建以某种方式引用 POJO 表的 hibernate 映射文件
    例如,在我的新 POJOPermission.hbm.xml 文件中,以某种方式指定,使用与 POJO 表相同的字段,并添加这些新字段。
  • 编写 Java 代码在 Hibernate 中创建表
    例如,我可以使用 java.lang.Class 返回 POJO 中所有字段的列表,然后迭代这些字段,不知何故将它们设置为新表的列标题,并且以某种方式调用 Hibernate 在运行时创建这些表

有人可以告诉我如何执行上述任一操作,或者解决此问题吗? 我有一种感觉,我可能以错误的方式思考这个问题......

谢谢!

[编辑 - 解决方案]

我最终使用 XSLT 将原始 hbm.xml 文件转换为新文件,更改字段类型等。并指定 hibernate.cfg.xml 文件以包含新生成的文件。 最后只需运行 schemaexport 即可生成所有 java 文件......

I have a web app that have many tables (each represents a POJO). I wrote mapping files for each class and then use Hibernate's SchemaExport to generate the tables in my database. Now I want to create 2 additional tables for each existing table that was created:

  • User permission table - stores user permissions on the POJO specific to each field
    each column represents a field in the POJO, each row represents an user, each cell will have a value of "read", "write" etc. representing user's permission on the field

  • Data history table - stores all data history with a version number
    this table will have all the columns that the POJO table has, and with 4 additional fields: data object version, transaction GUID (primary key), a datastamp and user performed this transaction.

I would like to be able to access these tables through Hibernate after they are created. So that I can easily add/remove/update entries in them.


My Question

Most of the columns on the additional tables will be the same as the POJO table. So I think it is probably better to somehow reference the POJO table instead of creating brand new tables. This way, if there is a new field added in the POJO table, these tables will have the changes automatically. But I don't seem to know how to do this. I thought maybe there's some way of:

  • create hibernate mapping files that references the POJO table somehow

    e.g. in my new POJOPermission.hbm.xml file, somehow to specify, use the same fields as the POJO table, and add these new fields.
  • write Java code to create tables in Hibernate

    e.g. I can use the java.lang.Class to return me a list of all the fields in the POJO and then iterate through these, somehow set these as column headers of my new table, and somehow call Hibernate to create these tables at runtime

Could someone please tell me how to do either of the above, or have a work around for this issue? I have a feeling that I might be thinking this in the wrong way....

Thank you!!!

[Edit - solution]

I ended up using XSLT to translate the orginal hbm.xml files into new ones, changing the field types etc. And specify the hibernate.cfg.xml file to include the newly generated ones. In the end just run schemaexport to generate all the java files together...

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

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

发布评论

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

评论(3

如果没有 2024-07-21 07:29:11

CREATE TABLE 语法是特定于实现的。 DB2 有 CREATE TABLE LIKE ,但是您需要解析目录表以检索国外和国内的键,然后生成 ALTER TABLE... 来实现相同的功能。 其他数据库可能也有类似的。

CREATE TABLE syntax is implementation specific. DB2 has CREATE TABLE LIKE , but you need to parse the catalog tables to retrieve the keys, foreign and domestic, then generate ALTER TABLE... to implement same. Other databases may have similar.

╰◇生如夏花灿烂 2024-07-21 07:29:11

使用“select into”您可以创建基本表结构的副本。 它仅复制列和数据类型,而不复制约束或索引。 如果您只想要没有数据的结构,只需指定一个不返回记录的 where 子句即可。

select * into targettable from sourcetable where 1=0

Using "select into" you can create a copy of the basic table structure. It only copies columns and data types, not constraints or indexes. If you only want the structure without data, just specify a where clause that returns no records.

select * into targettable from sourcetable where 1=0
终止放荡 2024-07-21 07:29:11

如果您想通过 Hibernate 访问用户权限和数据历史记录,那么我认为您需要通过考虑如何将这些表示为 POJO 来解决该问题。 然后,您可以生成映射以将它们存储在数据库中。

If you want to access the user permissions and data history through Hibernate then I think you need to approach the problem by thinking about how you would represent these as POJOs. You can then produce mappings for storing them in the database.

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