Code:
cfg.file
------------
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/GuestBook</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">usman</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
Code:
hbm.xml
<hibernate-mapping>
<class name="it.rpf.pojo.GuestBook" table="Guestbook" >
<id name="visitorNo" type="java.lang.Integer">
<column name="visitorNo" />
<generator class="identity" />
</id>
<property name="visitorName" type="string">
<column name="visitorName" length="50" />
</property>
<property name="message" type="string">
<column name="message" length="100" />
</property>
<property name="messageDate" type="string">
<column name="messageDate" length="40" />
</property>
</class>
Code:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Session session = null;
try {
SessionFactory factory = new Configuration().configure().buildSessionFactory();
session = factory.openSession();
System.out.println(" session established ");
List<GuestBook> list = session.createQuery( "from GuestBook" ).list();
System.out.println( "size>>> "+ list.size());
for (GuestBook guestBook : list) {
System.out.println( "---> "+ guestBook.getVisitorName() );
System.out.println( "---> "+ guestBook.getMessage() );
System.out.println( "---> "+ guestBook.getMessageDate() );
}
System.out.println( "data commited ");
} catch (HibernateException e) {
e.printStackTrace();
} //catch (ParseException e) {
//e.printStackTrace();
//}
finally{
// Actual contact insertion will happen at this step
/*if (!transaction.wasCommitted()) {
transaction.rollback();
}*/
//session.flush();
session.close();
}
}
}
when the test class run it returns only the last record of the dp table. meas if db contain 5 record then test class returns 5 times of last record . help me please
suppose db contain (1) usman,hi,12/11/2013, (2) pavan,hello,2/12/2013,
output is pavan,hello,2/12/2013, pavan,hello,2/12/2013,