package com.javahonk;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.javahonk.bean.Address;
import com.javahonk.bean.Person;
import com.javahonk.util.HibernateUtil;

public class OneToOneInsert {

	public static void main(String[] args) {
		
		SessionFactory sf = HibernateUtil.getSessionFactory();
        Session session = sf.openSession();
        session.beginTransaction();
 
        Address address=new Address();
		address.setSteetName("Main St."); 
		address.setLocation("APT 55");
		address.setCity("Edison");
		address.setZip("10038");
		address.setState("NJ");	
         
        Person person=new Person();
        person.setFirstName("First name");
        person.setLastName("Last name");
        
        address.setPerson(person);
        person.setAddress(address);
         
        session.save(address); 
         
        List<Address> address3 = session.createQuery("from Address").list();
        for (Address address2 : address3) {
            System.out.println("Address table data:");
            System.out.println("Stree Name: "+address2.getSteetName()+" Location: "+address2.getLocation()+" City: "+address2.getCity()+" State: "+address2.getState()+" Zip: "+address2.getZip());
            System.out.println("Person table data:");
            System.out.println("First Name: "+address2.getPerson().getFirstName()+" Last name: "+address2.getPerson().getLastName()+"\n");
        }
 
        session.getTransaction().commit();
        session.close();

	}

}
