This section describes the DDD domain driver Poto-Framework framework snapshot

  • Poto – Framework snapshot implementation mainly refers to the idea of snapshot mechanism of Hibernate caching mechanism.
  • Poto-framework:github.com/bfxyzshb/po…

Hibernate snapshot

  • When session.getTransaction().commit() is executed, Hibernate also flushers the session’s level 1 cache, that is, compares the data in the heap to the data in the snapshot, and if the data is inconsistent, it updates the data. If they are the same, update is not executed.
public void testGet(a){
        // Get the session object
        Session session = HbnUtils.getSession();
        // Start the transaction
        session.beginTransaction();
        try {
            // Perform the get operation
            Student student = session.get(Student.class, 1);
            student.setName("Fifty");
            System.out.println(student);

            // Commit the transaction
            session.getTransaction().commit();
        } catch (Exception e) {
            // Rollback the transactionsession.getTransaction().rollback(); e.printStackTrace(); }}Copy the code

Let’s start with deep copy and shallow copy in Java

  • Shallow copy refers to copying only the object itself (including the basic variables in the object) without copying the object to which the reference contained in the object refers. The deep copy not only copies the object itself, but also all objects that the object contains references to. An example is clearer: object A1 contains a reference to B1, and B1 contains a reference to C1. A shallow copy of A1 yields A2, which still contains a reference to B1, and B1 still contains a reference to C1. The deep copy is a recurrence of the shallow copy. A1 gets A2, which contains a reference to B2 (copy of B1), and B2 contains a reference to C2 (copy of C1). If the clone() method is not overwritten, the resulting object is a shallow copy, but let’s focus on the deep copy. Run the following program to take a look at the shallow copy:
class Professor0 implements Cloneable {
    String name;
    int age;
 
    Professor0(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    public Object clone(a) throws CloneNotSupportedException {
        return super.clone(); }}class Student0 implements Cloneable {
    String name;// Constant object.
    int age;
    Professor0 p;// Student 1 and student 2 have the same reference value.
 
    Student0(String name, int age, Professor0 p) {
        this.name = name;
        this.age = age;
        this.p = p;
    }
 
    public Object clone(a) {
        Student0 o = null;
        try {
            o = (Student0) super.clone();
        } catch (CloneNotSupportedException e) {
            System.out.println(e.toString());
        }
 
        returno; }}public class ShallowCopy {
    public static void main(String[] args) {
        Professor0 p = new Professor0("wangwu".50);
        Student0 s1 = new Student0("zhangsan".18, p);
        Student0 s2 = (Student0) s1.clone();
        s2.p.name = "lisi";
        s2.p.age = 30;
        s2.name = "z";
        s2.age = 45;
        System.out.println("Name of Student S1:" + s1.name + "\n Name of professor S1, student:" + s1.p.name + "," + "\n Student s1's age of professor" + s1.p.age);// The professor of student 1}}Copy the code
  • S2 has changed, but s1 has also changed, proving that p for S1 and P for S2 refer to the same object. This is not the case in the actual requirements we have, so we need deep copy:
class Professor implements Cloneable {
    String name;
    int age;
 
    Professor(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    public Object clone(a) {
        Object o = null;
        try {
            o = super.clone();
        } catch (CloneNotSupportedException e) {
            System.out.println(e.toString());
        }
        returno; }}class Student implements Cloneable {
    String name;
    int age;
    Professor p;
 
    Student(String name, int age, Professor p) {
        this.name = name;
        this.age = age;
        this.p = p;
    }
 
    public Object clone(a) {
        Student o = null;
        try {
            o = (Student) super.clone();
        } catch (CloneNotSupportedException e) {
            System.out.println(e.toString());
        }
        o.p = (Professor) p.clone();
        returno; }}public class DeepCopy {
    public static void main(String args[]) {
        long t1 = System.currentTimeMillis();
        Professor p = new Professor("wangwu".50);
        Student s1 = new Student("zhangsan".18, p);
        Student s2 = (Student) s1.clone();
        s2.p.name = "lisi";
        s2.p.age = 30;
        System.out.println("name=" + s1.p.name + "," + "age=" + s1.p.age);// The professor of student 1 does not change.
        longt2 = System.currentTimeMillis(); System.out.println(t2-t1); }}Copy the code
  • Of course, we have another deep-copy method, which serializes objects:
class Professor2 implements Serializable {
    / * * * * /
    private static final long serialVersionUID = 1L;
    String name;
    int age;
 
    Professor2(String name, int age) {
        this.name = name;
        this.age = age; }}class Student2 implements Serializable {
    / * * * * /
    private static final long serialVersionUID = 1L;
    String name;// Constant object.
    int age;
    Professor2 p;// Student 1 and student 2 have the same reference value.
 
    Student2(String name, int age, Professor2 p) {
        this.name = name;
        this.age = age;
        this.p = p;
    }
 
    public Object deepClone(a) throws IOException, OptionalDataException,
            ClassNotFoundException {
        // Write the object to the stream
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream oo = new ObjectOutputStream(bo);
        oo.writeObject(this);
        // Read from the stream
        ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
        ObjectInputStream oi = new ObjectInputStream(bi);
        return(oi.readObject()); }}public class DeepCopy2 {
 
    / * * *@param args
     */
    public static void main(String[] args) throws OptionalDataException,
            IOException, ClassNotFoundException {
        long t1 = System.currentTimeMillis();
        Professor2 p = new Professor2("wangwu".50);
        Student2 s1 = new Student2("zhangsan".18, p);
        Student2 s2 = (Student2) s1.deepClone();
        s2.p.name = "lisi";
        s2.p.age = 30;
        System.out.println("name=" + s1.p.name + "," + "age=" + s1.p.age); // The professor of student 1 does not change.
        longt2 = System.currentTimeMillis(); System.out.println(t2-t1); }}Copy the code

Poto – Framework SnapshotUtils implementation

  • SnapshotUtils: SnapshotUtils: SnapshotUtils: SnapshotUtils: SnapshotUtils: SnapshotUtils
public class SnapshotUtils {

    public static <T extends AggregateRoot> T snapshot(T aggregate)
            throws IOException, ClassNotFoundException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(aggregate);

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return(T) ois.readObject(); }}Copy the code
  • Poto – Framework uses SnapshotUtils to implement a deep copy of the object to make a snapshot of the object, the main purpose is to solve the object contains reference to the object, the new second object to do diff discovery of the object’s changed attributes, complete the data update, reduce unnecessary update operations.