简体   繁体   English

在Hibernate 4.2中从XML迁移到注释配置

[英]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.

相关问题 如何将Hibernate多对多关系从xml切换到基于注释的配置? - How to switch an Hibernate many-to-many relationship from xml to annotation based configuration? Hibernate 4注释配置 - Hibernate 4 Annotation Configuration 如何从XML Spring调度配置转向注释/代码配置? - How to go from XML Spring scheduling configuration to annotation/code configuration? 休眠注释/ XML解决方法 - Hibernate Annotation/XML workaround 休眠-从注释到XML配置文件 - Hibernate - from Annotations to XML Configuration files 基于Hibernate SessionFactory注释的配置 - Hibernate SessionFactory annotation based configuration 春季:将数据库配置从XML文件迁移到注释 - Spring: Migrating database configuration from XML file to annotation Spring 3的最小Hibernate 4 XML配置,用于基于注释的事务管理和对象映射? - Minimal Hibernate 4 XML configuration with Spring 3 for annotation based transaction management and object mapping? 在xml配置文件中找不到类[org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean] - Cannot find class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean] in xml configuration file Spring注释:将XML配置转换为注释 - Spring Annotation : Converting XML Configuration to Annotation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM