Java Tutorial/Hibernate/Inner Property Mapping

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

Read Component Properties Out

File: Main.java



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 CD(id varchar,title varchar,purchaseDate date,cost decimal(6,2),countryCode varchar,areaCode varchar)");
    Session session = hibernateUtil.getSession();
    CD cd = new CD();
    cd.setArtist("artist");
    cd.setTitle("title");
    SerialNumber serial = new SerialNumber();
    serial.setAreaCode("1");
    serial.setCountryCode("1");
    
    cd.setSerial(serial);
    
    String id = (String)session.save(cd);
    System.out.println(id);
    session.flush();
    Query query = session.createQuery("FROM CD");
    List list = query.list();
    session.flush();
    for (int i = 0; i < list.size(); i++) {
        CD c = (CD) list.get(i);
        System.out.println(c.getSerial().getCountryCode());
    }
    
    session.close();
    hibernateUtil.checkData("select * from CD");
  }
}





Use Component To Map Member Type

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 CD(id varchar,title varchar,purchaseDate date,cost decimal(6,2),countryCode varchar,areaCode varchar)");
    Session session = hibernateUtil.getSession();
    CD cd = new CD();
    cd.setArtist("artist");
    cd.setTitle("title");
    SerialNumber serial = new SerialNumber();
    serial.setAreaCode("1");
    serial.setCountryCode("1");
    
    cd.setSerial(serial);
    
    String id = (String)session.save(cd);
    System.out.println(id);
    session.flush();
    session.close();
    hibernateUtil.checkData("select * from CD");
  }
}