如何从 hibernate.cfg 文件中删除映射资源属性
我目前正在做一个项目。在我的项目中有很多实体/POJO 文件。目前我正在使用简单的 hibernate.cfg.xml 将所有映射文件添加到配置中,例如:-
<mapping resource="xml/ClassRoom.hbm.xml"/>
<mapping resource="xml/Teacher.hbm.xml"/>
<mapping resource="xml/Student.hbm.xml"/>
我有大量映射文件,这使我的 hibernate.cfg 文件看起来有点混乱,所以有什么办法吗我不需要将上述内容添加到 hibernate.cfg 文件中。相反,可以有任何其他方法来实现相同的目标..请帮助
i am currently working on one project. In my project there are many entity/POJO files. currently i am using simple hibernate.cfg.xml to add all the mapping files in to the configuration like :-
<mapping resource="xml/ClassRoom.hbm.xml"/>
<mapping resource="xml/Teacher.hbm.xml"/>
<mapping resource="xml/Student.hbm.xml"/>
i am having huge number of mapping files, which makes my hibernate.cfg file looks a bit messy, so is there any way so that i do not need to add the above in to the hibernate.cfg file. rather there can be any other way to achieve the same.. please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以以编程方式创建一个
Configuration
并使用Configuration#addClass(Class)
来指定映射的类(Hibernate 将为您加载映射文件)。来自javadoc:所以你可以这样做:
另请参阅
You could create a
Configuration
programmatically and useConfiguration#addClass(Class)
to specify the mapped class (and Hibernate will load the mapping file for you). From the javadoc:So you could do something like this:
See also
Hibernate Configuration 类本身不提供神奇的 addAllEntities 方法。但是您可以使用 AnnotationSessionFactoryBean setPackagesToScan 方法。请记住,它仅在使用带注释的实体类时有效,并且它是 Spring 依赖类
Hibernate Configuration class itself does not provide a magic addAllEntities method. But you can use AnnotationSessionFactoryBean setPackagesToScan method. Keep in mind it just works when using annotated Entity class and it is a Spring dependent class
是的,使用注释。
Hibernate 将自动检测使用
@Entity
注解的类。Yes, use annotations.
Hibernate will automatically detect classes that are annotated with
@Entity
.Configuration 的 addDirectory()/addJar() 方法使用在指定目录/JAR 文件中找到的所有 .hbm.xml 文件。您将需要对该位置进行硬编码,但仅限于该位置
the addDirectory()/addJar() method of Configuration uses all .hbm.xml files found inside a specified directory/JAR-File. you will need to hardcode that location, but only that one