Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] copy object and then inserted.


I am having problems on colone object using eclipseLink objectcopyingPolicy.

I have an entity A with children entity B, and B have multiple children entity C , D. Here is the roughly for the structure.

A has oneToMany relationship to B, and A has pk aId annotated as generatedValue AUTO. B is annotated as privateOwned, @OneToMany(mappedBy="a", cascade = ALL, targetEntity=B.class)

B has oneToMany relationship to C, and B has @EmbeddedId pk class which including aId and bId, and C has @EmbeddedId pk class which include aId, bId, cId

B has oneToMany relationship to D, D has @EmbeddedId pk class which including aId and bId and dId.

when using the snippet below to colon object and then persist it,

                private EntityManagerFactory emf = Persistence.createEntityManagerFactory("brancoch");
                private EntityManager em = emf.createEntityManager();
                // first step, tried to retrieve one existing one from Db
                A a = findAById(new String("10005699"));
                String xml = getXml(a);
                // next step, colon A and then insert colonedA
                a = fromXml(xml);
                A colonedA = clone(a, em);
                private A clone(A s, EntityManager em) {
                EntityManagerImpl eme = (EntityManagerImpl) em.getDelegate();
                Session session = eme.getSession();
                ObjectCopyingPolicy p = new ObjectCopyingPolicy();
                s = (A) session.copyObject(s, p);
                return s;
In the console, i can see that it clearly insert A , and B with differnt newly generated value aId, but when inserting C, it throws error for duplciate recorde insertion, because it still using the old aId instead of the new one.

is that because I use @EmbeddedId pk class in C? If that is right, why it works for class B?

Or it is because the ObjectCopyingPocily.Depth = 3? I tried to setDepth(CASCADE_ALL_PARTS), it is still as 3. Any suggestion?

thanks a lot.

Back to the top