Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] colon object and then insert

Hi,

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 (aId is FK and part of PK)and bId, and C has @EmbeddedId pk class which include aId(aId is FK and part of PK), bId(bId is FK and part of PK), cId

B has oneToMany relationship to D, D has @EmbeddedId pk class which including aId and bId (aId and bId is FK and part of PK)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);
                System.out.println(xml);
               
                // next step, colon A and then insert colonedA
                a = fromXml(xml);
                A colonedA = clone(a, em);
                em.getTransaction().begin();
                em.persist(colonedA);
                em.getTransaction().commit();
               
               
                -------------------------------------
                private A clone(A s, EntityManager em) {
                EntityManagerImpl eme = (EntityManagerImpl) em.getDelegate();
                Session session = eme.getSession();
                ObjectCopyingPolicy p = new ObjectCopyingPolicy();
                p.cascadePrivateParts();
                s = (A) session.copyObject(s, p);
                return s;
        }
       
========================================================
In the console, i can see that it clearly insert A with auto generated aId, but when inserting B, it insert null for aId, I use prePersist callback to set bId. for my case, how can I set aId when inserting object B, I have to use postPersist callback in object A to set it up?

Back to the top