spring-projects / spring-projects/spring-data-rest

Composite primary key not used at all in resource URI [DATAREST-846]

Open
#1,217 1 comment 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

in: repository type: bug
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

Hendy Irawan opened DATAREST-846 and commented

With the following:

@Embeddable
public class SiteStatId implements Serializable {
    @Column(name = "watchedsite_id", nullable = false)
    public Long watchedSiteId;
    @Column(name = "creationtime", nullable = false)
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    public DateTime creationTime;

    public SiteStatId() {
    }

    public SiteStatId(String id) {
        final String[] split = id.split("_");
        this.watchedSiteId = Long.valueOf(split[0]);
        this.creationTime = new DateTime(split[1]);
    }

    public SiteStatId(Long watchedSiteId, DateTime creationTime) {
        this.watchedSiteId = watchedSiteId;
        this.creationTime = creationTime;
    }

    @Override
    public String toString() {
        return watchedSiteId + "_" + creationTime;
    }
}

GETing http://localhost:8080/api/siteStats gives:

{
  "_embedded": {
    "siteStats": [
      {
        "followerCount": 144,
        "followedByCount": 43,
        "postLikedByCount": 1,
        "postCount": 45,
        "creationTime": "2016-06-26T12:20:51.954Z",
        "watchedSite_id": 1,
        "_links": {
          "self": {
            "href": "http://localhost:8080/api/siteStats"
          },
          "siteStat": {
            "href": "http://localhost:8080/api/siteStats"
          },
          "watchedSite": {
            "href": "http://localhost:8080/api/siteStats/watchedSite"
          }
        }
      },
...

i.e. the (composite) ID is not used at all.

I've tried adding String constructor, and toString, to the ID class but that has no effect.

I've tried adding @Component BackendIdConverter and <del>it also doesn't work</del> it's not used by the collection REST output. But it is used when calling the individual resource URI, e.g. /api/siteStat/some_ID. But my problem here it's not shown in the collection JSON.

@Component
public class SiteStatIdConverter implements BackendIdConverter {

    @Override
    public Serializable fromRequestId(String id, Class<?> entityType) {
        return new SiteStatId(id);
    }

    @Override
    public String toRequestId(Serializable id, Class<?> entityType) {
        return id.toString();
    }

    @Override
    public boolean supports(Class<?> delimiter) {
        return SiteStat.class.equals(delimiter) || SiteStatId.class.equals(delimiter);
    }
}

I also tried adding @Component org.springframework.core.convert.converter implementation, that also doesn't work.

I suspect it has something to do with joda-time's DateTime field but maybe there's something else I'm missing. http://stackoverflow.com/questions/28700252/spring-data-rest-embeddedid-converters-registration#comment45738933_28700252 said that just a String constructor and toString is required, but it has no effect in my case.

Test case: https://github.com/soluvas/socmedmon/tree/spring-data-rest-composite-id-bug (branch spring-data-rest-composite-id-bug)

Related to DATAJPA-770

Related:


Affects: 2.4.4 (Gosling SR4)

Reference URL: https://github.com/soluvas/socmedmon/tree/spring-data-rest-composite-id-bug

3 votes, 5 watchers

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.