Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring data rest with composite primary key

I use spring data rest for crud. But when the entity has composite primary keys, I dont know how to to get an entity by giving the primary key.

River class:

@Entity
public class River {
    private RiverPK id;
    private Double length;
    private Timestamp date;
    private String comment;


    @Basic
    @Column(name = "length")
    public Double getLength() {
        return length;
    }

    public void setLength(Double length) {
        this.length = length;
    }

    @Basic
    @Column(name = "date")
    public Timestamp getDate() {
        return date;
    }

    public void setDate(Timestamp date) {
        this.date = date;
    }

    @Basic
    @Column(name = "comment")
    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    @Id
    public RiverPK getId() {
        return id;
    }

    public void setId(RiverPK id) {
        this.id = id;
    }
}

RiverPK class:

@Embeddable
public class RiverPK implements Serializable {
    private String name;
    private int upcode;
    private int downcode;

    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "upcode")
    public int getUpcode() {
        return upcode;
    }

    public void setUpcode(int upcode) {
        this.upcode = upcode;
    }

    @Column(name = "downcode")
    public int getDowncode() {
        return downcode;
    }

    public void setDowncode(int downcode) {
        this.downcode = downcode;
    } 

}

RiverDAO class:

@RepositoryRestResource(path = "river")
public interface RiverDAO extends JpaRepository<River, RiverPK> {
}

Then I can get river data by call get http://localhost:8080/river/, and also create new entity to db by call post http://localhost:8080/river/ {river json}

river json is:

id": {

    "name": "1",
    "upcode": 2,
    "downcode": 3

},
"length": 4.4,
"date": 1493740800000,
"comment": "6"
}

In spring data rest doc, it should be able to call get localhost:8080/river/1 (the primary key) to get the entity which primary key is 1. This can work when the entity has only one primary key. But my entity river has composite primary keys as RiverPK. If I call get localhost:8080/river/{name='1',upcode=2,downcode=3}, it returns a error "No converter found capable of converting from type [java.lang.String] to type [com.example.db.entity.RiverPK]", I means spring use {name='1',upcode=2,downcode=3} as a String, but not RiverPK type.

The question is how to call get\put\delete with composite primary keys as other normal entity?

like image 261
kidfruit Avatar asked May 03 '17 14:05

kidfruit


People also ask

How do I map a composite key in spring boot?

Mapping Composite Key using @IdClass Annotation We want to create a compound key using this information to uniquely identify each account in the database. In the above Entity-Relationship (ER) diagram, the accounts table has a composite primary key, which consists of two columns: account_number. account_type.

Can embeddable class be used as primary key?

Introduction to Composite Primary Keys and Primary Key Classes. The EJB 3.0 specification allows you to define a primary key class as a @Embeddable and use it as the primary key of your Entity bean. One or more properties can be used as members of the primary key for that particular table.

How can create primary key in spring?

For a complex type primary key, specify the primary key class in a single field as @EmbeddedId or specify the complex primary key as a field set using @IdClass and @Id. For a complex type primary key, create a class that contains the primary key, called the primary key class.


1 Answers

There is a jira issue you can look at: https://jira.spring.io/browse/DATAREST-598

This comment might be especially interesting for you

https://jira.spring.io/browse/DATAREST-598?focusedCommentId=117740&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-117740

There you find also a github link to a sample project. It uses a BackendIdConverter to convert the composite key to string and back. So the trick is to convert your composite id to a string that can be used as the path segment.

This answer might also be interesting for you https://stackoverflow.com/a/31830586/5371736

like image 113
Mathias Dpunkt Avatar answered Sep 19 '22 11:09

Mathias Dpunkt