为什么mapping.xml和configuration.xml必须在pojo包之外? [英] Why the mapping.xml and configuration.xml has to be outside the pojo package?

查看:294
本文介绍了为什么mapping.xml和configuration.xml必须在pojo包之外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习Hibernate,并在各种在线站点中找到了它: mapping.xml config.xml 必须在Pojo包之外定义?



为什么会这样?

还有什么区别在JPA和Hibernate之间。我通过网络搜索和根据我hibernate只是JPA的实施之一。你可以纠正我。

解决方案


我刚开始学习Hibernate,在线站点:mapping.xml和config.xml必须在pojo包之外定义?


您可以将 xml 配置。举个例子

  SessionFactory factory = new Configuration()。configure()。buildsessionFactory(); 

使用默认的 hibernate.cfg.xml 配置会话工厂C $ C>。它与

  SessionFactory factory = new Configuration()
.configure(hibernate.cfg.xml ).buildsessionFactory();

那么为什么 hibernate.cfg.xml 应该在这种情况下,在源文件夹的根目录中(或在 resources 文件夹中)?



Hibernate尝试通过类加载器加载 hibernate.cfg.xml

  InputStream stream = classLoader .getResourceAsStream(hibernate.cfg.xml); 

如果您只指定一个没有路径的名称(hibernate.cfg。 xml)类加载器将尝试在编译的源文件夹的根目录中找到资源 - bin build 文件夹,或者 war classes 文件夹。在构建根目录或 classes 文件夹中复制构建后的资源文件夹(例如,由Maven创建) 。

如果您指定

 新配置()
.configure( /一些/ POJO / hibernate.cfg.xml中)buildsessionFactory();

类加载器将尝试在 some.pojo 包。在这种情况下,Hibernate会删除前面的 / ,因为通过类加载器加载前导的 / 不正确。所以你可以使用下面的代码:

  new Configuration()
.configure(some / pojo / hibernate。 。cfg.xml中)buildsessionFactory();

对于 xml 资源。


JPA和Hibernate有什么区别。我通过网络搜索和根据我hibernate只是JPA的实施之一。你可以纠正我。


是的,Hibernate是JPA的一个实现。但还有一件事。如果您使用 SessionFactory ,您可以认为您不使用 JPA 。但在同一时间,您使用JPA批注(例如, @OneToMany )。当您使用 EntityManager 时 - 您完全使用 JPA

I have just started to learn the Hibernate and found this in various online sites : mapping.xml and config.xml has to be defined outside the pojo package?

Why is that so?

Also what's the difference between JPA and Hibernate. I searched through web and According to me hibernate is just one of the implementation of JPA. Could u correct me.

解决方案

I have just started to learn the Hibernate and found this in various online sites : mapping.xml and config.xml has to be defined outside the pojo package?

You can put xml configurations whenever you want. For an example

SessionFactory factory = new Configuration().configure().buildsessionFactory();

Configure a session factory from the default hibernate.cfg.xml. It is the same as

SessionFactory factory = new Configuration()
    .configure("hibernate.cfg.xml").buildsessionFactory();

So why hibernate.cfg.xml should be in the root of the source folder (or in the resources folder) in this situation?

Hibernate tries to load hibernate.cfg.xml via class loader

    InputStream stream = classLoader.getResourceAsStream( "hibernate.cfg.xml" );

if you specify just a name without a path ("hibernate.cfg.xml") a class loader will try to find a resource in the root of the compiled sources folder — bin or build folder, or the classes folder for war. The resources folder after build is copied (for an example, by Maven) in the root of the build or classes folder.

if you specify

new Configuration()
        .configure("/some/pojo/hibernate.cfg.xml").buildsessionFactory(); 

A class loader will try to find a resource in the some.pojo package. In this situation Hibernate removes the leading /, because of for a loading via a class loader the leading / is incorrect. So you can use a code below too

new Configuration()
        .configure("some/pojo/hibernate.cfg.xml").buildsessionFactory();

The same rule for other paths to the xml resources.

Also what's the difference between JPA and Hibernate. I searched through web and According to me hibernate is just one of the implementation of JPA. Could u correct me.

Yes, Hibernate is an implementation of JPA. But one thing more. If you use the SessionFactory you can think that you don't use JPA. But in the same time you use JPA annotations (@OneToMany for an example). When you use EntityManager — you use JPA exactly.

这篇关于为什么mapping.xml和configuration.xml必须在pojo包之外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆