Skip to content

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

Description

@spring-projects-issues

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions