Skip to main content

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

Hello,

You have not mentioned how aId is mapped within B, but it is likely mapped as an @Id. If so, you always have to set the aId field directly, as the provider has no way of knowing it comes from the A object. If you are using JPA 2.0, you can use M:1 and 1:1 mappings in the primary key, so that the provider knows that the foreign key to A is to be used as the primary key. Since EmbeddedIds still do not allow relationship mappings, it will not change, but the M:1 mapping in B to A would need to be annotated with a @MapsId("aId") where "aId" refers to the attribute name within the embeddedId class. This will cause the aID foreign key to be populated with the value from the reference A.

If you are not using JPA 2.0, you will need to set your ID mapping as insertable/updateable=false, and then set the B->A relationship so that it writes the foreign key. This will mean that a refresh will need to be done on B after a flush inorder to get the embeddedId's aId value set (or a postPersist used to set it)

Best Regards,
Chris

Weiquan Yuan wrote:
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?
------------------------------------------------------------------------

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top