org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found

前端 未结 1 1156
日久生厌
日久生厌 2021-01-21 07:56

I have been trying my hand to implement hibernate using a small example.

Below is my hibernate.config.xml



        
相关标签:
1条回答
  • 2021-01-21 08:39

    Please, put hibernate.cfg.xml in the root of src folder.

    And use

    <mapping resource="HibernateExposed/Person.hbm.xml"/>
    

    Hibernate loads all those files using ClassLoader.getResourceAsStream(resourcePath) resourcePath — is the path to the file

    ClassLoader tries to get access to the files in the root of bin or build folder in the IDE, or root of jar, or root of war/WEB-INF/classes/ for web-applications. Those all are the root of the class path.

    bin is a folder where Eclipse compiles your files. The root of src folder is compiled to the root of bin folder. You can check it.

    For an example

    configure("hibernate.cfg.xml")bin/hibernate.cfg.xml configure("xxx/hibernate.cfg.xml")bin/xxx/hibernate.cfg.xml

     <mapping resource="HibernateExposed/Person.hbm.xml"/>
    

    corresponds bin/HibernateExposed/Person.hbm.xml

    A path should be without the leading / for a ClassLoader. Hibernate tries to delete the leading /.

    A path like this is valid too

    <mapping resource="/HibernateExposed/Person.hbm.xml"/>
    

    Update

    You can specify path to the hibernate.cfg.xml, if you don't want to have it in the root

    new Configuration().configure("HibernateExposed/hibernate.cfg.xml")

    if you use

    new Configuration().configure()

    it should be in the root of the class path.

    0 讨论(0)
提交回复
热议问题