spring-projects / spring-projects/spring-data-rest
Entity with @OneToOne relationship with Entity in another spring-data-rest service [DATAREST-1272]
@odrotbohm is already working on this.
Since Dec 31, 2020.
- Dominant language
- Java
- Stars
- 958
- Forks
- 568
- PR merge metrics
- No merged PRs in 30d
Description
Farrukh Najmi opened DATAREST-1272 and commented
Please see issue described here.
In my spring-data-rest application I have the following entities:
@Entity
public class com.foo.client.Foo {
@Id
@Column(name = "id", nullable = false, length = 48)
public String id = UUID.randomUUID().toString();
@OneToOne
public Bar bar;
}
@Entity
public class Bar {
@Id
@Column(name = "id", nullable = false, length = 48)
public String id = UUID.randomUUID().toString();
public String name;
}
I have JpaRepository for each entity class:
@RepositoryRestResource(collectionResourceRel = "foos", path = "foos")
public interface FooRepository extends JpaRepository<Foo, String> {
}
@RepositoryRestResource(collectionResourceRel = "bars", path = "bars")
public interface BarRepository extends JpaRepository<Bar, String> {
}
I create an instance for both Foo and Bar using:
curl -i -X PUT -H "Content-Type:application/json"
-d '{"id": "urn:foo:test:0"}' http://localhost:8000/foos
curl -i -X PUT -H "Content-Type:application/json"
-d '{"id": "urn:bar:test:0", "name" : "0"}' http://localhost:8000/bars
I then associate the Bar instance to the Foo instance using:
curl -i -X PUT -d "http://localhost:8000/bars/urn:bar:test:0\n"
-H "Content-Type:text/uri-list" "http://localhost:8000/foos/urn:foo:test:0/bar"
When the two entities are defined in the same spring-data-rest service endpoint then all is well and I can get the bar instance associated with the foo instance using:
curl -i -X GET -H "Content-Type:application/json" "http://localhost:8000/foos/urn:foo:test:0/bar"
{{
Question: Looking at the postgresdb where entities are stored I see an association table FOO_BAR where there are two columns holding the id of each entity. However, I do not see where the URL for bar is stored and would like to understand where it is stored.
}}
Now if I split my app into two separate spring-data-rest services one foo-service for Foo and another bar-service for bar at separate ports, and also split the Repository classes between two project, then creating the association does not work and I get a 404. See modified code below:
I create an instance for both Foo and Bar using:
curl -i -X PUT -H "Content-Type:application/json"
-d '{"id": "urn:foo:test:0"}' http://localhost:8000/foos
curl -i -X PUT -H "Content-Type:application/json"
-d '{"id": "urn:bar:test:0", "name" : "0"}' http://localhost:8001/bars
{{
I then associate the Bar instance to the Foo instance using:
}}
curl -i -X PUT -d "http://localhost:8001/bars/urn:bar:test:0\n"
-H "Content-Type:text/uri-list" "http://localhost:8000/foos/urn:foo:test:0/bar"
This last request above gives me a 404 when Foo and Bar are managed by different spring-data-rest services.
How can I get the second case to work?
Note I used this excellent resource for my example.
{{}}
No further details from DATAREST-1272
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.