Java Tutorial/Hibernate/Introduction

Материал из Java эксперт
Версия от 17:44, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Hibernate and HSQL

File: Main.java



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);");
    Session session = hibernateUtil.getSession();
    Survey survey = new Survey();
    survey.setName("Survey");
    System.out.println(survey.getId());
    
    session.save(survey);
    session.flush();
    
    System.out.println(survey.getId());
    Survey surveyInSession = (Survey) session.get(Survey.class, survey.getId());
    System.out.println(surveyInSession.getName());
    session.close();
    hibernateUtil.checkData("select * from survey");
  }
}





Setup Hibernate Environment

File: Main.java



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);");
    Session session = hibernateUtil.getSession();
    Survey survey = new Survey();
    survey.setName("Survey");
    System.out.println(survey.getId());
    
    session.save(survey);
    session.flush();
    
    System.out.println(survey.getId());
    Survey surveyInSession = (Survey) session.get(Survey.class, survey.getId());
    System.out.println(surveyInSession.getName());
    session.close();
    hibernateUtil.checkData("select * from survey");
  }
}