Spring Boot java.lang.IllegalStateException:JTA EntityManager不能使用getTransaction()

时间:2018-05-11 16:00:04

标签: java spring hibernate spring-boot jpa

我得到一个JTA EntityManager在尝试调用DELETE api时无法使用getTransaction()错误。不确定,如果我遗漏任何与JPA相关的配置。

Questionnarie.java#

@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 */

QuestionnarieController.java#

@RequestMapping(method=RequestMethod.DELETE, value="/questionnaries/{id}")
    public void deleteQuestionnarie(@PathVariable Long id){

            questionnarieService.deleteQuestionnarie(id);
    }

QuestionnarieService.java

public void deleteQuestionnarie(Long id) {
        questionnarieRepository.deleteById(id);
    }

QuestionnarieRepository.java#

public void deleteById(Long id);

application.properties

spring.datasource.jndi-name=java:jboss/datasources/testDS

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

1 个答案:

答案 0 :(得分:0)

在这种情况下,由于您要删除问题解答,而对于RequestMapping,您只使用 id 作为参数。在删除之前,您需要加载一个 Questionnarie 来删除它。

类似的东西:

public void deleteQuestionnarie(Long id) {
     Questionnarie questionnaire = questionnarieRepository.find(id);
     questionnarieRepository.delete(questionnaire);
}