eclipse - org.hibernate.InvalidMappingException: Could not parse mapping document from resource *.hbm.xml -
i know question has been asked lot, read every 1 of them non of them helped me. i'm writing eclipse maven project hibernate , i'm getting error:
org.hibernate.invalidmappingexception: not parse mapping document resource ir/ac/ut/ieproj/da/student.hbm.xml
my files this:
pom.xml
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>ir.ac.ut</groupid> <artifactid>ieproj</artifactid> <version>0.2</version> <packaging>war</packaging> <name>ieproj</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> </properties> <build> <resources> <resource> <directory>src/main/java</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-war-plugin</artifactid> <version>2.0</version> </plugin> <plugin> <artifactid>maven-assembly-plugin</artifactid> <version>2.2</version> <configuration> <descriptorrefs> <descriptorref>jar-with-dependencies</descriptorref> </descriptorrefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <repositories> <repository> <id>jboss repository</id> <url>http://repository.jboss.org/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupid>ir.ac.ut</groupid> <artifactid>iecommon</artifactid> <version>1.0</version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.24</version> </dependency> <dependency> <groupid>javax.servlet</groupid> <artifactid>servlet-api</artifactid> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-entitymanager</artifactid> <version>3.5.1-final</version> </dependency> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-core</artifactid> <version>3.5.1-final</version> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-api</artifactid> <version>1.7.4</version> </dependency> <dependency> <groupid>dom4j</groupid> <artifactid>dom4j</artifactid> <version>1.6.1</version> </dependency> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-validator</artifactid> <version>5.0.1.final</version> </dependency> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate</artifactid> <version>3.5.4-final</version> <type>pom</type> </dependency> <dependency> <groupid>log4j</groupid> <artifactid>log4j</artifactid> <version>1.2.17</version> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-log4j12</artifactid> <version>1.7.4</version> </dependency> </dependencies> </project>
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!doctype hibernate-configuration public "-//hibernate/hibernate configuration dtd//en" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">13812002</property> <property name="hibernate.connection.pool_size">10</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.mysqldialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- mapping files --> <mapping resource="ir/ac/ut/ieproj/da/department.hbm.xml"/> <mapping resource="ir/ac/ut/ieproj/da/studyrec.hbm.xml"/> <mapping resource="ir/ac/ut/ieproj/da/student.hbm.xml"/> </session-factory> </hibernate-configuration>
department.hbm.xml
<?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 package="ir.ac.ut.ieproj.da"> <class name="departmentrepo" table="department"> <id name="id" type="int" column="id" > <generator class="assigned"/> </id> <property name="name" column="name" type="string"/> </class> <sql-query name="getdeptname"> <return alias="department" class="departmentrepo"/> <![cdata[select * db.department d d.id = :id]]> </sql-query> </hibernate-mapping>
student.hbm.xml
<?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 package="ir.ac.ut.ieproj.da"> <class name="studentrepo" table="student"> <id name="id" type="int" column="id" > <generator class="assigned"/> </id> <property name="firstname" type="string" column="firstname" /> </property> <property name="lastname" type="string" column="lastname"/> </property> <property name="program" type="string" column="program"/> </property> <many-to-one name="dept" class="departmentrepo" cascade="all" not-null="true" column="deptid"/> </class> <sql-query name="findstudentid"> <return alias="student" class="studentrepo"/> <![cdata[select * db.student s s.id = :sid]]> </sql-query> </hibernate-mapping>
studentrepo.java
package ir.ac.ut.ieproj.da; import ir.ac.ut.ieproj.model.student; import org.hibernate.hibernateexception; import org.hibernate.mappingexception; import org.hibernate.query; import org.hibernate.session; import org.hibernate.transaction; public class studentrepo { private int id; private string firstname; private string lastname; private string program; private departmentrepo dept; public student getstudentbyid (string sid) throws mappingexception, hibernateexception, exception { student student = new student(); session session = hibernateutil.gethibernatesession(); transaction tx = session.begintransaction(); query query = session.getnamedquery("findstudentid").setlong("sid", long.valueof(sid)); studentrepo studentrepo = (studentrepo) query.uniqueresult(); student.setid(studentrepo.getid()); student.setfirstname(studentrepo.getfirstname()); student.setlastname(studentrepo.getlastname()); student.setprogram(integer.valueof(studentrepo.getprogram())); tx.commit(); session.close(); return student; } public int getid() { return id; } public void setid(int id) { this.id = id; } public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } public string getlastname() { return lastname; } public void setlastname(string lastname) { this.lastname = lastname; } public string getprogram() { return program; } public void setprogram(string program) { this.program = program; } public departmentrepo getdept() { return dept; } public void setdept(departmentrepo dept) { this.dept = dept; } }
departmentrepo.java
package ir.ac.ut.ieproj.da; import org.hibernate.hibernateexception; import org.hibernate.mappingexception; import org.hibernate.query; import org.hibernate.session; import org.hibernate.transaction; import ir.ac.ut.ieproj.model.department; public class departmentrepo { private int id; private string name; public department getdeptbyid(string id) throws mappingexception, hibernateexception, exception { session session = hibernateutil.gethibernatesession(); transaction tx = session.begintransaction(); query query = session.getnamedquery("getdeptname").setlong("id", integer.valueof(id)); departmentrepo departmentrepo = (departmentrepo) query.uniqueresult(); department department = new department(); department.setname(departmentrepo.getname()); tx.commit(); session.close(); return department; } public int getid() { return id; } public void setid(int id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } }
i think problem many-to-one tag in student.hbm.xml because department.hbm.xml , namedquery within not causing error. doing wrong? i'm using mvn package make .war file , deploy in tomcat 7.
the error could not parse mapping document
xml files not being well-formed. when error comes up, we'd better double check our xml files see if ok (all tags closed , on).
in case, message states, student.hbm.xml
file problem. have tags not belong:
<property name="firstname" type="string" column="firstname" /> </property> <------------------------------------------------------ remove <property name="lastname" type="string" column="lastname"/> </property> <------------------------------------------------------ remove <property name="program" type="string" column="program"/> </property> <------------------------------------------------------ remove
those closing </property>
tags aren't closing no one, <property
tags above them self-closed (notice />
).
Comments
Post a Comment