Java Tutorial/Hibernate/Find

Материал из Java эксперт
Перейти к: навигация, поиск

Locate Record By ID

File: Main.java



import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
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);
    session.flush();
    Survey account2 = (Survey)session.load(Survey.class, survey.getId());
    System.out.println(account2.getName());
    session.close();
    hibernateUtil.checkData("select * from survey");
  }
}