I use spring to store data. But when an object has composite primary keys, I don’t know how to get the object by specifying a 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 the river data by calling get http: // localhost: 8080 / river / , and also create a new entity for db by calling post http: // localhost: 8080 / river / {river json}
Json river:
id": {
"name": "1",
"upcode": 2,
"downcode": 3
},
"length": 4.4,
"date": 1493740800000,
"comment": "6"
}
spring dow data rest, get localhost: 8080/river/1 ( ), , 1. , . RiverPK. get localhost: 8080/river/{ name= '1', upcode = 2, downcode = 3}, " , [java.lang.String], [ com.example.db.entity.RiverPK]", spring {name= '1', upcode = 2, downcode = 3} String, RiverPK.
, get\put\delete ?