java 在没有“目录”属性的情况下运行休眠工具注释生成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1218028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running hibernate tool annotation generation without the "catalog" attribute
提问by Titi Wangsa bin Damhore
when i run my hibernate tools it reads from the db and create java classes for each tables, and a java class for composite primary keys. that's great.
当我运行我的休眠工具时,它从数据库读取并为每个表创建 java 类,并为复合主键创建一个 java 类。那太棒了。
the problem is this line
问题是这条线
@Table(name="tst_feature"
,catalog="tstdb"
)
while the table name is required, the "catalog" attribute is not required. sometimes i want to use "tstdb", sometimes i want to use "tstdev"
虽然需要表名,但不需要“目录”属性。有时我想使用“tstdb”,有时我想使用“tstdev”
i thought which db was chosen depends on the jdbc connection url but when i change the jdbc url to point to "tstdev", it is still using "tstdb"
我认为选择哪个 db 取决于 jdbc 连接 url,但是当我将 jdbc url 更改为指向“tstdev”时,它仍在使用“tstdb”
so, i know what must be done, just don't know how its is done my options are
所以,我知道必须做什么,只是不知道它是如何完成的,我的选择是
- suppress the generation of the "catalog" attribute currently i'm doing this manually (not very productive) or i could write a program that parses the java file and remove the attribute manually but i'm hoping i don't have to
- 抑制“目录”属性的生成,目前我正在手动执行此操作(效率不高),或者我可以编写一个程序来解析 java 文件并手动删除该属性,但我希望我不必这样做
OR
或者
- find a way to tell hibernate to ignore the "catalog" attribute and use the schema that is explicitly specified. i don't know the exact setting i have to change to achive this, or even if the option is available.
- 找到一种方法告诉 hibernate 忽略“目录”属性并使用明确指定的模式。我不知道我必须更改的确切设置才能实现此目的,或者即使该选项可用。
采纳答案by Titi Wangsa bin Damhore
You need to follow 3 steps -
您需要遵循 3 个步骤 -
1) In the hibernate.cfg.xml, add this property
1) 在 中hibernate.cfg.xml,添加此属性
hibernate.default_catalog = MyDatabaseName
(as specified in above post)
(如上帖所述)
2) In the hibernate.reveng.xml, add all the table filters like this
2)在 中hibernate.reveng.xml,像这样添加所有表过滤器
table-filter match-name="MyTableName"
(just this, no catalog name here)
(只是这个,这里没有目录名称)
3) Regenerate hibernate code
3)重新生成休眠代码
You will not see any catalog name in any of the *.hbm.xmlfiles.
您不会在任何*.hbm.xml文件中看到任何目录名称。
I have used Eclipse Galileo and Hibernate-3.2.4.GA.
我使用过 Eclipse Galileo 和 Hibernate-3.2.4.GA。
回答by KLE
There is a customization to the generation, that will tell what tables to put in what catalog.
对生成进行了自定义,这将告诉将哪些表放在哪个目录中。
You can specify the catalogue manually (in reveng file, <table>element), or programmatically (in your custom ReverseEngineeringStrategy class if I remember well).
您可以手动(在 reveng 文件、<table>元素中)或以编程方式(如果我没记错的话,在您的自定义 ReverseEngineeringStrategy 类中)指定目录。
Also, I recently had to modify the generation templates.
另外,我最近不得不修改生成模板。
See the reference documentation :
请参阅参考文档:
- http://docs.jboss.org/tools/archive/3.0.1.GA/en/hibernatetools/html_single/index.html#hibernaterevengxmlfileyou can customize the catalogue of each of your tables manually
- https://www.hibernate.org/hib_docs/tools/viewlets/custom_reverse_engineering.htmwatch a movie that explains a lot ...
- http://docs.jboss.org/tools/archive/3.0.1.GA/en/hibernatetools/html_single/index.html#d0e5363for customizing the templates (I start with the directory that's closest to my needs, copy all of them in my own directory, then edit as will)
- http://docs.jboss.org/tools/archive/3.0.1.GA/en/hibernatetools/html_single/index.html#hibernaterevengxmlfile您可以手动自定义每个表的目录
- https://www.hibernate.org/hib_docs/tools/viewlets/custom_reverse_engineering.htm看一部解释很多的电影......
- http://docs.jboss.org/tools/archive/3.0.1.GA/en/hibernatetools/html_single/index.html#d0e5363用于自定义模板(我从最接近我需要的目录开始,复制所有它们在我自己的目录中,然后随意编辑)
Sorry, this could get more precise, but I don't have access to my work computer right now.
抱歉,这可能会更精确,但我现在无法访问我的工作计算机。
回答by michele
The attribute catalog is a "connection" attribute and should be specified in the "connection" config file hibernate.cfg.xmland NOT in a "data" config file *.hbm.xml.
属性目录是“连接”属性,应在“连接”配置文件中指定,hibernate.cfg.xml而不是在“数据”配置文件中指定*.hbm.xml。
I generate hibernate code via ant task <hibernatetool>and I put this replace task after regeneration (replace schema-name with your database).
我通过 ant 任务生成休眠代码,<hibernatetool>并在重新生成后放置此替换任务(用您的数据库替换 schema-name)。
<replace dir='../src' token='catalog="schema-name"' value=''>
So, after generation, attribute catalog has been removed.
因此,生成后,属性目录已被删除。
This is a workaround, but code generated works in my development a production environment with different schema-name.
这是一种解决方法,但生成的代码在我的开发中使用不同模式名称的生产环境工作。

