i am new to hibernate i try the example for foreign key mapping but it show the exception mentioned below. i send the xml files and pojo classes. please reply me soon it is very urgent. i have to finish this task.
advance thanks for those u reply to me.
contact.hbm.xml mapping. <?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="join.Contact" table="emp"> <id name="id" type="int" column="ID" > <generator class="increment"/> </id>
<property name="firstName" column ="firstname"/> <property name="lastName" column ="lastname"/> <property name="insid" column ="InsID"/> <bag name="insurance" inverse=" true" cascade="all,delete-orphan"> <key column="InsID"/> <one-to-many class=" join.Insurance"/> </bag> </class> </hibernate-mapping>
insurance.xml files <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="join.Insurance" table="Insurance"> <id name="id" column ="InsID" type="int"> <generator class="increment"/> </id> <property name="insuranceName" column="InsuranceName" type="String"/> <property name="investementAmount" column="InvestmentAmount" type="double"/> <many-to-one name="Contact" class=" join.Contact" column="InsID" insert="false" update="false"/> </class> </hibernate-mapping> 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/sakthi</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">admin</property> <property name="hibernate.connection.pool_size">10</property> <property name="hibernate.jdbc.batch_size">50</property> <property name="show_sql">true</property> <property name="connection.autocommit">false</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- Mapping files --> <mapping resource="contact.hbm.xml"/> <mapping resource="insurance.hbm.xml"/> </session-factory> </hibernate-configuration>
EmpDetail.java package sample;
public class EmpDetail{ private String firstname; private String city; private int empid; public String getfirstname() { return firstname; } public void setfirstname(String firstname) { this.firstname =firstname; } public String getcity() { return city; } public void setcity(String city) { this.city = city; } public int getempid() { return empid; } public void setempid(int Empid) { this.empid =empid; } } Employee.java package sample;
public class Employee { private String firstname; private String email; private int empid;
/** * @return Email */
/** * @return First Name */ public String getfirstname() { return firstname; } public void setfirstname(String string) { firstname = string; } public String getemail() { return email; }
/** * @param string Sets the Email */ public void setemail(String string) { email = string; }
/** * @param string Sets the First Name */
/** * @param string sets the Last Name */ public int getempid() { return empid; }
/** * @param l Sets the ID */ public void setempid(int i) { empid = i; }
}
my main class
package sample;
import org.hibernate.Query; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import java.util.*; /*import java.util.List; import java.util.ListIterator;*/ import sample.Employee; import sample.EmpDetail; public class Inner { public static void main(String[] args) { // TODO Auto-generated method stub Session sess = null; try{ SessionFactory fact = new Configuration().configure().buildSessionFactory(); sess = fact.openSession(); String sql_query = " select empdet.firstname,empdet.city,empmail.firstname,empmail.email FROM sample.EmpDetail empdet left outer join fetch sample.Employee empmail where empdet.Empid = empmail.Empid"; Query query = sess.createQuery(sql_query); Iterator ite = query.list().iterator(); System.out.println("FirstName\t"+"Email\t"+"FirstName\t"+"City"); while ( ite.hasNext() ) { Object[] pair = (Object[]) ite.next(); Employee mail = (Employee) pair[0]; EmpDetail det = (EmpDetail) pair[1]; System.out.print(mail.getfirstname()); System.out.print(mail.getemail()); System.out.print("\t"+det.getfirstname()); System.out.print("\t\t"+det.getcity()); System.out.println(); } sess.close(); } catch(Exception e ){ System.out.println(e.getMessage()); e.printStackTrace(); }
}
}
11:43:53,250 INFO Environment:456 -11:43:53,250 INFO Environment:469 -11:43:53,250 INFO Environment:502 -11:43:53,250 INFO Environment:532 -11:43:53,250 INFO Configuration:1228 -11:43:53,265 INFO Configuration:1199 -11:43:53,375 INFO Configuration:439 -11:43:53,484 INFO HbmBinder:256 -11:43:53,500 INFO Configuration:439 -11:43:53,546 INFO HbmBinder:256 -11:43:53,609 INFO Configuration:1340 -11:43:53,609 INFO Configuration:844 -11:43:53,609 INFO Configuration:848 -Association references unmapped class: join.Insurance org.hibernate.MappingException: Association references unmapped class: join.Insurance at org.hibernate.cfg.HbmBinder.bindCollectionSecondPass(HbmBinder.java:1946) at org.hibernate.cfg.HbmBinder$CollectionSecondPass.secondPass(HbmBinder.java:2295) at org.hibernate.cfg.HbmBinder$SecondPass.doSecondPass(HbmBinder.java:2266) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:853) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1036) at join.Inner.main(Inner.java:16)
|