Hi All,

I know that I am comparing apples to oranges here. But I am surprised to find that a simple Hibernate query ("from Photoset p") takes 4 times as much time as JDBC. I know that there is overhead involved with the benefits that Hibernate offers. But don't you think 4 times is just too slow? Please let me know if there is a way to get a better performance out of Hibernate. Thank you!

Here are the results:

# Records: 5000

# Mapped Columns: 2
Hibernate Query / Plain JDBC: 560 ms / 150 ms

# Mapped Columns: 3
Hibernate Query / Plain JDBC: 1200 ms / 270 ms

# Mapped Columns: 5
Hibernate Query / Plain JDBC: 1680 ms / 390 ms

Thanks,
Joe

/* Hibernate Code */
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

Date beginTime = new Date();

Query query = session.createQuery("from Photoset p");
List<Photoset> photosets = query.list();
System.out.println(photosets.size() + " photoset(s) found:");

Date endTime = new Date();

tx.commit();
session.close();
System.out.println("Total Time Taken: " + (endTime.getTime() - beginTime.getTime()) + " ms");


/* Photoset.hbm.xml - Hibernate Mapping file */
<hibernate-mapping>
    <class name="Photoset" table="pub.fotoset">
        <id name="id" column="fotosetnum">
            <generator class="sequence">
                <param name="sequence">pub.nextfotosetnum</param>
            </generator>
        </id>
       
        <property name="title" column="fotosettitle" />
    </class>
</hibernate-mapping>


/* Photoset.java */
public class Photoset {
    private Long id;
    private String title;
   
    Photoset() {}
   
    public Photoset (String title) {
        this.title = title;
    }
   
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
   
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
}

/* JDBC Code that runs 4 times faster */
Date beginTime = new Date();

PreparedStatement pstmt = con.prepareStatement("select fotosetnum, fotosetitle from pub.fotoset");
ResultSet rs = pstmt.executeQuery();
int numFotosets = 0;
while (rs != null && rs.next()) {
    Photoset photoset = new Photoset();
    photoset.setId(rs.getLong(1));
    photoset.setTitle(rs.getString(2));
    numFotosets++;
}
rs.close();
System.out.println("# Photosets:" + numFotosets);

Date endTime = new Date();
System.out.println("Total Time Taken: " + (endTime.getTime() - beginTime.getTime()) + " ms");