Dear,
I am facing the following error . Please tell me where is the error ?
Exception in thread "main" org.hibernate.InvalidMappingException: Unable to read XML
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109)
at org.hibernate.cfg.Configuration.add(Configuration.java:478)
at org.hibernate.cfg.Configuration.add(Configuration.java:474)
at org.hibernate.cfg.Configuration.add(Configuration.java:647)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:730)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2109)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2081)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2061)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1929)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1908)
at com.vaannila.student.Main.main(Main.java:22)
My source code are
Address.java
Code:
/**
*
*/
package com.vaannila.student;
public class Address implements java.io.Serializable{
private long addressId;
private String street;
private String city;
private String state;
private String zipcode;
public Address(){
}
public Address (String street,String city, String state,String zipcode){
this.street=street;
this.city=city;
this.state=state;
this.zipcode=zipcode;
}
public long getAddressId() {
return addressId;
}
public void setAddressId(long addressId) {
this.addressId = addressId;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
}
Student.java
Code:
public class Student implements java.io.Serializable{
private long studentId;
private String studentName;
private Address studentAddress;
public Student(){
}
public Student(String studentName, Address StudentAddress){
this.studentName=studentName;
this.studentAddress=StudentAddress;
}
public long getStudentId() {
return studentId;
}
public void setStudentId(long studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Address getStudentAddress() {
return studentAddress;
}
public void setStudentAddress(Address studentAddress) {
this.studentAddress = studentAddress;
}
}
Address.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping>
<class name="com.vaannila.student.Address" table="Address">
<meta attribute="class-description">This class contains the Student's address details</meta>
<id name="addressId" type="long" column="Address_id">
<generator class="native" />
</id>
<property name="street" type="string" column="Address_street" length="50"/>
<property name="city" type="string" column="Address_city" length="50"/>
<property name="state" type="string" column="Address_state" length="50"/>
<property name="zipcode" column="Address_zipcode" type="string" length="10"/>
</class>
</hibernate-mapping>
Student.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.vaannila.student.Student" table="STUDENT">
<meta attribute="class-description">This class contains student details.</meta>
<id name="studentId" type="long" column="STUDENT_ID">
<generator class="native" />
</id>
<property name="studentName" type="string" not-null="true" length="100" column="STUDENT_NAME" />
<many-to-one name="studentAddress" class="com.vaannila.student.Address" column="STUDENT_ADDRESS" not-null="true" cascade="all" unique="true" />
</class>
</hibernate-mapping>
Main.java
Code:
public class Main {
public static void main(String[] args) {
Configuration config = new Configuration();
config.configure();
SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();
//starting a transaction
Transaction tx = session.beginTransaction();
//persisting…
Address address1=new Address("OMR Road" ,"Chennai","TN","6000241");
Address address2=new Address("lace Road" ,"mennai","PL","9859635");
Student student1=new Student("Exwar",address1);
Student student2=new Student("joe",address2);
session.save(student1);
session.save(student2);
//commiting the transaction
tx.commit();
session.close();
}
}
Hibernate.cfg.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- database related configurations -->
<property name="connection.driver_class">
org.hsqldb.jdbcDriver
</property>
<property name="connection.url">
jdbc:hsqldb:hsql://localhost/hiberdb
</property>
<property name="connection.username">sa</property>
<property name="connection.password"> </property>
<property name="pool_size">5</property>
<property name="connection.autocommit">true</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="dialect">
org.hibernate.dialect.HSQLDialect</property>
<!-- mapping files-->
<mapping resource="com/vaannila/student/Student.hbm.xml"/>
<mapping resource="com/vaannila/student/Address.hbm.xml"/>
</session-factory>
</hibernate-configuration>