Каково будет тело запроса POST / PUT для сущности, у которой есть пара внешних ключей для двух других таблиц?
Я пытался отправить через httpie как:
http POST: 8080/ api / tickets description = "создать из httpie" state_id = 1 severity_id = 1
, но получил ошибку:
{
"cause": {
"cause": {
"cause": null,
"message": "Column 'severity_id' cannot be null"
},
"message": "could not execute statement"
},
"message": "could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement"
}
вот класс сущностей и JpaRepository
@Data
@AllArgsConstructor
@NoArgsConstructor
@ExposeId
@Entity
@Table(name = "tickets")
public class Ticket {
@Id
@Column(name = "id")
private long id;
@Column(name = "description")
private String description;
@OneToOne
@JoinColumn(name = "state_id")
private State state;
@OneToOne
@JoinColumn(name = "severity_id")
private Severity severity;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "states")
public class State {
@Id
@Column(name = "id")
private long id;
@Column(name = "name")
private String name;
@OneToOne(mappedBy = "state")
private Ticket ticket;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "severities")
public class Severity {
@Id
@Column(name = "id")
private long id;
@Column(name = "name")
private String name;
@OneToOne(mappedBy = "severity")
private Ticket ticket;
}
@CrossOrigin
@RepositoryRestResource(collectionResourceRel = "tickets",
path = "tickets")
public interface TicketRepository extends JpaRepository<Ticket, Long> {
}
и вот база данных:
DROP TABLE IF EXISTS tickets,
states,
severities;
CREATE TABLE states (
id int NOT NULL,
name VARCHAR(10),
PRIMARY KEY (id),
UNIQUE KEY (name)
);
CREATE TABLE severities (
id int NOT NULL,
name VARCHAR(10),
PRIMARY KEY (id),
UNIQUE KEY (name)
);
CREATE TABLE tickets (
id int auto_increment NOT NULL,
description VARCHAR(50) NOT NULL,
state_id int NOT NULL,
severity_id int NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (state_id) REFERENCES states (id) ON DELETE CASCADE,
FOREIGN KEY (severity_id) REFERENCES severities (id) ON DELETE CASCADE
);
insert into states (id, name) VALUES(0, '');
insert into states (id, name) VALUES(1, 'Open');
insert into states (id, name) VALUES(2, 'Hold');
insert into states (id, name) VALUES(3, 'Cancel');
insert into states (id, name) VALUES(4, 'Close');
insert into severities (id, name) VALUES(0, '');
insert into severities (id, name) VALUES(1, 'Normal');
insert into severities (id, name) VALUES(2, 'Low');
insert into severities (id, name) VALUES(3, 'Medium');
insert into severities (id, name) VALUES(4, 'High');
insert into severities (id, name) VALUES(5, 'Critical');
insert into tickets (description, state_id, severity_id) VALUES("first ticket", 1, 1);
insert into tickets (description, state_id, severity_id) VALUES("second ticket", 4, 4);
insert into tickets (description, state_id, severity_id) VALUES("third ticket", 2, 2);