我得到一个JTA EntityManager在尝试调用DELETE api时无法使用getTransaction()错误。不确定,如果我遗漏任何与JPA相关的配置。
@Entity
@Table(name = "questionnaries")
public class Questionnarie {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="id_seq")
@SequenceGenerator(name = "id_seq", sequenceName="questionnaries_id_seq")
@Column(name = "id")
private Long id;
@Column(name = "personname")
@NotNull
private String personname;
@Column(name = "question")
@NotNull
private String question;
@Column(name = "response")
@NotNull
private String response;
public Questionnarie() {}
public Questionnarie(@NotNull String personname, @NotNull String question, @NotNull String response) {
super();
this.personname = personname;
this.question = question;
this.response = response;
}
/* Not including GETTERS AND SETTERS for brevity */
@RequestMapping(method=RequestMethod.DELETE, value="/questionnaries/{id}")
public void deleteQuestionnarie(@PathVariable Long id){
questionnarieService.deleteQuestionnarie(id);
}
public void deleteQuestionnarie(Long id) {
questionnarieRepository.deleteById(id);
}
public void deleteById(Long id);
spring.datasource.jndi-name=java:jboss/datasources/testDS
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
答案 0 :(得分:0)
在这种情况下,由于您要删除问题解答,而对于RequestMapping
,您只使用 id 作为参数。在删除之前,您需要加载一个 Questionnarie 来删除它。
类似的东西:
public void deleteQuestionnarie(Long id) {
Questionnarie questionnaire = questionnarieRepository.find(id);
questionnarieRepository.delete(questionnaire);
}