如何从hibernate.cfg文件中删除映射资源属性
我目前正在做一个项目。在我的项目中有许多实体/POJO文件。目前,我正在使用simple hibernate.cfg.xml将所有映射文件添加到配置中,如:-如何从hibernate.cfg文件中删除映射资源属性,hibernate,hibernate-mapping,Hibernate,Hibernate Mapping,我目前正在做一个项目。在我的项目中有许多实体/POJO文件。目前,我正在使用simple hibernate.cfg.xml将所有映射文件添加到配置中,如:- <mapping resource="xml/ClassRoom.hbm.xml"/> <mapping resource="xml/Teacher.hbm.xml"/> <mapping resource="xml/Student.hbm.xml"/> 我有大量的映射文件,这使得我的hibern
<mapping resource="xml/ClassRoom.hbm.xml"/>
<mapping resource="xml/Teacher.hbm.xml"/>
<mapping resource="xml/Student.hbm.xml"/>
我有大量的映射文件,这使得我的hibernate.cfg文件看起来有点凌乱,所以有没有办法不需要将上述内容添加到hibernate.cfg文件中。相反,可以通过任何其他方式实现同样的目的。。请帮助是
Hibernate将自动检测带有
@Entity
注释的类。您可以通过编程方式创建配置,并使用来指定映射的类(Hibernate将为您加载映射文件)。从javadoc:
使用类的约定将映射作为应用程序资源读取
命名的foo.bar.foo
由文件foo/bar/foo.hbm.xml
可以将其解析为类路径资源
所以你可以这样做:
Configuration cfg = new Configuration()
.addClass(org.hibernate.auction.Item.class)
.addClass(org.hibernate.auction.Bid.class)
...
.configure();
SessionFactory factory = cfg.buildSessionFactory();
另见
Hibernate配置类本身不提供magic AddAllenties方法。但你可以使用这个方法。请记住,它只在使用带注释的实体类时起作用,并且它是一个依赖于Spring的类
AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean();
sessionFactory.setDataSource(<javax.sql.DataSource> implementation goes here)
sessionFactory.setPackagesToScan(new String [] {"xml"});
AnnotationSessionFactoryBean sessionFactory=newannotationsessionfactorybean();
setDataSource(此处实现)
setPackagesToScan(新字符串[]{“xml”});
配置的addDirectory()/addJar()方法使用指定目录/JAR文件中的所有.hbm.xml文件。您需要对该位置进行硬编码,但只需对该位置进行硬编码。。这很好,而且会很好地工作。。但为此,我需要更改类文件。。这又是一件乱七八糟的工作。。你能告诉我如何在不改变我的项目很多的情况下实现同样的目标吗?没有办法。您必须在某个地方指定此信息,这只会将“问题”转移到另一个地方。我想他想让hibernate神奇地理解他想要什么;)@波佐是真的。但是OP询问是否有任何方法可以使我不需要将上述内容添加到hibernate.cfg.xml文件中,我的建议在使用映射文件时严格地回答了这个问题:)确实如此。我的也是一个不错的选择,但让我们看看他会喜欢什么。
AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean();
sessionFactory.setDataSource(<javax.sql.DataSource> implementation goes here)
sessionFactory.setPackagesToScan(new String [] {"xml"});