[
https://issues.apache.org/jira/browse/OLINGO-49?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13875110#comment-13875110
]
Chandan V.A commented on OLINGO-49:
-----------------------------------
Hello Georgi,
The Entities can be now linked using the <link> tag.
You could quickly check this behavior by executing an HTTP POST operation on
"Customers" collection that is part of the reference scenario in the processor
library.
You could use the following sample XML as Input
{code}
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xml:base="http://localhost:8080/olingo-odata2-jpa-processor-ref-web-incubating/SalesOrderProcessing.svc/">
<link href="Customers(239L)/SalesOrderDetails"
rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/SalesOrderDetails"
title="SalesOrderDetails" type="application/atom+xml;type=feed"/>
<link href="Customers(239L)/SalesOrderDetails"
rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/SalesOrderDetails"
title="SalesOrderDetails" type="application/atom+xml;type=feed">
<m:inline>
<feed
xml:base="http://localhost:8080/com.sap.core.odata.processor.ref.web/SalesOrderProcessing.svc/">
<link href="SalesOrderDetails" rel="self" title="SalesOrderItems"/>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
<link href="Customers(239L)"
rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CustomerDetails"
title="CustomerDetails" type="application/atom+xml;type=entry"/>
<content type="application/xml">
<m:properties>
<d:CreationDate>2013-01-02T00:00:00</d:CreationDate>
<d:CurrencyCode>USD</d:CurrencyCode>
<d:DeliveryStatus>01</d:DeliveryStatus>
<d:GrossAmount>0.0</d:GrossAmount>
<d:LongText m:null="true"/>
<d:NetAmount>0.0</d:NetAmount>
<d:ShortText m:null="true"/>
<d:ID>2001</d:ID>
<d:Status m:null="true"/>
</m:properties>
</content>
</entry>
</feed>
</m:inline>
</link>
<content type="application/xml">
<m:properties>
<d:Address m:type="SalesOrderProcessing.AddressInfo">
<d:City>Bangalore</d:City>
<d:Country>India</d:Country>
<d:Number>7</d:Number>
<d:Pincode>200101</d:Pincode>
<d:Street>9 cross Street</d:Street>
</d:Address>
<d:Id>239</d:Id>
<d:Name>Chandan</d:Name>
</m:properties>
</content>
</entry>
{code}
Regards
Chandan
> Create of an owning Entity creates also the inverse Entity even if it exists
> ----------------------------------------------------------------------------
>
> Key: OLINGO-49
> URL: https://issues.apache.org/jira/browse/OLINGO-49
> Project: Olingo
> Issue Type: Bug
> Components: odata2-jpa
> Affects Versions: V2 1.0.0, V2 1.1.0
> Reporter: Georgi
> Assignee: Chandan V.A
> Priority: Blocker
> Fix For: V2 1.1.0
>
>
> Consider the case when you want to add a new Entity (A) to a collection of
> that type of entities (List<A>) that belongs to another Entity(B).
> A bi-directional JPA relationships, to model the case would be:
> A: @ManyToOne
> @JoinColumn(name = "B_ID", referencedColumnName="B_ID")
> private B b;
> B: @OneToMany(mappedBy = "b", cascade = CascadeType.ALL,
> fetch=FetchType.EAGER)
> private List<A> as;
> Notice that the underlying relational model would be constrained so that B_ID
> in A's corresponding table is a foreign key and not null. Therefore, when
> inserting A rows/entities we need to make sure that B exists and provides us
> with a valid id.
> With that setup and constraints a create request for a new A that references
> an existing B entity with id 3 will send something like this:
> HTTP POST <path>/As
> "{\"aId\":0\"b\":{\"__metadata\":{\"uri\":\"Bs(3)\"}}}"
> The problem is that the inline B entry here is interpreted as new one and
> olingo tries to create a new B instance out of it and persist it, instead of
> finding the B entity and setting as value to A's property b.
> That is, after normalization with normalizeInlineEntries it becomes part of
> the oDataEntryProperties map that is passed to the write method with flag
> isCreate=true, i.e. all inside is created.
> Ultimately, during em.commit this fails with :
> javax.persistence.RollbackException: java.lang.IllegalStateException: During
> synchronization a new object was found through a relationship that was not
> marked cascade PERSIST: B@xxxx
> (The object is new because it has been created out of the existing B because
> nobody bothered to call persist on it we get this exception. It's kind of
> misleading to the real problem here)
> The client is obliged to set the referenced property 'b' or otherwise it
> would a) violate the underlying relational model constraints 2) there's no
> other way to claim to which B entity's aggregation A belongs.
> So it's up to the JpaEntity implementation to consider also this case and
> distinguish inline requests for new entities from references to existing ones
> that need not be created.
> For reference case, see:
> http://www.odata.org/documentation/odata-v2-documentation/operations/#24_Creating_new_Entries
> , the second create example with the entry and link.
--
This message was sent by Atlassian JIRA
(v6.1.5#6160)