Java Tutorial/Hibernate/Save
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
Flush a Session
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");
}
}
Get Row Count With Projection
File: Main.java
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
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);
crit.setProjection(Projections.rowCount());
List results = crit.list();
System.out.println(results);
session.close();
hibernateUtil.checkData("select * from survey");
}
}
Save date type data
File: Main.java
import java.util.Date;
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());
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");
}
}
Save entity with Session
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");
}
}
Save linked objects twice
File: Main.java
import java.util.HashSet;
import org.hibernate.Session;
public class Main {
public static void main(String[] args) throws Exception {
HibernateUtil hibernateUtil = new HibernateUtil();
hibernateUtil
.executeSQLCommand("create table speakers (uid int, firstName varchar,lastName varchar, event_id int);");
hibernateUtil
.executeSQLCommand("create table locations (uid int, name varchar, address varchar, event_id int);");
hibernateUtil
.executeSQLCommand("create table attendees (uid int, firstName varchar, lastName varchar, event_id int);");
hibernateUtil
.executeSQLCommand("create table events (uid int , name varchar, start_date date, duration integer,location_id int);");
Session session = hibernateUtil.getSession();
Event e = new Event();
e.setName("testSave");
Location l = new Location();
l.setName("building 1");
e.setLocation(l);
e.setSpeakers(new HashSet());
e.getSpeakers().add(new Speaker("John", "Doe"));
e.setAttendees(new HashSet());
e.getAttendees().add(new Attendee("John", "Smith"));
session.save(l);
session.save(e);
session.close();
hibernateUtil.checkData("select * from events");
hibernateUtil.checkData("select * from speakers");
hibernateUtil.checkData("select * from locations");
hibernateUtil.checkData("select * from attendees");
}
}