I am able to reterive data if table contains only one row(No error).If table contains more than one row then its printing value of primary key column but not able to reterive other column values and getting follwing Error:
Hibernate: select insurance0_.ID as col_0_0_ from insurance insurance0_
ID: 1
Hibernate: select insurance0_.ID as ID0_, insurance0_.insurance_name as insurance2_2_0_, insurance0_.invested_amount as invested3_2_0_, insurance0_.investement_date as investem4_2_0_ from insurance insurance0_ where insurance0_.ID=?
could not load an entity: [test.hibernate.Insurance#1]
Resource Mapping file
<?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="test.hibernate.Insurance" table="insurance">
<id name="lngInsuranceId" column="ID" type="long">
<generator class="increment"/>
</id>
<property name="insuranceName">
<column name="insurance_name" />
</property>
<property name="investementAmount">
<column name="invested_amount" />
</property>
<property name="investementDate">
<column name="investement_date" />
</property>
</class>
</hibernate-mapping>
Java File
public class SelectHQLExample {
public static void main(
String[] args) {
Session session = null;
try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//Using from Clause
String SQL_QUERY =" from Insurance as insurance";
Query query = session.createQuery(SQL_QUERY);
for(Iterator it=query.iterate();it.hasNext()
{
Insurance insurance=(Insurance)it.next();
System.out.println("Before");
System.out.println("ID: " + insurance.getLngInsuranceId());
System.out.println("First Name: " + insurance.getInsuranceName());
System.out.println("Amount " + insurance.getInvestementAmount());
System.out.println("Date:: " + insurance.getInvestementDate());
}
session.close();
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
}
}
}