如何从 hibernate.cfg 文件中删除映射资源属性

发布于 2024-09-04 12:00:21 字数 368 浏览 4 评论 0原文

我目前正在做一个项目。在我的项目中有很多实体/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不如归去 2024-09-11 12:00:21

您可以以编程方式创建一个 Configuration 并使用 Configuration#addClass(Class) 来指定映射的类(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();

另请参阅

You could create a Configuration programmatically and use Configuration#addClass(Class) to specify the mapped class (and Hibernate will load the mapping file for you). From the javadoc:

Read a mapping as an application resource using the convention that a class
named foo.bar.Foo is mapped by a file foo/bar/Foo.hbm.xml
which can be resolved as a classpath resource.

So you could do something like this:

Configuration cfg = new Configuration()
    .addClass(org.hibernate.auction.Item.class)
    .addClass(org.hibernate.auction.Bid.class)
    ...
    .configure();
SessionFactory factory = cfg.buildSessionFactory();

See also

挽袖吟 2024-09-11 12:00:21

Hibernate Configuration 类本身不提供神奇的 addAllEntities 方法。但是您可以使用 AnnotationSessionFactoryBean setPackagesToScan 方法。请记住,它仅在使用带注释的实体类时有效,并且它是 Spring 依赖类

AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean();

sessionFactory.setDataSource(<javax.sql.DataSource> implementation goes here)
sessionFactory.setPackagesToScan(new String [] {"xml"});

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

AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean();

sessionFactory.setDataSource(<javax.sql.DataSource> implementation goes here)
sessionFactory.setPackagesToScan(new String [] {"xml"});
小瓶盖 2024-09-11 12:00:21

是的,使用注释

@Entity
public class Teacher {

    @Column
    private String name;

    @Column
    private String address;

    etc..
}

Hibernate 将自动检测使用 @Entity 注解的类。

Yes, use annotations.

@Entity
public class Teacher {

    @Column
    private String name;

    @Column
    private String address;

    etc..
}

Hibernate will automatically detect classes that are annotated with @Entity.

诗笺 2024-09-11 12:00:21

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文