Hi,
I created a new dynamic web project using Eclipse. For the database access, I implemented the DAO pattern. The concrete DAO objects use Hibernate to access the database (PostgreSQL in my case).
I also use the HibernateUtil from the documentation which should return a SessionFactory. But when I start my software, this class throws an error because the initial SessionFactory creation failed. The Stacktrace tells that Hibernate is not able to find my mapping files. I don't know why the mapping files cannot be found because I wrote the full qualified path in the mappings section in my hibernate.cfg.xml.
My directory structure is the following (the hbm.xml files are all in the folder src/com/mycompany/datamgmt/dao/transferobjects):
projectFolder
├───.settings
├───build
│ └───classes
│ ├───com
│ │ └───mycompany
│ │ └───datamgmt
│ │ └───dao
│ │ ├───transferobjects
│ │ └───util
│ └───tests
├───docs
├───etc
├───lib
├───src
│ ├───com
│ │ └───mycompany
│ │ └───datamgmt
│ │ └───dao
│ │ ├───transferobjects
│ │ └───util
│ └───tests
└───WebContent
├───META-INF
└───WEB-INF
└───lib
Here is the StackTrace:
Code:
Initial SessionFactory creation failed. org.hibernate.MappingNotFoundException: resource: com.mycompany.datamgmt.dao.transferobjects/User.hbm.xml not found
java.lang.ExceptionInInitializerError
at com.mycompany.datamgmt.dao.util.HibernateUtil.<clinit>(HibernateUtil.java:17)
at com.mycompany.datamgmt.dao.UserDAOImpl.saveUser(UserDAOImpl.java:36)
at tests.DAOTest.saveUser(DAOTest.java:20)
at tests.DAOTest.main(DAOTest.java:26)
Caused by: org.hibernate.MappingNotFoundException: resource: com.mycompany.datamgmt.dao.transferobjects/User.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:563)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
at com.mycompany.datamgmt.dao.util.HibernateUtil.<clinit>(HibernateUtil.java:13)
... 3 more
Exception in thread "main"
Here is the hibernate.cfg.xml:
Code:
<?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.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">xxxxxx</property>
<property name="hibernate.connection.url">jdbc:postgresql://dbserver:1507/datamgmt</property>
<property name="hibernate.connection.username">admin</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- Mapping files -->
<mapping resource="com.mycompany.datamgmt.dao.transferobjects/User.hbm.xml"/>
<mapping resource="com.mycompany.datamgmt.dao.transferobjects/Country.hbm.xml"/>
<mapping resource="com.mycompany.datamgmt.dao.transferobjects/Vendor.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Here is the User.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.mycompany.datamgmt.dao.transferobjects.User" table="tbl_user">
<id name="id" type="java.lang.Long" column="id_user">
<generator class="native"/>
</id>
<property name="surname">
<column name="surname" />
</property>
<property name="firstname">
<column name="firstname" />
</property>
<property name="email">
<column name="email" />
</property>
</class>
</hibernate-mapping>
Here is the User class:
Code:
public class User {
private Long id;
private String surname;
private String firstname;
private String email;
//getter and setter methods
}
I hope someone can help me.
Regards,
TMK