java 表不是由 Hibernate 创建的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1376543/
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
Table not created by Hibernate
提问by User1
I annotated a bunch of POJO's so JPA can use them to create tables in Hibernate. It appears that all of the tables are created except one very central table called "Revision". The Revision class has an @Entity(name="RevisionT")annotation so it will be renamed to RevisionT so there is not a conflict with any reserved words in MySQL (the target database).
我注释了一堆 POJO,以便 JPA 可以使用它们在 Hibernate 中创建表。除了一个名为“修订版”的非常中心的表外,所有表似乎都已创建。Revision 类有一个@Entity(name="RevisionT")注解,所以它会被重命名为 RevisionT,这样就不会与 MySQL(目标数据库)中的任何保留字发生冲突。
I delete the entire database, recreate it and basically open and close a JPA session. All the tables seem to get recreated without a problem.
我删除了整个数据库,重新创建它并基本上打开和关闭 JPA 会话。所有的表似乎都可以毫无问题地重新创建。
Why would a single table be missing from the created schema? What instrumentation can be used to see what Hibernate is producing and which errors?
为什么创建的模式中会缺少单个表?可以使用什么工具来查看 Hibernate 正在产生什么以及哪些错误?
Thanks.
谢谢。
UPDATE: I tried to create as a Derby DB and it was successful. However, one of the fields has a a name of "index". I use @org.hibernate.annotations.IndexColumnto specify the name to something other than a reserved word. However, the column is always called "index" when it is created.
更新:我尝试创建一个 Derby DB,它成功了。但是,其中一个字段的名称为“索引”。我@org.hibernate.annotations.IndexColumn用来将名称指定为保留字以外的内容。但是,该列在创建时始终称为“索引”。
Here's a sample of the suspect annotations.
这是可疑注释的示例。
@ManyToOne
@JoinColumn(name="MasterTopID")
@IndexColumn(name="Cx3tHApe")
protected MasterTop masterTop;
Instead of creating MasterTop.Cx3tHApeas a field, it creates MasterTop.Index. Why is the name ignored?
它不是MasterTop.Cx3tHApe作为字段创建,而是创建MasterTop.Index. 为什么名称被忽略?
回答by markbaldy
In case this helps anybody, this happened to me today and it turned out I was using a reserved word on my entity definition:
万一这对任何人有帮助,今天发生在我身上,结果我在我的实体定义中使用了保留字:
@OneToMany(mappedBy="list")
@OrderColumn(name="order")
private List<Wish> wishes;
"order" in this case, and Hibernate just skipped over this class. So check your user defined names! :)
在这种情况下是“order”,而 Hibernate 只是跳过了这个类。因此,请检查您的用户定义名称!:)
Cheers, Mark
干杯,马克
回答by André Chalella
Answer to your side question (What instrumentation can be used to see what Hibernate is producing and which errors?)
回答您的附带问题(可以使用什么工具来查看 Hibernate 正在产生什么以及哪些错误?)
You can org.hibernate.tool.hbm2ddl.SchemaExportto generate your tables.
你可以org.hibernate.tool.hbm2ddl.SchemaExport生成你的表。
AnnotationConfiguration conf = (new AnnotationConfiguration()).configure();
new SchemaExport(conf).create(showHql, run);
The first argument allows you to see which HQL this command generates (CREATE TABLEs etc). The second one is whether it should actually perform any modifications (ie false = dry-run).
第一个参数允许您查看此命令生成的 HQL(CREATE TABLEs 等)。第二个是它是否应该实际执行任何修改(即 false = 空运行)。
So running it with (true, false)will show you exactly what Hibernate would do to your tables, without changing anything.
所以运行它(true, false)会告诉你 Hibernate 会对你的表做些什么,而不需要改变任何东西。
回答by ChssPly76
name attributeon @Entityis not what you want to use for this purpose. Use @Tableannotation instead:
name属性上@Entity是不是你要使用这个目的是什么。改用@Table注释:
@Entity
@Table(name="RevisionT")
public class Revision {
回答by Aniket Kulkarni
It is due to column names matches with your underlying Database sql reserved words.....try by changing name of columns.....i had faced same problem by changing names it did work.
这是由于列名与您的基础数据库 sql 保留字匹配.....尝试更改列名.....我通过更改名称遇到了同样的问题,它确实有效。
回答by bentzy
For me it was a syntax mistake in columnDefinition. Easily detectable by the debug logs.
对我来说,这是columnDefinition 中的语法错误。可以通过调试日志轻松检测到。
回答by Eva M
Put hibernate.show_sql config property to true and see if Hibernate generated the create table code. If so, copy the create statement into your database client (ui or command line) and see if your database returns an error.
将 hibernate.show_sql 配置属性设置为 true 并查看 Hibernate 是否生成了创建表代码。如果是,请将 create 语句复制到您的数据库客户端(ui 或命令行)中,然后查看您的数据库是否返回错误。
For me the error was not apparent until I did so.
对我来说,错误并不明显,直到我这样做。
I used the field name 'condition' which was a reserved MySQL word. So check your field names...
我使用了字段名称“条件”,这是一个保留的 MySQL 字。所以检查你的字段名称......
回答by André Chalella
Perhaps you're using the wrong annotation. Once I accidentally annotated an entity with @org.hibernate.annotations.Entityinstead of @javax.persistence.Entity, and Hibernate just skipped it.
也许您使用了错误的注释。一旦我不小心用@org.hibernate.annotations.Entity而不是注释了一个实体@javax.persistence.Entity,Hibernate 就跳过了它。

