package io.github.liulixiang1988.contactmgr;

import io.github.liulixiang1988.contactmgr.model.Contact;
import io.github.liulixiang1988.contactmgr.model.Contact.ContactBuilder;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;

import java.util.List;

public class Application {
    //保存一个SessionFactory(我们也只需要一个)
    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        //创建StandardServiceRegistry
        final ServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
        return new MetadataSources(registry).buildMetadata().buildSessionFactory();
    }

    public static void main(String[] args) {
        Contact contact = new ContactBuilder("Liu","Lixiang")
                .withEmail("550488300@qq.com")
                .withPhone(7735556666L)
                .build();
        save(contact);

        //显示列表数据
//        for(Contact c : fetchAllContact()) {
//            System.out.println(c);
//        }
        fetchAllContact().stream().forEach(System.out::println);

    }

    @SuppressWarnings("unchecked")
    private static List<Contact> fetchAllContact() {
        Session session = sessionFactory.openSession();
        //创建Criteria
        Criteria criteria = session.createCriteria(Contact.class);

        //从Criteria中获取数据
        List<Contact> contacts = criteria.list();

        //关闭会话
        session.close();
        return contacts;
    }


    private static void save(Contact contact) {
        //打开session
        Session session = sessionFactory.openSession();

        //begin transaction
        session.beginTransaction();

        //使用session来保存对象
        session.save(contact);

        //提交session
        session.getTransaction().commit();

        //关闭session
        session.close();
    }
}

@@ -1103,16 +1103,25 @@
 %0A       
+ int id =
  save(co
@@ -1120,32 +1120,32 @@
  save(contact);%0A
-
 %0A        //%E6%98%BE%E7%A4%BA%E5%88%97%E8%A1%A8%E6%95%B0
@@ -1304,24 +1304,987 @@
 :println);%0A%0A
+        Contact c = findContactById(id);%0A%0A        c.setEmail(%22xjtuliulixiang@qq.com%22);%0A%0A        update(c);%0A%0A        fetchAllContact().stream().forEach(System.out::println);%0A%0A    %7D%0A%0A    private static Contact findContactById(int id) %7B%0A        //%E6%89%93%E5%BC%80%E4%BC%9A%E8%AF%9D%0A        Session session = sessionFactory.openSession();%0A        //%E8%8E%B7%E5%8F%96%E5%AF%B9%E8%B1%A1%E6%88%96%E8%80%85null%0A        Contact contact = session.get(Contact.class, id);%0A        //%E5%85%B3%E9%97%AD%E4%BC%9A%E8%AF%9D%0A        session.close();%0A        //%E8%BF%94%E5%9B%9E%E5%AF%B9%E8%B1%A1%0A        return contact;%0A    %7D%0A%0A    private static void update(Contact contact) %7B%0A        Session session = sessionFactory.openSession();%0A        session.beginTransaction();%0A        session.update(contact);%0A        session.getTransaction().commit();%0A        session.close();%0A    %7D%0A%0A    private static void delete(Contact contact) %7B%0A        Session session = sessionFactory.openSession();%0A        session.beginTransaction();%0A        session.delete(contact);%0A        session.getTransaction().commit();%0A        session.close();%0A
     %7D%0A%0A    @
@@ -2679,20 +2679,19 @@
  static 
-void
+int
  save(Co
@@ -2874,32 +2874,46 @@
 on%E6%9D%A5%E4%BF%9D%E5%AD%98%E5%AF%B9%E8%B1%A1%0A        
+int id = (int)
 session.save(con
@@ -2996,32 +2996,32 @@
     //%E5%85%B3%E9%97%ADsession%0A
-
         session.
@@ -3024,21 +3024,41 @@
 ion.close();
+%0A%0A        return id;
 %0A    %7D%0A%7D%0A

update and delete