Java Tutorial/Hibernate/Many to One mapping

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

Many to One mapping

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.flush();
    
    session.close();
    hibernateUtil.checkData("select * from events");
    hibernateUtil.checkData("select * from speakers");
    hibernateUtil.checkData("select * from locations");
    hibernateUtil.checkData("select * from attendees");
  }
}





Many To One Mapping: cascade all and set collection

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.flush();
    
    session.close();
    hibernateUtil.checkData("select * from events");
    hibernateUtil.checkData("select * from speakers");
    hibernateUtil.checkData("select * from locations");
    hibernateUtil.checkData("select * from attendees");
  }
}