i have try with association as u said but i am getting the same null pointer exception.
once again i send the modified outer join query with the foreign key mapping.
help me it is very urgent.where i have done the mistake
my main class package simple;
import org.hibernate.Query; import org.hibernate.MappingException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import java.util.*; import simple.Contact; import simple.Insurance; 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 emp.firstname,Insurance.InsuranceName,Insurance.InvestmentAmount FROM simple.Contact emp left outer join fetch simple.Insurance Insurance where emp.InsID = Insurance.InsID"; Query query = sess.createQuery(sql_query); Iterator ite = query.list().iterator(); System.out.println("First Name\t"+"Insurance Name\t"+"InvestmentAmount"); while ( ite.hasNext() ) { Object[] pair = (Object[]) ite.next(); Contact con = (Contact) pair[0]; Insurance ins = (Insurance) pair[1]; System.out.print(con.getFirstName()); System.out.print("\t"+ins.getInsuranceName()); System.out.print("\t\t"+ins.getInvestementAmount()); System.out.println(); /*System.out.println("First Name:"+pair[0]); System.out.println("Insurance name:"+pair[1]); System.out.println("InvestmentAmount:"+pair[2]);*/ } sess.close(); } catch(Exception e ){ System.out.println(e.getMessage()); e.printStackTrace(); }
}
}
package simple; import java.util.*; public class Contact { private String firstName; private String lastName; private int insid; private int id; private List insurance; public List getInsurance() { return insurance; } public void setInsurance(List insurance) { this.insurance = insurance; }
/** * @return insid */ /** * @return First Name */ public String getFirstName() { return firstName; }
/** * @return Last name */ public String getLastName() { return lastName; } public int getinsid() { return insid; }
/** * @param string Sets the Insid */ public void setinsid(int i) { insid = i; }
/** * @param string Sets the First Name */ public void setFirstName(String string) { firstName = string; }
/** * @param string sets the Last Name */ public void setLastName(String string) { lastName = string; } public int getId() { return id; }
/** * @param l Sets the ID */ public void setId(int i) { id = i; }
}
package simple;
import java.util.List;
public class Insurance { private String insuranceName; private double investementAmount; private int id; private List Contact; public List getContact() { return Contact; } public void setContact(List Contact) { this.Contact = Contact; } public String getInsuranceName() { return insuranceName; } public void setInsuranceName(String insuranceName) { this.insuranceName = insuranceName; } public double getInvestementAmount() { return investementAmount; } public void setInvestementAmount(double investementAmount) { this.investementAmount = investementAmount; } public int getId() { return id; } public void setId(int Id) { this.id = id; } } contact.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="simple" > <class name="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="simple.Insurance"/> </bag> </class> </hibernate-mapping> } insurance.hbm..xml <?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 package="simple"> <class name="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="simple.Contact" column="InsID" insert="false" update="false"/> </class> </hibernate-mapping>
<?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>
|