How to deep copy a Hibernate entity while using a newly generated entity identifier

前端 未结 9 657
抹茶落季
抹茶落季 2020-12-30 20:05

I\'m using a relational DB using a single column pk with a few nested tables.

I need to add a simple archiving to my project. The archiving only happens when the appl

9条回答
  •  鱼传尺愫
    2020-12-30 20:34

    I am also working with Hibernate and I got the same requirement you got. What I followed was to implement Cloneable. Below is a code example of how to do it.

    class Person implements Cloneable {
    
            private String firstName;
            private String lastName;
    
            public Object clone() {
    
                Person obj = new Person();
                obj.setFirstName(this.firstName);
                obj.setLastName(this.lastName);
    
                return obj;
            }
    
            public String getFirstName() {
                return firstName;
            }
    
            public void setFirstName(String firstName) {
                this.firstName = firstName;
            }
    
            public String getLastName() {
                return lastName;
            }
    
            public void setLastName(String lastName) {
                this.lastName = lastName;
            }
        }
    

    Or you could go to a reflection based solution but I won't recommend that. Check this website for more details.

提交回复
热议问题