import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;




public class PlanMachineTest {
private static final SessionFactory sessionFactory; 
static { 
try { 
Resource resource=new ClassPathResource("/applicationContext.xml"); 
BeanFactory factory=new XmlBeanFactory(resource); 
sessionFactory = (SessionFactory)factory.getBean("sessionFactory"); 
} catch (HibernateException ex) { 
throw new RuntimeException("Exception building SessionFactory: " 
+ ex.getMessage(), ex); 




    public static void main(String[] args) {
    Session session = sessionFactory.openSession();
    Machine m = (Machine)session.get(Machine.class, 1);
        PlanMachine pm = new PlanMachine();
        pm.setMachine(m);
        save(pm);
    }


    public static void save(Object o){
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(o);
        session.getTransaction().commit();
        session.close();
        sessionFactory.close();
    }
}

posted on 2014-09-05 15:46  什果的果子  阅读(681)  评论(0编辑  收藏  举报