File: Main.java
import java.util.Date;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
public class Main {
public static void main(String[] args) throws Exception {
HibernateUtil hibernateUtil = new HibernateUtil();
hibernateUtil
.executeSQLCommand("create table survey (id int,name varchar, purchasedate date);");
Session session = hibernateUtil.getSession();
Survey survey = new Survey();
survey.setName("Survey");
survey.setPurchaseDate(new Date());
session.save(survey);
survey = new Survey();
survey.setName("Survey1");
survey.setPurchaseDate(new Date());
session.save(survey);
session.flush();
Criteria crit = session.createCriteria(Survey.class);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.groupProperty("id"));
projList.add(Projections.property("id"));
crit.setProjection(projList);
List results = crit.list();
System.out.println(results);
session.close();
hibernateUtil.checkData("select * from survey");
}
}
File: Survey.hbm.xml
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
File: Survey.java
import java.util.Date;
public class Survey {
private int id;
private String name;
private Date purchaseDate;
public Date getPurchaseDate() {
return purchaseDate;
}
public void setPurchaseDate(Date purchaseDate) {
this.purchaseDate = purchaseDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
File: HibernateUtil.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
Session session;
Statement st;
public HibernateUtil() throws Exception{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
// Load the JDBC driver.
Class.forName("org.hsqldb.jdbcDriver");
System.out.println("Driver Loaded.");
// Establish the connection to the database.
String url = "jdbc:hsqldb:data/tutorial";
Connection conn = DriverManager.getConnection(url, "sa", "");
System.out.println("Got Connection.");
st = conn.createStatement();
}
public Session getSession(){
return session;
}
public void executeSQLCommand(String sql) throws Exception {
st.executeUpdate(sql);
}
public void checkData(String sql) throws Exception {
ResultSet rs = st.executeQuery(sql);
ResultSetMetaData metadata = rs.getMetaData();
for (int i = 0; i < metadata.getColumnCount(); i++) {
System.out.print("\t"+ metadata.getColumnLabel(i + 1));
}
System.out.println("\n----------------------------------");
while (rs.next()) {
for (int i = 0; i < metadata.getColumnCount(); i++) {
Object value = rs.getObject(i + 1);
if (value == null) {
System.out.print("\t ");
} else {
System.out.print("\t"+value.toString().trim());
}
}
System.out.println("");
}
}
}
File: hibernate.cfg.xml
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
org.hsqldb.jdbcDriver
jdbc:hsqldb:data/tutorial
sa
1
org.hibernate.dialect.HSQLDialect
org.hibernate.context.ManagedSessionContext
false
false
org.hibernate.cache.NoCacheProvider
true