2012-03-20 141 views
0

有没有什么方法可以使用正则表达式(或只是*)来告诉Hibernate获取给定目录中甚至项目中的所有hbm.xml文件?我只是反向设计我的数据库,并且不想指定使用<映射资源=“...”>hibernate.cfg.xml映射资源

我使用Maven运行hbm2java。我是否需要在组件配置中指明某些内容?

我的hibernate.cfg.xml目前看起来是这样的:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
     <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 
     <property name="hibernate.connection.url">jdbc:oracle:thin:@....</property> 
     <property name="hibernate.connection.username">hithere</property> 
     <property name="hibernate.connection.password">shhhh</property> 
     <property name="hibernate.default_schema">foobar</property> 
     <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> 

     <mapping resource="com/foo/bar/db/Blip.hbm.xml" /> 
     <mapping resource="com/foo/bar/db/Blap.hbm.xml" /> 
     <mapping resource="com/foo/bar/db/Blop.hbm.xml" /> 
    </session-factory> 
</hibernate-configuration> 

回答

1

我不认为hibernate.cfg.xml支持正则表达式有选择性地导入资源。您可以采取务实的方式导入资源。在拨打ConfigurationbuildSessionFactory()以构建SessionFactory之前,请使用正则表达式找出想要休眠的映射文件路径列表,然后使用addResource()addClass()将它们添加到Configuration对象。

或者,如果你使用JPA与Hibernate的实现,可以通过设置属性hibernate.archive.autodetectionhbm(见this)使所有的hbm.xml文件的自动发现

+0

这是一个非常有用的答案,但很可惜,我想包含用于在它们上运行hbm2java的资源(我不知道在这个问题中指出了这一点!)然后您确认了我对可能使用正则表达式的想法。所以它不直接帮助我。但我相信别人会从中得到应用。 – Marvo 2012-03-21 18:15:08

+0

我认为你是对的,hibernate.cfg.xml根本不支持这个。过了一段时间,事实证明,将整个hbm.xml文件列表加载到文件中是件乏味的事情,但这是一次性的事情,而且我从来没有惹过它。最后,没有那么重要。谢谢你的帮助! – Marvo 2012-05-02 21:58:06