在 jar 中嵌入 hibernate hbm.xml 映射
是否可以将 hibernate 映射 hbm.xml 嵌入到 jar 中并避免在 applicationContext.xml 中手动引用
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>
com/…/domain/Question.hbm.xml
并将其指向 jar/etc?
Nhibernate 有这样一个选项来指向一个程序集,从那里它获取 hbm。
注释不是一个选项
编辑: 编辑: 我的目的是删除对 hbm 的手动引用,并指向 hibernate 可以从中获取它的通用位置
<list>
<value>
com/.../Question.hbm.xml
</value>
<value>com/.../Users.hbm.xml</value>
<value>
com/.../Answers.hbm.xml
</value>
Is it possible to embed the hibernate mapping hbm.xml’s to the jar and avoid manual reference in applicationContext.xml like
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>
com/…/domain/Question.hbm.xml
and point it to a jar/etc?
Nhibernate has such an option to point to an assembly, from where it picks ups the hbm’s.
Annotations are not an option
Edit:
Edit:
My intention is to remove manual reference to hbm’s and point to a generic location from where hibernate can pick it up
<list>
<value>
com/.../Question.hbm.xml
</value>
<value>com/.../Users.hbm.xml</value>
<value>
com/.../Answers.hbm.xml
</value>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
还要澄清一下:您专门一起谈论 Spring 和 Hibernate,因为您显示的配置是 Spring 的 Hibernate 配置。 Spring的
LocalSessionFactoryBean
接受多种不同的方式来设置 Hibernate 映射文件的位置;您仅显示使用mappingResources
参数,但还有mappingLocations
,mappingJarLocations
和mappingDirectoryLocations
。我认为对于您的示例,您可能需要使用
mappingDirectoryLocations
和只需将其指向 JAR 中的特定目录,例如:Just to clarify, as well: you're specifically talking about Spring and Hibernate together, since the configuration you're showing is Spring's configuration of Hibernate. Spring's
LocalSessionFactoryBean
accepts a multitude of different ways to set the location of your Hibernate mapping files; you only show using themappingResources
parameter, but there's alsomappingLocations
,mappingJarLocations
, andmappingDirectoryLocations
.I'd think that for your example, you might want to use
mappingDirectoryLocations
and just point it to a specific directory within your JAR, such as:是的,你可以,默认情况下,Spring 在搜索映射文件时从类路径开始。因此,如果该 jar 位于您的类路径上,那么它应该会发现这些文件对于要包含在本地会话工厂 bean 中的 hibernate 映射文件没有问题。
因此,如果您的映射文件位于 my.spring.package 中并且名为 mapping.xml,则该路径
应该可以正常工作。
编辑
感谢您的评论,我将更新我的答案。
否,您不能指向 jar...但是是,您可以指向 jar 中的映射文件。
Yes, you can, by default Spring goes from the class path when search for mapping files. So if the jar is on your class path, it should find the files no problem for the hibernate mapping files to be included in your local session factory bean.
So if your mapping file is in my.spring.package and is called mapping.xml, the path
should work just fine.
EDIT
Thanks for the comment, I will update my answer.
NO, you cannot point at a jar... but YES, you may point to mapping files in the jar.
我使用 mappingJarLocations 属性,如下所示来提取给定 jar 中的所有 *hbm.xml 文件:
顺便说一句,AnnotationSessionFactoryBean 与注释和映射文件配合得很好。
编辑:我重新阅读了其中一些帖子,我还想指出,您可以通过创建抽象 bean 定义来减少重复的内容,如下所示:
I use the mappingJarLocations attribute like so to pull in all *hbm.xml files in a given jar:
AnnotationSessionFactoryBean plays nice with annotations and mapping files, btw.
Edit: I re-read some of these posts and I also want to point out that you can cut down on repetitive stuff by creating abstract bean definitions like so:
这解决了我的问题
this solved my problem
它对我有用:
It works for me: