[英]Migration from XML to Annotation configuration in Hibernate 4.2
I have always used hibernate.cfg.xml to configure Hibernate. 我一直使用hibernate.cfg.xml来配置Hibernate。 For various reasons I need to move all the configuration to Java and I have problem with mapping entities.
由于各种原因,我需要将所有配置都移至Java,并且映射实体存在问题。
Previously in XML my mappings looked like this: 以前在XML中,我的映射如下所示:
<mapping class="com.mycompany.enitites.Test" />
Everything is annotation driven, so I did not need to specify anything else. 一切都是注释驱动的,因此我不需要指定其他任何内容。
Now that I configure everything using Hibernate's Configuration.class
, how to add the mappings? 现在,我使用Hibernate的
Configuration.class
配置了所有内容,如何添加映射? I tried configuration.addClass(Test.class)
but this throws an error that file test.hbm.xml
does not exist. 我尝试了
configuration.addClass(Test.class)
但这引发了一个错误,即文件test.hbm.xml
不存在。 And I don't want to use it, I want Hibernate to read the mappings from annotations. 而且我不想使用它,我希望Hibernate从注解中读取映射。
EDIT: I tried configuration.addAnnotatedClass(Test.class)
but this throws relation "test" does not exist
on the first encountered query that involved Test.class
entity. 编辑:我尝试了
configuration.addAnnotatedClass(Test.class)
但在涉及Test.class
实体的第一个遇到的查询上,此引发relation "test" does not exist
。
I made grammar mistake in the following line: 我在以下几行中犯了语法错误:
configuration.setProperty("hibernate.hbm2ddl.auto", "update");
Which preventing Hibernate from creating tables in startup and thus not finding them... 这阻止了Hibernate在启动时创建表并因此找不到它们...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.